blob: 89407b2977bba5f6665b9692df53a4adffeadcae [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 Moolenaar33570922005-01-25 22:26:29 +00003741 hi = hash_find(ht, varname);
3742 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003744 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003746 || var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003747 return FAIL;
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003748
3749 if (ht == &globvarht)
3750 d = &globvardict;
3751 else if (current_funccal != NULL
3752 && ht == &current_funccal->l_vars.dv_hashtab)
3753 d = &current_funccal->l_vars;
3754 else
3755 {
3756 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3757 d = di->di_tv.vval.v_dict;
3758 }
3759 if (d == NULL || tv_check_lock(d->dv_lock, name, FALSE))
3760 return FAIL;
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 delete_var(ht, hi);
3763 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 if (forceit)
3767 return OK;
3768 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770}
3771
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772/*
3773 * Lock or unlock variable indicated by "lp".
3774 * "deep" is the levels to go (-1 for unlimited);
3775 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3776 */
3777 static int
3778do_lock_var(lp, name_end, deep, lock)
3779 lval_T *lp;
3780 char_u *name_end;
3781 int deep;
3782 int lock;
3783{
3784 int ret = OK;
3785 int cc;
3786 dictitem_T *di;
3787
3788 if (deep == 0) /* nothing to do */
3789 return OK;
3790
3791 if (lp->ll_tv == NULL)
3792 {
3793 cc = *name_end;
3794 *name_end = NUL;
3795
3796 /* Normal name or expanded name. */
3797 if (check_changedtick(lp->ll_name))
3798 ret = FAIL;
3799 else
3800 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003801 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 if (di == NULL)
3803 ret = FAIL;
3804 else
3805 {
3806 if (lock)
3807 di->di_flags |= DI_FLAGS_LOCK;
3808 else
3809 di->di_flags &= ~DI_FLAGS_LOCK;
3810 item_lock(&di->di_tv, deep, lock);
3811 }
3812 }
3813 *name_end = cc;
3814 }
3815 else if (lp->ll_range)
3816 {
3817 listitem_T *li = lp->ll_li;
3818
3819 /* (un)lock a range of List items. */
3820 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3821 {
3822 item_lock(&li->li_tv, deep, lock);
3823 li = li->li_next;
3824 ++lp->ll_n1;
3825 }
3826 }
3827 else if (lp->ll_list != NULL)
3828 /* (un)lock a List item. */
3829 item_lock(&lp->ll_li->li_tv, deep, lock);
3830 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003831 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003832 item_lock(&lp->ll_di->di_tv, deep, lock);
3833
3834 return ret;
3835}
3836
3837/*
3838 * Lock or unlock an item. "deep" is nr of levels to go.
3839 */
3840 static void
3841item_lock(tv, deep, lock)
3842 typval_T *tv;
3843 int deep;
3844 int lock;
3845{
3846 static int recurse = 0;
3847 list_T *l;
3848 listitem_T *li;
3849 dict_T *d;
3850 hashitem_T *hi;
3851 int todo;
3852
3853 if (recurse >= DICT_MAXNEST)
3854 {
3855 EMSG(_("E743: variable nested too deep for (un)lock"));
3856 return;
3857 }
3858 if (deep == 0)
3859 return;
3860 ++recurse;
3861
3862 /* lock/unlock the item itself */
3863 if (lock)
3864 tv->v_lock |= VAR_LOCKED;
3865 else
3866 tv->v_lock &= ~VAR_LOCKED;
3867
3868 switch (tv->v_type)
3869 {
3870 case VAR_LIST:
3871 if ((l = tv->vval.v_list) != NULL)
3872 {
3873 if (lock)
3874 l->lv_lock |= VAR_LOCKED;
3875 else
3876 l->lv_lock &= ~VAR_LOCKED;
3877 if (deep < 0 || deep > 1)
3878 /* recursive: lock/unlock the items the List contains */
3879 for (li = l->lv_first; li != NULL; li = li->li_next)
3880 item_lock(&li->li_tv, deep - 1, lock);
3881 }
3882 break;
3883 case VAR_DICT:
3884 if ((d = tv->vval.v_dict) != NULL)
3885 {
3886 if (lock)
3887 d->dv_lock |= VAR_LOCKED;
3888 else
3889 d->dv_lock &= ~VAR_LOCKED;
3890 if (deep < 0 || deep > 1)
3891 {
3892 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003893 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3895 {
3896 if (!HASHITEM_EMPTY(hi))
3897 {
3898 --todo;
3899 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3900 }
3901 }
3902 }
3903 }
3904 }
3905 --recurse;
3906}
3907
Bram Moolenaara40058a2005-07-11 22:42:07 +00003908/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003909 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3910 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003911 */
3912 static int
3913tv_islocked(tv)
3914 typval_T *tv;
3915{
3916 return (tv->v_lock & VAR_LOCKED)
3917 || (tv->v_type == VAR_LIST
3918 && tv->vval.v_list != NULL
3919 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3920 || (tv->v_type == VAR_DICT
3921 && tv->vval.v_dict != NULL
3922 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3923}
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3926/*
3927 * Delete all "menutrans_" variables.
3928 */
3929 void
3930del_menutrans_vars()
3931{
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003936 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 {
3939 if (!HASHITEM_EMPTY(hi))
3940 {
3941 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3943 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 }
3945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948#endif
3949
3950#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3951
3952/*
3953 * Local string buffer for the next two functions to store a variable name
3954 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3955 * get_user_var_name().
3956 */
3957
3958static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3959
3960static char_u *varnamebuf = NULL;
3961static int varnamebuflen = 0;
3962
3963/*
3964 * Function to concatenate a prefix and a variable name.
3965 */
3966 static char_u *
3967cat_prefix_varname(prefix, name)
3968 int prefix;
3969 char_u *name;
3970{
3971 int len;
3972
3973 len = (int)STRLEN(name) + 3;
3974 if (len > varnamebuflen)
3975 {
3976 vim_free(varnamebuf);
3977 len += 10; /* some additional space */
3978 varnamebuf = alloc(len);
3979 if (varnamebuf == NULL)
3980 {
3981 varnamebuflen = 0;
3982 return NULL;
3983 }
3984 varnamebuflen = len;
3985 }
3986 *varnamebuf = prefix;
3987 varnamebuf[1] = ':';
3988 STRCPY(varnamebuf + 2, name);
3989 return varnamebuf;
3990}
3991
3992/*
3993 * Function given to ExpandGeneric() to obtain the list of user defined
3994 * (global/buffer/window/built-in) variable names.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u *
3997get_user_var_name(xp, idx)
3998 expand_T *xp;
3999 int idx;
4000{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 static long_u gdone;
4002 static long_u bdone;
4003 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 static long_u tdone;
4006#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static int vidx;
4008 static hashitem_T *hi;
4009 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004013 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014#ifdef FEAT_WINDOWS
4015 tdone = 0;
4016#endif
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* Global variables */
4020 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 else
4025 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 while (HASHITEM_EMPTY(hi))
4027 ++hi;
4028 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4029 return cat_prefix_varname('g', hi->hi_key);
4030 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return (char_u *)"b:changedtick";
4049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050
4051 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004052 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004055 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 else
4058 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 while (HASHITEM_EMPTY(hi))
4060 ++hi;
4061 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004063
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064#ifdef FEAT_WINDOWS
4065 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 if (tdone < ht->ht_used)
4068 {
4069 if (tdone++ == 0)
4070 hi = ht->ht_array;
4071 else
4072 ++hi;
4073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('t', hi->hi_key);
4076 }
4077#endif
4078
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 /* v: variables */
4080 if (vidx < VV_LEN)
4081 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 vim_free(varnamebuf);
4084 varnamebuf = NULL;
4085 varnamebuflen = 0;
4086 return NULL;
4087}
4088
4089#endif /* FEAT_CMDL_COMPL */
4090
4091/*
4092 * types for expressions.
4093 */
4094typedef enum
4095{
4096 TYPE_UNKNOWN = 0
4097 , TYPE_EQUAL /* == */
4098 , TYPE_NEQUAL /* != */
4099 , TYPE_GREATER /* > */
4100 , TYPE_GEQUAL /* >= */
4101 , TYPE_SMALLER /* < */
4102 , TYPE_SEQUAL /* <= */
4103 , TYPE_MATCH /* =~ */
4104 , TYPE_NOMATCH /* !~ */
4105} exptype_T;
4106
4107/*
4108 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4111 */
4112
4113/*
4114 * Handle zero level expression.
4115 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004117 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Return OK or FAIL.
4119 */
4120 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 char_u **nextcmd;
4125 int evaluate;
4126{
4127 int ret;
4128 char_u *p;
4129
4130 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (ret == FAIL || !ends_excmd(*p))
4133 {
4134 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * Report the invalid expression unless the expression evaluation has
4138 * been cancelled due to an aborting error, an interrupt, or an
4139 * exception.
4140 */
4141 if (!aborting())
4142 EMSG2(_(e_invexpr2), arg);
4143 ret = FAIL;
4144 }
4145 if (nextcmd != NULL)
4146 *nextcmd = check_nextcmd(p);
4147
4148 return ret;
4149}
4150
4151/*
4152 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004153 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004158 * Note: "rettv.v_lock" is not set.
4159 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
4168 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 if ((*arg)[0] == '?')
4178 {
4179 result = FALSE;
4180 if (evaluate)
4181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 int error = FALSE;
4183
4184 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190
4191 /*
4192 * Get the second variable.
4193 */
4194 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197
4198 /*
4199 * Check for the ":".
4200 */
4201 if ((*arg)[0] != ':')
4202 {
4203 EMSG(_("E109: Missing ':' after '?'"));
4204 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207 }
4208
4209 /*
4210 * Get the third variable.
4211 */
4212 *arg = skipwhite(*arg + 1);
4213 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4214 {
4215 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223 return OK;
4224}
4225
4226/*
4227 * Handle first level expression:
4228 * expr2 || expr2 || expr2 logical OR
4229 *
4230 * "arg" must point to the first non-white of the expression.
4231 * "arg" is advanced to the next non-white after the recognized expression.
4232 *
4233 * Return OK or FAIL.
4234 */
4235 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 int evaluate;
4240{
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 long result;
4243 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * Get the first variable.
4248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
4251
4252 /*
4253 * Repeat until there is no following "||".
4254 */
4255 first = TRUE;
4256 result = FALSE;
4257 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4258 {
4259 if (evaluate && first)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 first = FALSE;
4267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 2);
4273 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4274 return FAIL;
4275
4276 /*
4277 * Compute the result.
4278 */
4279 if (evaluate && !result)
4280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (error)
4285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_NUMBER;
4290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * Handle second level expression:
4299 * expr3 && expr3 && expr3 logical AND
4300 *
4301 * "arg" must point to the first non-white of the expression.
4302 * "arg" is advanced to the next non-white after the recognized expression.
4303 *
4304 * Return OK or FAIL.
4305 */
4306 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 int evaluate;
4311{
Bram Moolenaar33570922005-01-25 22:26:29 +00004312 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 long result;
4314 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 /*
4318 * Get the first variable.
4319 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 return FAIL;
4322
4323 /*
4324 * Repeat until there is no following "&&".
4325 */
4326 first = TRUE;
4327 result = TRUE;
4328 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4329 {
4330 if (evaluate && first)
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (error)
4336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 first = FALSE;
4338 }
4339
4340 /*
4341 * Get the second variable.
4342 */
4343 *arg = skipwhite(*arg + 2);
4344 if (eval4(arg, &var2, evaluate && result) == FAIL)
4345 return FAIL;
4346
4347 /*
4348 * Compute the result.
4349 */
4350 if (evaluate && result)
4351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (error)
4356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358 if (evaluate)
4359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 rettv->v_type = VAR_NUMBER;
4361 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 }
4364
4365 return OK;
4366}
4367
4368/*
4369 * Handle third level expression:
4370 * var1 == var2
4371 * var1 =~ var2
4372 * var1 != var2
4373 * var1 !~ var2
4374 * var1 > var2
4375 * var1 >= var2
4376 * var1 < var2
4377 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 * var1 is var2
4379 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 *
4381 * "arg" must point to the first non-white of the expression.
4382 * "arg" is advanced to the next non-white after the recognized expression.
4383 *
4384 * Return OK or FAIL.
4385 */
4386 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int evaluate;
4391{
Bram Moolenaar33570922005-01-25 22:26:29 +00004392 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 char_u *p;
4394 int i;
4395 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int len = 2;
4398 long n1, n2;
4399 char_u *s1, *s2;
4400 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4401 regmatch_T regmatch;
4402 int ic;
4403 char_u *save_cpo;
4404
4405 /*
4406 * Get the first variable.
4407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
4410
4411 p = *arg;
4412 switch (p[0])
4413 {
4414 case '=': if (p[1] == '=')
4415 type = TYPE_EQUAL;
4416 else if (p[1] == '~')
4417 type = TYPE_MATCH;
4418 break;
4419 case '!': if (p[1] == '=')
4420 type = TYPE_NEQUAL;
4421 else if (p[1] == '~')
4422 type = TYPE_NOMATCH;
4423 break;
4424 case '>': if (p[1] != '=')
4425 {
4426 type = TYPE_GREATER;
4427 len = 1;
4428 }
4429 else
4430 type = TYPE_GEQUAL;
4431 break;
4432 case '<': if (p[1] != '=')
4433 {
4434 type = TYPE_SMALLER;
4435 len = 1;
4436 }
4437 else
4438 type = TYPE_SEQUAL;
4439 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 case 'i': if (p[1] == 's')
4441 {
4442 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4443 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004444 i = p[len];
4445 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 {
4447 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4448 type_is = TRUE;
4449 }
4450 }
4451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453
4454 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 */
4457 if (type != TYPE_UNKNOWN)
4458 {
4459 /* extra question mark appended: ignore case */
4460 if (p[len] == '?')
4461 {
4462 ic = TRUE;
4463 ++len;
4464 }
4465 /* extra '#' appended: match case */
4466 else if (p[len] == '#')
4467 {
4468 ic = FALSE;
4469 ++len;
4470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 else
4473 ic = p_ic;
4474
4475 /*
4476 * Get the second variable.
4477 */
4478 *arg = skipwhite(p + len);
4479 if (eval5(arg, &var2, evaluate) == FAIL)
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return FAIL;
4483 }
4484
4485 if (evaluate)
4486 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 if (type_is && rettv->v_type != var2.v_type)
4488 {
4489 /* For "is" a different type always means FALSE, for "notis"
4490 * it means TRUE. */
4491 n1 = (type == TYPE_NEQUAL);
4492 }
4493 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4494 {
4495 if (type_is)
4496 {
4497 n1 = (rettv->v_type == var2.v_type
4498 && rettv->vval.v_list == var2.vval.v_list);
4499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 else if (rettv->v_type != var2.v_type
4503 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4504 {
4505 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004506 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004508 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 clear_tv(rettv);
4510 clear_tv(&var2);
4511 return FAIL;
4512 }
4513 else
4514 {
4515 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004516 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4517 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 if (type == TYPE_NEQUAL)
4519 n1 = !n1;
4520 }
4521 }
4522
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004523 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4524 {
4525 if (type_is)
4526 {
4527 n1 = (rettv->v_type == var2.v_type
4528 && rettv->vval.v_dict == var2.vval.v_dict);
4529 if (type == TYPE_NEQUAL)
4530 n1 = !n1;
4531 }
4532 else if (rettv->v_type != var2.v_type
4533 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4534 {
4535 if (rettv->v_type != var2.v_type)
4536 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4537 else
4538 EMSG(_("E736: Invalid operation for Dictionary"));
4539 clear_tv(rettv);
4540 clear_tv(&var2);
4541 return FAIL;
4542 }
4543 else
4544 {
4545 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004546 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4547 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004548 if (type == TYPE_NEQUAL)
4549 n1 = !n1;
4550 }
4551 }
4552
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4554 {
4555 if (rettv->v_type != var2.v_type
4556 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4557 {
4558 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004559 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004561 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 clear_tv(rettv);
4563 clear_tv(&var2);
4564 return FAIL;
4565 }
4566 else
4567 {
4568 /* Compare two Funcrefs for being equal or unequal. */
4569 if (rettv->vval.v_string == NULL
4570 || var2.vval.v_string == NULL)
4571 n1 = FALSE;
4572 else
4573 n1 = STRCMP(rettv->vval.v_string,
4574 var2.vval.v_string) == 0;
4575 if (type == TYPE_NEQUAL)
4576 n1 = !n1;
4577 }
4578 }
4579
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004580#ifdef FEAT_FLOAT
4581 /*
4582 * If one of the two variables is a float, compare as a float.
4583 * When using "=~" or "!~", always compare as string.
4584 */
4585 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4586 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4587 {
4588 float_T f1, f2;
4589
4590 if (rettv->v_type == VAR_FLOAT)
4591 f1 = rettv->vval.v_float;
4592 else
4593 f1 = get_tv_number(rettv);
4594 if (var2.v_type == VAR_FLOAT)
4595 f2 = var2.vval.v_float;
4596 else
4597 f2 = get_tv_number(&var2);
4598 n1 = FALSE;
4599 switch (type)
4600 {
4601 case TYPE_EQUAL: n1 = (f1 == f2); break;
4602 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4603 case TYPE_GREATER: n1 = (f1 > f2); break;
4604 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4605 case TYPE_SMALLER: n1 = (f1 < f2); break;
4606 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4607 case TYPE_UNKNOWN:
4608 case TYPE_MATCH:
4609 case TYPE_NOMATCH: break; /* avoid gcc warning */
4610 }
4611 }
4612#endif
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 /*
4615 * If one of the two variables is a number, compare as a number.
4616 * When using "=~" or "!~", always compare as string.
4617 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004618 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 n1 = get_tv_number(rettv);
4622 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 switch (type)
4624 {
4625 case TYPE_EQUAL: n1 = (n1 == n2); break;
4626 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4627 case TYPE_GREATER: n1 = (n1 > n2); break;
4628 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4629 case TYPE_SMALLER: n1 = (n1 < n2); break;
4630 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4631 case TYPE_UNKNOWN:
4632 case TYPE_MATCH:
4633 case TYPE_NOMATCH: break; /* avoid gcc warning */
4634 }
4635 }
4636 else
4637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 s1 = get_tv_string_buf(rettv, buf1);
4639 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4642 else
4643 i = 0;
4644 n1 = FALSE;
4645 switch (type)
4646 {
4647 case TYPE_EQUAL: n1 = (i == 0); break;
4648 case TYPE_NEQUAL: n1 = (i != 0); break;
4649 case TYPE_GREATER: n1 = (i > 0); break;
4650 case TYPE_GEQUAL: n1 = (i >= 0); break;
4651 case TYPE_SMALLER: n1 = (i < 0); break;
4652 case TYPE_SEQUAL: n1 = (i <= 0); break;
4653
4654 case TYPE_MATCH:
4655 case TYPE_NOMATCH:
4656 /* avoid 'l' flag in 'cpoptions' */
4657 save_cpo = p_cpo;
4658 p_cpo = (char_u *)"";
4659 regmatch.regprog = vim_regcomp(s2,
4660 RE_MAGIC + RE_STRING);
4661 regmatch.rm_ic = ic;
4662 if (regmatch.regprog != NULL)
4663 {
4664 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004665 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (type == TYPE_NOMATCH)
4667 n1 = !n1;
4668 }
4669 p_cpo = save_cpo;
4670 break;
4671
4672 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4673 }
4674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 rettv->v_type = VAR_NUMBER;
4678 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 }
4681
4682 return OK;
4683}
4684
4685/*
4686 * Handle fourth level expression:
4687 * + number addition
4688 * - number subtraction
4689 * . string concatenation
4690 *
4691 * "arg" must point to the first non-white of the expression.
4692 * "arg" is advanced to the next non-white after the recognized expression.
4693 *
4694 * Return OK or FAIL.
4695 */
4696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 int evaluate;
4701{
Bram Moolenaar33570922005-01-25 22:26:29 +00004702 typval_T var2;
4703 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 int op;
4705 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706#ifdef FEAT_FLOAT
4707 float_T f1 = 0, f2 = 0;
4708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 char_u *s1, *s2;
4710 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4711 char_u *p;
4712
4713 /*
4714 * Get the first variable.
4715 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return FAIL;
4718
4719 /*
4720 * Repeat computing, until no '+', '-' or '.' is following.
4721 */
4722 for (;;)
4723 {
4724 op = **arg;
4725 if (op != '+' && op != '-' && op != '.')
4726 break;
4727
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 if ((op != '+' || rettv->v_type != VAR_LIST)
4729#ifdef FEAT_FLOAT
4730 && (op == '.' || rettv->v_type != VAR_FLOAT)
4731#endif
4732 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 {
4734 /* For "list + ...", an illegal use of the first operand as
4735 * a number cannot be determined before evaluating the 2nd
4736 * operand: if this is also a list, all is ok.
4737 * For "something . ...", "something - ..." or "non-list + ...",
4738 * we know that the first operand needs to be a string or number
4739 * without evaluating the 2nd operand. So check before to avoid
4740 * side effects after an error. */
4741 if (evaluate && get_tv_string_chk(rettv) == NULL)
4742 {
4743 clear_tv(rettv);
4744 return FAIL;
4745 }
4746 }
4747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 /*
4749 * Get the second variable.
4750 */
4751 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004752 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 return FAIL;
4756 }
4757
4758 if (evaluate)
4759 {
4760 /*
4761 * Compute the result.
4762 */
4763 if (op == '.')
4764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4766 s2 = get_tv_string_buf_chk(&var2, buf2);
4767 if (s2 == NULL) /* type error ? */
4768 {
4769 clear_tv(rettv);
4770 clear_tv(&var2);
4771 return FAIL;
4772 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004773 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 rettv->v_type = VAR_STRING;
4776 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004778 else if (op == '+' && rettv->v_type == VAR_LIST
4779 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004780 {
4781 /* concatenate Lists */
4782 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4783 &var3) == FAIL)
4784 {
4785 clear_tv(rettv);
4786 clear_tv(&var2);
4787 return FAIL;
4788 }
4789 clear_tv(rettv);
4790 *rettv = var3;
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
4793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 int error = FALSE;
4795
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004799 f1 = rettv->vval.v_float;
4800 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 else
4803#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 n1 = get_tv_number_chk(rettv, &error);
4806 if (error)
4807 {
4808 /* This can only happen for "list + non-list". For
4809 * "non-list + ..." or "something - ...", we returned
4810 * before evaluating the 2nd operand. */
4811 clear_tv(rettv);
4812 return FAIL;
4813 }
4814#ifdef FEAT_FLOAT
4815 if (var2.v_type == VAR_FLOAT)
4816 f1 = n1;
4817#endif
4818 }
4819#ifdef FEAT_FLOAT
4820 if (var2.v_type == VAR_FLOAT)
4821 {
4822 f2 = var2.vval.v_float;
4823 n2 = 0;
4824 }
4825 else
4826#endif
4827 {
4828 n2 = get_tv_number_chk(&var2, &error);
4829 if (error)
4830 {
4831 clear_tv(rettv);
4832 clear_tv(&var2);
4833 return FAIL;
4834 }
4835#ifdef FEAT_FLOAT
4836 if (rettv->v_type == VAR_FLOAT)
4837 f2 = n2;
4838#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841
4842#ifdef FEAT_FLOAT
4843 /* If there is a float on either side the result is a float. */
4844 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4845 {
4846 if (op == '+')
4847 f1 = f1 + f2;
4848 else
4849 f1 = f1 - f2;
4850 rettv->v_type = VAR_FLOAT;
4851 rettv->vval.v_float = f1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004854#endif
4855 {
4856 if (op == '+')
4857 n1 = n1 + n2;
4858 else
4859 n1 = n1 - n2;
4860 rettv->v_type = VAR_NUMBER;
4861 rettv->vval.v_number = n1;
4862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
4867 return OK;
4868}
4869
4870/*
4871 * Handle fifth level expression:
4872 * * number multiplication
4873 * / number division
4874 * % number modulo
4875 *
4876 * "arg" must point to the first non-white of the expression.
4877 * "arg" is advanced to the next non-white after the recognized expression.
4878 *
4879 * Return OK or FAIL.
4880 */
4881 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004882eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004886 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
Bram Moolenaar33570922005-01-25 22:26:29 +00004888 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 int op;
4890 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 int use_float = FALSE;
4893 float_T f1 = 0, f2;
4894#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Get the first variable.
4899 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004900 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 return FAIL;
4902
4903 /*
4904 * Repeat computing, until no '*', '/' or '%' is following.
4905 */
4906 for (;;)
4907 {
4908 op = **arg;
4909 if (op != '*' && op != '/' && op != '%')
4910 break;
4911
4912 if (evaluate)
4913 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#ifdef FEAT_FLOAT
4915 if (rettv->v_type == VAR_FLOAT)
4916 {
4917 f1 = rettv->vval.v_float;
4918 use_float = TRUE;
4919 n1 = 0;
4920 }
4921 else
4922#endif
4923 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 if (error)
4926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 n1 = 0;
4930
4931 /*
4932 * Get the second variable.
4933 */
4934 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004935 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 return FAIL;
4937
4938 if (evaluate)
4939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940#ifdef FEAT_FLOAT
4941 if (var2.v_type == VAR_FLOAT)
4942 {
4943 if (!use_float)
4944 {
4945 f1 = n1;
4946 use_float = TRUE;
4947 }
4948 f2 = var2.vval.v_float;
4949 n2 = 0;
4950 }
4951 else
4952#endif
4953 {
4954 n2 = get_tv_number_chk(&var2, &error);
4955 clear_tv(&var2);
4956 if (error)
4957 return FAIL;
4958#ifdef FEAT_FLOAT
4959 if (use_float)
4960 f2 = n2;
4961#endif
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 /*
4965 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968#ifdef FEAT_FLOAT
4969 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 if (op == '*')
4972 f1 = f1 * f2;
4973 else if (op == '/')
4974 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975# ifdef VMS
4976 /* VMS crashes on divide by zero, work around it */
4977 if (f2 == 0.0)
4978 {
4979 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004982 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004984 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004985 }
4986 else
4987 f1 = f1 / f2;
4988# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 /* We rely on the floating point library to handle divide
4990 * by zero to result in "inf" and not a crash. */
4991 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004996 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 return FAIL;
4998 }
4999 rettv->v_type = VAR_FLOAT;
5000 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 if (op == '*')
5006 n1 = n1 * n2;
5007 else if (op == '/')
5008 {
5009 if (n2 == 0) /* give an error message? */
5010 {
5011 if (n1 == 0)
5012 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5013 else if (n1 < 0)
5014 n1 = -0x7fffffffL;
5015 else
5016 n1 = 0x7fffffffL;
5017 }
5018 else
5019 n1 = n1 / n2;
5020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023 if (n2 == 0) /* give an error message? */
5024 n1 = 0;
5025 else
5026 n1 = n1 % n2;
5027 }
5028 rettv->v_type = VAR_NUMBER;
5029 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 }
5033
5034 return OK;
5035}
5036
5037/*
5038 * Handle sixth level expression:
5039 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005040 * "string" string constant
5041 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * &option-name option value
5043 * @r register contents
5044 * identifier variable value
5045 * function() function call
5046 * $VAR environment variable
5047 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005048 * [expr, expr] List
5049 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 *
5051 * Also handle:
5052 * ! in front logical NOT
5053 * - in front unary minus
5054 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * trailing [] subscript in String or List
5056 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 *
5058 * "arg" must point to the first non-white of the expression.
5059 * "arg" is advanced to the next non-white after the recognized expression.
5060 *
5061 * Return OK or FAIL.
5062 */
5063 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005064eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005068 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 long n;
5071 int len;
5072 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 char_u *start_leader, *end_leader;
5074 int ret = OK;
5075 char_u *alias;
5076
5077 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 /*
5084 * Skip '!' and '-' characters. They are handled later.
5085 */
5086 start_leader = *arg;
5087 while (**arg == '!' || **arg == '-' || **arg == '+')
5088 *arg = skipwhite(*arg + 1);
5089 end_leader = *arg;
5090
5091 switch (**arg)
5092 {
5093 /*
5094 * Number constant.
5095 */
5096 case '0':
5097 case '1':
5098 case '2':
5099 case '3':
5100 case '4':
5101 case '5':
5102 case '6':
5103 case '7':
5104 case '8':
5105 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 {
5107#ifdef FEAT_FLOAT
5108 char_u *p = skipdigits(*arg + 1);
5109 int get_float = FALSE;
5110
5111 /* We accept a float when the format matches
5112 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005113 * strict to avoid backwards compatibility problems.
5114 * Don't look for a float after the "." operator, so that
5115 * ":let vers = 1.2.3" doesn't fail. */
5116 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 get_float = TRUE;
5119 p = skipdigits(p + 2);
5120 if (*p == 'e' || *p == 'E')
5121 {
5122 ++p;
5123 if (*p == '-' || *p == '+')
5124 ++p;
5125 if (!vim_isdigit(*p))
5126 get_float = FALSE;
5127 else
5128 p = skipdigits(p + 1);
5129 }
5130 if (ASCII_ISALPHA(*p) || *p == '.')
5131 get_float = FALSE;
5132 }
5133 if (get_float)
5134 {
5135 float_T f;
5136
5137 *arg += string2float(*arg, &f);
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_FLOAT;
5141 rettv->vval.v_float = f;
5142 }
5143 }
5144 else
5145#endif
5146 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005147 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005148 *arg += len;
5149 if (evaluate)
5150 {
5151 rettv->v_type = VAR_NUMBER;
5152 rettv->vval.v_number = n;
5153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
5159 * String constant: "string".
5160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 break;
5169
5170 /*
5171 * List: [expr, expr]
5172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 * Dictionary: {key: val, key: val}
5178 */
5179 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5180 break;
5181
5182 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005185 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
5189 * Environment variable: $VAR.
5190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Register contents: @r.
5196 */
5197 case '@': ++*arg;
5198 if (evaluate)
5199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005201 rettv->vval.v_string = get_reg_contents(**arg,
5202 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 if (**arg != NUL)
5205 ++*arg;
5206 break;
5207
5208 /*
5209 * nested expression: (expression).
5210 */
5211 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (**arg == ')')
5214 ++*arg;
5215 else if (ret == OK)
5216 {
5217 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 ret = FAIL;
5220 }
5221 break;
5222
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 break;
5225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226
5227 if (ret == NOTDONE)
5228 {
5229 /*
5230 * Must be a variable or function name.
5231 * Can also be a curly-braces kind of name: {expr}.
5232 */
5233 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 if (alias != NULL)
5236 s = alias;
5237
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 ret = FAIL;
5240 else
5241 {
5242 if (**arg == '(') /* recursive! */
5243 {
5244 /* If "s" is the name of a variable of type VAR_FUNC
5245 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005246 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247
5248 /* Invoke the function. */
5249 ret = get_func_tv(s, len, rettv, arg,
5250 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005251 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005252
5253 /* If evaluate is FALSE rettv->v_type was not set in
5254 * get_func_tv, but it's needed in handle_subscript() to parse
5255 * what follows. So set it here. */
5256 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5257 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005258 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005259 rettv->v_type = VAR_FUNC;
5260 }
5261
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 /* Stop the expression evaluation when immediately
5263 * aborting on error, or when an interrupt occurred or
5264 * an exception was thrown but not caught. */
5265 if (aborting())
5266 {
5267 if (ret == OK)
5268 clear_tv(rettv);
5269 ret = FAIL;
5270 }
5271 }
5272 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005273 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005274 else
5275 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005277 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 }
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 *arg = skipwhite(*arg);
5281
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5283 * expr(expr). */
5284 if (ret == OK)
5285 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287 /*
5288 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5289 */
5290 if (ret == OK && evaluate && end_leader > start_leader)
5291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005293 int val = 0;
5294#ifdef FEAT_FLOAT
5295 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005297 if (rettv->v_type == VAR_FLOAT)
5298 f = rettv->vval.v_float;
5299 else
5300#endif
5301 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 clear_tv(rettv);
5305 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 else
5308 {
5309 while (end_leader > start_leader)
5310 {
5311 --end_leader;
5312 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = !f;
5317 else
5318#endif
5319 val = !val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322 {
5323#ifdef FEAT_FLOAT
5324 if (rettv->v_type == VAR_FLOAT)
5325 f = -f;
5326 else
5327#endif
5328 val = -val;
5329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005331#ifdef FEAT_FLOAT
5332 if (rettv->v_type == VAR_FLOAT)
5333 {
5334 clear_tv(rettv);
5335 rettv->vval.v_float = f;
5336 }
5337 else
5338#endif
5339 {
5340 clear_tv(rettv);
5341 rettv->v_type = VAR_NUMBER;
5342 rettv->vval.v_number = val;
5343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
5347 return ret;
5348}
5349
5350/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005351 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5352 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5354 */
5355 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005356eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361{
5362 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 long len = -1;
5366 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005370 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005372 if (verbose)
5373 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 return FAIL;
5375 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005376#ifdef FEAT_FLOAT
5377 else if (rettv->v_type == VAR_FLOAT)
5378 {
5379 if (verbose)
5380 EMSG(_(e_float_as_string));
5381 return FAIL;
5382 }
5383#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005385 init_tv(&var1);
5386 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * dict.name
5391 */
5392 key = *arg + 1;
5393 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5394 ;
5395 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 /*
5402 * something[idx]
5403 *
5404 * Get the (first) variable from inside the [].
5405 */
5406 *arg = skipwhite(*arg + 1);
5407 if (**arg == ':')
5408 empty1 = TRUE;
5409 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5410 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5412 {
5413 /* not a number or string */
5414 clear_tv(&var1);
5415 return FAIL;
5416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417
5418 /*
5419 * Get the second variable from inside the [:].
5420 */
5421 if (**arg == ':')
5422 {
5423 range = TRUE;
5424 *arg = skipwhite(*arg + 1);
5425 if (**arg == ']')
5426 empty2 = TRUE;
5427 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 if (!empty1)
5430 clear_tv(&var1);
5431 return FAIL;
5432 }
5433 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5434 {
5435 /* not a number or string */
5436 if (!empty1)
5437 clear_tv(&var1);
5438 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 return FAIL;
5440 }
5441 }
5442
5443 /* Check for the ']'. */
5444 if (**arg != ']')
5445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005446 if (verbose)
5447 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 clear_tv(&var1);
5449 if (range)
5450 clear_tv(&var2);
5451 return FAIL;
5452 }
5453 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455
5456 if (evaluate)
5457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 n1 = 0;
5459 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 n1 = get_tv_number(&var1);
5462 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 if (range)
5465 {
5466 if (empty2)
5467 n2 = -1;
5468 else
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 n2 = get_tv_number(&var2);
5471 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 }
5474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 {
5477 case VAR_NUMBER:
5478 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 len = (long)STRLEN(s);
5481 if (range)
5482 {
5483 /* The resulting variable is a substring. If the indexes
5484 * are out of range the result is empty. */
5485 if (n1 < 0)
5486 {
5487 n1 = len + n1;
5488 if (n1 < 0)
5489 n1 = 0;
5490 }
5491 if (n2 < 0)
5492 n2 = len + n2;
5493 else if (n2 >= len)
5494 n2 = len;
5495 if (n1 >= len || n2 < 0 || n1 > n2)
5496 s = NULL;
5497 else
5498 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5499 }
5500 else
5501 {
5502 /* The resulting variable is a string of a single
5503 * character. If the index is too big or negative the
5504 * result is empty. */
5505 if (n1 >= len || n1 < 0)
5506 s = NULL;
5507 else
5508 s = vim_strnsave(s + n1, 1);
5509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 clear_tv(rettv);
5511 rettv->v_type = VAR_STRING;
5512 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 break;
5514
5515 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 if (n1 < 0)
5518 n1 = len + n1;
5519 if (!empty1 && (n1 < 0 || n1 >= len))
5520 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005521 /* For a range we allow invalid values and return an empty
5522 * list. A list index out of range is an error. */
5523 if (!range)
5524 {
5525 if (verbose)
5526 EMSGN(_(e_listidx), n1);
5527 return FAIL;
5528 }
5529 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 if (range)
5532 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 list_T *l;
5534 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535
5536 if (n2 < 0)
5537 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005538 else if (n2 >= len)
5539 n2 = len - 1;
5540 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005541 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 l = list_alloc();
5543 if (l == NULL)
5544 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 n1 <= n2; ++n1)
5547 {
5548 if (list_append_tv(l, &item->li_tv) == FAIL)
5549 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005550 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 return FAIL;
5552 }
5553 item = item->li_next;
5554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 clear_tv(rettv);
5556 rettv->v_type = VAR_LIST;
5557 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005558 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560 else
5561 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005562 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567
5568 case VAR_DICT:
5569 if (range)
5570 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005571 if (verbose)
5572 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 if (len == -1)
5574 clear_tv(&var1);
5575 return FAIL;
5576 }
5577 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579
5580 if (len == -1)
5581 {
5582 key = get_tv_string(&var1);
5583 if (*key == NUL)
5584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005585 if (verbose)
5586 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 clear_tv(&var1);
5588 return FAIL;
5589 }
5590 }
5591
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005592 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005595 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 if (len == -1)
5597 clear_tv(&var1);
5598 if (item == NULL)
5599 return FAIL;
5600
5601 copy_tv(&item->di_tv, &var1);
5602 clear_tv(rettv);
5603 *rettv = var1;
5604 }
5605 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 }
5607 }
5608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 return OK;
5610}
5611
5612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 * Get an option value.
5614 * "arg" points to the '&' or '+' before the option name.
5615 * "arg" is advanced to character after the option name.
5616 * Return OK or FAIL.
5617 */
5618 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 int evaluate;
5623{
5624 char_u *option_end;
5625 long numval;
5626 char_u *stringval;
5627 int opt_type;
5628 int c;
5629 int working = (**arg == '+'); /* has("+option") */
5630 int ret = OK;
5631 int opt_flags;
5632
5633 /*
5634 * Isolate the option name and find its value.
5635 */
5636 option_end = find_option_end(arg, &opt_flags);
5637 if (option_end == NULL)
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 EMSG2(_("E112: Option name missing: %s"), *arg);
5641 return FAIL;
5642 }
5643
5644 if (!evaluate)
5645 {
5646 *arg = option_end;
5647 return OK;
5648 }
5649
5650 c = *option_end;
5651 *option_end = NUL;
5652 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 if (opt_type == -3) /* invalid name */
5656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 EMSG2(_("E113: Unknown option: %s"), *arg);
5659 ret = FAIL;
5660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 if (opt_type == -2) /* hidden string option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_STRING;
5666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else if (opt_type == -1) /* hidden number option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_NUMBER;
5671 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else if (opt_type == 1) /* number option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_NUMBER;
5676 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else /* string option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_STRING;
5681 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 }
5684 else if (working && (opt_type == -2 || opt_type == -1))
5685 ret = FAIL;
5686
5687 *option_end = c; /* put back for error messages */
5688 *arg = option_end;
5689
5690 return ret;
5691}
5692
5693/*
5694 * Allocate a variable for a string constant.
5695 * Return OK or FAIL.
5696 */
5697 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005698get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 int evaluate;
5702{
5703 char_u *p;
5704 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 int extra = 0;
5706
5707 /*
5708 * Find the end of the string, skipping backslashed characters.
5709 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 if (*p == '\\' && p[1] != NUL)
5713 {
5714 ++p;
5715 /* A "\<x>" form occupies at least 4 characters, and produces up
5716 * to 6 characters: reserve space for 2 extra */
5717 if (*p == '<')
5718 extra += 2;
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721
5722 if (*p != '"')
5723 {
5724 EMSG2(_("E114: Missing quote: %s"), *arg);
5725 return FAIL;
5726 }
5727
5728 /* If only parsing, set *arg and return here */
5729 if (!evaluate)
5730 {
5731 *arg = p + 1;
5732 return OK;
5733 }
5734
5735 /*
5736 * Copy the string into allocated memory, handling backslashed
5737 * characters.
5738 */
5739 name = alloc((unsigned)(p - *arg + extra));
5740 if (name == NULL)
5741 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 rettv->v_type = VAR_STRING;
5743 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 if (*p == '\\')
5748 {
5749 switch (*++p)
5750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 case 'b': *name++ = BS; ++p; break;
5752 case 'e': *name++ = ESC; ++p; break;
5753 case 'f': *name++ = FF; ++p; break;
5754 case 'n': *name++ = NL; ++p; break;
5755 case 'r': *name++ = CAR; ++p; break;
5756 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758 case 'X': /* hex: "\x1", "\x12" */
5759 case 'x':
5760 case 'u': /* Unicode: "\u0023" */
5761 case 'U':
5762 if (vim_isxdigit(p[1]))
5763 {
5764 int n, nr;
5765 int c = toupper(*p);
5766
5767 if (c == 'X')
5768 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005769 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005771 else
5772 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 nr = 0;
5774 while (--n >= 0 && vim_isxdigit(p[1]))
5775 {
5776 ++p;
5777 nr = (nr << 4) + hex2nr(*p);
5778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780#ifdef FEAT_MBYTE
5781 /* For "\u" store the number according to
5782 * 'encoding'. */
5783 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 else
5786#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 break;
5790
5791 /* octal: "\1", "\12", "\123" */
5792 case '0':
5793 case '1':
5794 case '2':
5795 case '3':
5796 case '4':
5797 case '5':
5798 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 case '7': *name = *p++ - '0';
5800 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 *name = (*name << 3) + *p++ - '0';
5803 if (*p >= '0' && *p <= '7')
5804 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 break;
5808
5809 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 if (extra != 0)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816 /* FALLTHROUGH */
5817
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 }
5822 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 *arg = p + 1;
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return OK;
5830}
5831
5832/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 * Return OK or FAIL.
5835 */
5836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005837get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 int evaluate;
5841{
5842 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 int reduce = 0;
5845
5846 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 */
5849 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 break;
5855 ++reduce;
5856 ++p;
5857 }
5858 }
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 return FAIL;
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 if (!evaluate)
5868 {
5869 *arg = p + 1;
5870 return OK;
5871 }
5872
5873 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 */
5876 str = alloc((unsigned)((p - *arg) - reduce));
5877 if (str == NULL)
5878 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 rettv->v_type = VAR_STRING;
5880 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++p;
5889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 *arg = p + 1;
5894
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 return OK;
5896}
5897
5898/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 * Allocate a variable for a List and fill it from "*arg".
5900 * Return OK or FAIL.
5901 */
5902 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 int evaluate;
5907{
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 list_T *l = NULL;
5909 typval_T tv;
5910 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911
5912 if (evaluate)
5913 {
5914 l = list_alloc();
5915 if (l == NULL)
5916 return FAIL;
5917 }
5918
5919 *arg = skipwhite(*arg + 1);
5920 while (**arg != ']' && **arg != NUL)
5921 {
5922 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5923 goto failret;
5924 if (evaluate)
5925 {
5926 item = listitem_alloc();
5927 if (item != NULL)
5928 {
5929 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005930 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 list_append(l, item);
5932 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005933 else
5934 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 }
5936
5937 if (**arg == ']')
5938 break;
5939 if (**arg != ',')
5940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 goto failret;
5943 }
5944 *arg = skipwhite(*arg + 1);
5945 }
5946
5947 if (**arg != ']')
5948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950failret:
5951 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005952 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 return FAIL;
5954 }
5955
5956 *arg = skipwhite(*arg + 1);
5957 if (evaluate)
5958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 rettv->v_type = VAR_LIST;
5960 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 ++l->lv_refcount;
5962 }
5963
5964 return OK;
5965}
5966
5967/*
5968 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005969 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005971 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972list_alloc()
5973{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005974 list_T *l;
5975
5976 l = (list_T *)alloc_clear(sizeof(list_T));
5977 if (l != NULL)
5978 {
5979 /* Prepend the list to the list of lists for garbage collection. */
5980 if (first_list != NULL)
5981 first_list->lv_used_prev = l;
5982 l->lv_used_prev = NULL;
5983 l->lv_used_next = first_list;
5984 first_list = l;
5985 }
5986 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987}
5988
5989/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005990 * Allocate an empty list for a return value.
5991 * Returns OK or FAIL.
5992 */
5993 static int
5994rettv_list_alloc(rettv)
5995 typval_T *rettv;
5996{
5997 list_T *l = list_alloc();
5998
5999 if (l == NULL)
6000 return FAIL;
6001
6002 rettv->vval.v_list = l;
6003 rettv->v_type = VAR_LIST;
6004 ++l->lv_refcount;
6005 return OK;
6006}
6007
6008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Unreference a list: decrement the reference count and free it when it
6010 * becomes zero.
6011 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006012 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006016 if (l != NULL && --l->lv_refcount <= 0)
6017 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018}
6019
6020/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006021 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 * Ignores the reference count.
6023 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006024 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006025list_free(l, recurse)
6026 list_T *l;
6027 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006031 /* Remove the list from the list of lists for garbage collection. */
6032 if (l->lv_used_prev == NULL)
6033 first_list = l->lv_used_next;
6034 else
6035 l->lv_used_prev->lv_used_next = l->lv_used_next;
6036 if (l->lv_used_next != NULL)
6037 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6038
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 /* Remove the item before deleting it. */
6042 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006043 if (recurse || (item->li_tv.v_type != VAR_LIST
6044 && item->li_tv.v_type != VAR_DICT))
6045 clear_tv(&item->li_tv);
6046 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 }
6048 vim_free(l);
6049}
6050
6051/*
6052 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006053 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006055 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056listitem_alloc()
6057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059}
6060
6061/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006062 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006068 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 vim_free(item);
6070}
6071
6072/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073 * Remove a list item from a List and free it. Also clears the value.
6074 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006075 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 list_T *l;
6078 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006080 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081 listitem_free(item);
6082}
6083
6084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 * Get the number of items in a list.
6086 */
6087 static long
6088list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 if (l == NULL)
6092 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006093 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094}
6095
6096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE when two lists have exactly the same values.
6098 */
6099 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 list_T *l1;
6102 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105{
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006108 if (l1 == NULL || l2 == NULL)
6109 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006110 if (l1 == l2)
6111 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006112 if (list_len(l1) != list_len(l2))
6113 return FALSE;
6114
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 for (item1 = l1->lv_first, item2 = l2->lv_first;
6116 item1 != NULL && item2 != NULL;
6117 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 return FALSE;
6120 return item1 == NULL && item2 == NULL;
6121}
6122
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006123#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6124 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125/*
6126 * Return the dictitem that an entry in a hashtable points to.
6127 */
6128 dictitem_T *
6129dict_lookup(hi)
6130 hashitem_T *hi;
6131{
6132 return HI2DI(hi);
6133}
6134#endif
6135
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137 * Return TRUE when two dictionaries have exactly the same key/values.
6138 */
6139 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 dict_T *d1;
6142 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006144 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 hashitem_T *hi;
6147 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006150 if (d1 == NULL || d2 == NULL)
6151 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006152 if (d1 == d2)
6153 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 if (dict_len(d1) != dict_len(d2))
6155 return FALSE;
6156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006157 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 if (!HASHITEM_EMPTY(hi))
6161 {
6162 item2 = dict_find(d2, hi->hi_key, -1);
6163 if (item2 == NULL)
6164 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 return FALSE;
6167 --todo;
6168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169 }
6170 return TRUE;
6171}
6172
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173static int tv_equal_recurse_limit;
6174
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006177 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006178 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006179 */
6180 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 typval_T *tv1;
6183 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 int ic; /* ignore case */
6185 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186{
6187 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006188 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006190 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006192 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 * recursiveness to a limit. We guess they are equal then.
6197 * A fixed limit has the problem of still taking an awful long time.
6198 * Reduce the limit every time running into it. That should work fine for
6199 * deeply linked structures that are not recursively linked and catch
6200 * recursiveness quickly. */
6201 if (!recursive)
6202 tv_equal_recurse_limit = 1000;
6203 if (recursive_cnt >= tv_equal_recurse_limit)
6204 {
6205 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208
6209 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_FUNC:
6224 return (tv1->vval.v_string != NULL
6225 && tv2->vval.v_string != NULL
6226 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6227
6228 case VAR_NUMBER:
6229 return tv1->vval.v_number == tv2->vval.v_number;
6230
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006231#ifdef FEAT_FLOAT
6232 case VAR_FLOAT:
6233 return tv1->vval.v_float == tv2->vval.v_float;
6234#endif
6235
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236 case VAR_STRING:
6237 s1 = get_tv_string_buf(tv1, buf1);
6238 s2 = get_tv_string_buf(tv2, buf2);
6239 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241
6242 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 return TRUE;
6244}
6245
6246/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 * Locate item with index "n" in list "l" and return it.
6248 * A negative index is counted from the end; -1 is the last item.
6249 * Returns NULL when "n" is out of range.
6250 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 long n;
6255{
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 long idx;
6258
6259 if (l == NULL)
6260 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006261
6262 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264 n = l->lv_len + n;
6265
6266 /* Check for index out of range. */
6267 if (n < 0 || n >= l->lv_len)
6268 return NULL;
6269
6270 /* When there is a cached index may start search from there. */
6271 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273 if (n < l->lv_idx / 2)
6274 {
6275 /* closest to the start of the list */
6276 item = l->lv_first;
6277 idx = 0;
6278 }
6279 else if (n > (l->lv_idx + l->lv_len) / 2)
6280 {
6281 /* closest to the end of the list */
6282 item = l->lv_last;
6283 idx = l->lv_len - 1;
6284 }
6285 else
6286 {
6287 /* closest to the cached index */
6288 item = l->lv_idx_item;
6289 idx = l->lv_idx;
6290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 }
6292 else
6293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294 if (n < l->lv_len / 2)
6295 {
6296 /* closest to the start of the list */
6297 item = l->lv_first;
6298 idx = 0;
6299 }
6300 else
6301 {
6302 /* closest to the end of the list */
6303 item = l->lv_last;
6304 idx = l->lv_len - 1;
6305 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307
6308 while (n > idx)
6309 {
6310 /* search forward */
6311 item = item->li_next;
6312 ++idx;
6313 }
6314 while (n < idx)
6315 {
6316 /* search backward */
6317 item = item->li_prev;
6318 --idx;
6319 }
6320
6321 /* cache the used index */
6322 l->lv_idx = idx;
6323 l->lv_idx_item = item;
6324
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 return item;
6326}
6327
6328/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006329 * Get list item "l[idx]" as a number.
6330 */
6331 static long
6332list_find_nr(l, idx, errorp)
6333 list_T *l;
6334 long idx;
6335 int *errorp; /* set to TRUE when something wrong */
6336{
6337 listitem_T *li;
6338
6339 li = list_find(l, idx);
6340 if (li == NULL)
6341 {
6342 if (errorp != NULL)
6343 *errorp = TRUE;
6344 return -1L;
6345 }
6346 return get_tv_number_chk(&li->li_tv, errorp);
6347}
6348
6349/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006350 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6351 */
6352 char_u *
6353list_find_str(l, idx)
6354 list_T *l;
6355 long idx;
6356{
6357 listitem_T *li;
6358
6359 li = list_find(l, idx - 1);
6360 if (li == NULL)
6361 {
6362 EMSGN(_(e_listidx), idx);
6363 return NULL;
6364 }
6365 return get_tv_string(&li->li_tv);
6366}
6367
6368/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006369 * Locate "item" list "l" and return its index.
6370 * Returns -1 when "item" is not in the list.
6371 */
6372 static long
6373list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *l;
6375 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376{
6377 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379
6380 if (l == NULL)
6381 return -1;
6382 idx = 0;
6383 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6384 ++idx;
6385 if (li == NULL)
6386 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006387 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006388}
6389
6390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 * Append item "item" to the end of list "l".
6392 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 list_T *l;
6396 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
6398 if (l->lv_last == NULL)
6399 {
6400 /* empty list */
6401 l->lv_first = item;
6402 l->lv_last = item;
6403 item->li_prev = NULL;
6404 }
6405 else
6406 {
6407 l->lv_last->li_next = item;
6408 item->li_prev = l->lv_last;
6409 l->lv_last = item;
6410 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 item->li_next = NULL;
6413}
6414
6415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006419 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 list_T *l;
6422 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 copy_tv(tv, &li->li_tv);
6429 list_append(l, li);
6430 return OK;
6431}
6432
6433/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006434 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435 * Return FAIL when out of memory.
6436 */
6437 int
6438list_append_dict(list, dict)
6439 list_T *list;
6440 dict_T *dict;
6441{
6442 listitem_T *li = listitem_alloc();
6443
6444 if (li == NULL)
6445 return FAIL;
6446 li->li_tv.v_type = VAR_DICT;
6447 li->li_tv.v_lock = 0;
6448 li->li_tv.vval.v_dict = dict;
6449 list_append(list, li);
6450 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 return OK;
6452}
6453
6454/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006455 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006456 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457 * Returns FAIL when out of memory.
6458 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006459 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006460list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 list_T *l;
6462 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006463 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464{
6465 listitem_T *li = listitem_alloc();
6466
6467 if (li == NULL)
6468 return FAIL;
6469 list_append(l, li);
6470 li->li_tv.v_type = VAR_STRING;
6471 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006472 if (str == NULL)
6473 li->li_tv.vval.v_string = NULL;
6474 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006475 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006476 return FAIL;
6477 return OK;
6478}
6479
6480/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006481 * Append "n" to list "l".
6482 * Returns FAIL when out of memory.
6483 */
6484 static int
6485list_append_number(l, n)
6486 list_T *l;
6487 varnumber_T n;
6488{
6489 listitem_T *li;
6490
6491 li = listitem_alloc();
6492 if (li == NULL)
6493 return FAIL;
6494 li->li_tv.v_type = VAR_NUMBER;
6495 li->li_tv.v_lock = 0;
6496 li->li_tv.vval.v_number = n;
6497 list_append(l, li);
6498 return OK;
6499}
6500
6501/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 * If "item" is NULL append at the end.
6504 * Return FAIL when out of memory.
6505 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006506 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 list_T *l;
6509 typval_T *tv;
6510 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511{
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513
6514 if (ni == NULL)
6515 return FAIL;
6516 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006517 list_insert(l, ni, item);
6518 return OK;
6519}
6520
6521 void
6522list_insert(l, ni, item)
6523 list_T *l;
6524 listitem_T *ni;
6525 listitem_T *item;
6526{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 if (item == NULL)
6528 /* Append new item at end of list. */
6529 list_append(l, ni);
6530 else
6531 {
6532 /* Insert new item before existing item. */
6533 ni->li_prev = item->li_prev;
6534 ni->li_next = item;
6535 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 ++l->lv_idx;
6539 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 l->lv_idx_item = NULL;
6544 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006546 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548}
6549
6550/*
6551 * Extend "l1" with "l2".
6552 * If "bef" is NULL append at the end, otherwise insert before this item.
6553 * Returns FAIL when out of memory.
6554 */
6555 static int
6556list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 list_T *l1;
6558 list_T *l2;
6559 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006562 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006564 /* We also quit the loop when we have inserted the original item count of
6565 * the list, avoid a hang when we extend a list with itself. */
6566 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6568 return FAIL;
6569 return OK;
6570}
6571
6572/*
6573 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6574 * Return FAIL when out of memory.
6575 */
6576 static int
6577list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *l1;
6579 list_T *l2;
6580 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581{
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006584 if (l1 == NULL || l2 == NULL)
6585 return FAIL;
6586
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 if (l == NULL)
6590 return FAIL;
6591 tv->v_type = VAR_LIST;
6592 tv->vval.v_list = l;
6593
6594 /* append all items from the second list */
6595 return list_extend(l, l2, NULL);
6596}
6597
6598/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006599 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 * Returns NULL when out of memory.
6603 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609{
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 list_T *copy;
6611 listitem_T *item;
6612 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613
6614 if (orig == NULL)
6615 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616
6617 copy = list_alloc();
6618 if (copy != NULL)
6619 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 if (copyID != 0)
6621 {
6622 /* Do this before adding the items, because one of the items may
6623 * refer back to this list. */
6624 orig->lv_copyID = copyID;
6625 orig->lv_copylist = copy;
6626 }
6627 for (item = orig->lv_first; item != NULL && !got_int;
6628 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 {
6630 ni = listitem_alloc();
6631 if (ni == NULL)
6632 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006633 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 {
6635 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6636 {
6637 vim_free(ni);
6638 break;
6639 }
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 list_append(copy, ni);
6644 }
6645 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006646 if (item != NULL)
6647 {
6648 list_unref(copy);
6649 copy = NULL;
6650 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 }
6652
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 return copy;
6654}
6655
6656/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006658 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006659 * This used to be called list_remove, but that conflicts with a Sun header
6660 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006662 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006663vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 list_T *l;
6665 listitem_T *item;
6666 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667{
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670 /* notify watchers */
6671 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006673 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674 list_fix_watch(l, ip);
6675 if (ip == item2)
6676 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678
6679 if (item2->li_next == NULL)
6680 l->lv_last = item->li_prev;
6681 else
6682 item2->li_next->li_prev = item->li_prev;
6683 if (item->li_prev == NULL)
6684 l->lv_first = item2->li_next;
6685 else
6686 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006687 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688}
6689
6690/*
6691 * Return an allocated string with the string representation of a list.
6692 * May return NULL.
6693 */
6694 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698{
6699 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700
6701 if (tv->vval.v_list == NULL)
6702 return NULL;
6703 ga_init2(&ga, (int)sizeof(char), 80);
6704 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006705 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006706 {
6707 vim_free(ga.ga_data);
6708 return NULL;
6709 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710 ga_append(&ga, ']');
6711 ga_append(&ga, NUL);
6712 return (char_u *)ga.ga_data;
6713}
6714
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006715typedef struct join_S {
6716 char_u *s;
6717 char_u *tofree;
6718} join_T;
6719
6720 static int
6721list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6722 garray_T *gap; /* to store the result in */
6723 list_T *l;
6724 char_u *sep;
6725 int echo_style;
6726 int copyID;
6727 garray_T *join_gap; /* to keep each list item string */
6728{
6729 int i;
6730 join_T *p;
6731 int len;
6732 int sumlen = 0;
6733 int first = TRUE;
6734 char_u *tofree;
6735 char_u numbuf[NUMBUFLEN];
6736 listitem_T *item;
6737 char_u *s;
6738
6739 /* Stringify each item in the list. */
6740 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6741 {
6742 if (echo_style)
6743 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6744 else
6745 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6746 if (s == NULL)
6747 return FAIL;
6748
6749 len = (int)STRLEN(s);
6750 sumlen += len;
6751
Bram Moolenaarcde88542015-08-11 19:14:00 +02006752 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6754 if (tofree != NULL || s != numbuf)
6755 {
6756 p->s = s;
6757 p->tofree = tofree;
6758 }
6759 else
6760 {
6761 p->s = vim_strnsave(s, len);
6762 p->tofree = p->s;
6763 }
6764
6765 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006766 if (did_echo_string_emsg) /* recursion error, bail out */
6767 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 }
6769
6770 /* Allocate result buffer with its total size, avoid re-allocation and
6771 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6772 if (join_gap->ga_len >= 2)
6773 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6774 if (ga_grow(gap, sumlen + 2) == FAIL)
6775 return FAIL;
6776
6777 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6778 {
6779 if (first)
6780 first = FALSE;
6781 else
6782 ga_concat(gap, sep);
6783 p = ((join_T *)join_gap->ga_data) + i;
6784
6785 if (p->s != NULL)
6786 ga_concat(gap, p->s);
6787 line_breakcheck();
6788 }
6789
6790 return OK;
6791}
6792
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006793/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006795 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006796 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006798 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006799list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006803 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806 garray_T join_ga;
6807 int retval;
6808 join_T *p;
6809 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810
Bram Moolenaard39a7512015-04-16 22:51:22 +02006811 if (l->lv_len < 1)
6812 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006813 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6814 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6815
6816 /* Dispose each item in join_ga. */
6817 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006818 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 p = (join_T *)join_ga.ga_data;
6820 for (i = 0; i < join_ga.ga_len; ++i)
6821 {
6822 vim_free(p->tofree);
6823 ++p;
6824 }
6825 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006827
6828 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829}
6830
6831/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 * Garbage collection for lists and dictionaries.
6833 *
6834 * We use reference counts to be able to free most items right away when they
6835 * are no longer used. But for composite items it's possible that it becomes
6836 * unused while the reference count is > 0: When there is a recursive
6837 * reference. Example:
6838 * :let l = [1, 2, 3]
6839 * :let d = {9: l}
6840 * :let l[1] = d
6841 *
6842 * Since this is quite unusual we handle this with garbage collection: every
6843 * once in a while find out which lists and dicts are not referenced from any
6844 * variable.
6845 *
6846 * Here is a good reference text about garbage collection (refers to Python
6847 * but it applies to all reference-counting mechanisms):
6848 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850
6851/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 * Do garbage collection for lists and dicts.
6853 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006854 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 int
6856garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006857{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006859 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 buf_T *buf;
6861 win_T *wp;
6862 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006863 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006864 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006866#ifdef FEAT_WINDOWS
6867 tabpage_T *tp;
6868#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006870 /* Only do this once. */
6871 want_garbage_collect = FALSE;
6872 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006873 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006874
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 /* We advance by two because we add one for items referenced through
6876 * previous_funccal. */
6877 current_copyID += COPYID_INC;
6878 copyID = current_copyID;
6879
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880 /*
6881 * 1. Go through all accessible variables and mark all lists and dicts
6882 * with copyID.
6883 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884
6885 /* Don't free variables in the previous_funccal list unless they are only
6886 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006887 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6889 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6891 NULL);
6892 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6893 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 }
6895
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 /* script-local variables */
6897 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* buffer-local variables */
6901 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6903 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
6905 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006906 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6908 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006909#ifdef FEAT_AUTOCMD
6910 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6912 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006913#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#ifdef FEAT_WINDOWS
6916 /* tabpage-local variables */
6917 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6919 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920#endif
6921
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924
6925 /* function-local variables */
6926 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6927 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6929 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 }
6931
Bram Moolenaard812df62008-11-09 12:46:09 +00006932 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006934
Bram Moolenaar1dced572012-04-05 16:54:08 +02006935#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006937#endif
6938
Bram Moolenaardb913952012-06-29 12:54:53 +02006939#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006941#endif
6942
6943#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006945#endif
6946
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 /*
6950 * 2. Free lists and dictionaries that are not referenced.
6951 */
6952 did_free = free_unref_items(copyID);
6953
6954 /*
6955 * 3. Check if any funccal can be freed now.
6956 */
6957 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 if (can_free_funccal(*pfc, copyID))
6960 {
6961 fc = *pfc;
6962 *pfc = fc->caller;
6963 free_funccal(fc, TRUE);
6964 did_free = TRUE;
6965 did_free_funccal = TRUE;
6966 }
6967 else
6968 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006970 if (did_free_funccal)
6971 /* When a funccal was freed some more items might be garbage
6972 * collected, so run again. */
6973 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 else if (p_verbose > 0)
6976 {
6977 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6978 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979
6980 return did_free;
6981}
6982
6983/*
6984 * Free lists and dictionaries that are no longer referenced.
6985 */
6986 static int
6987free_unref_items(copyID)
6988 int copyID;
6989{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 dict_T *dd, *dd_next;
6991 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 int did_free = FALSE;
6993
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 */
6997 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 {
6999 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007002 /* Free the Dictionary and ordinary items it contains, but don't
7003 * recurse into Lists and Dictionaries, they will be in the list
7004 * of dicts or list of lists. */
7005 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007008 dd = dd_next;
7009 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010
7011 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007012 * Go through the list of lists and free items without the copyID.
7013 * But don't free a list that has a watcher (used in a for loop), these
7014 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 */
7016 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007017 {
7018 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007019 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7020 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007022 /* Free the List and ordinary items it contains, but don't recurse
7023 * into Lists and Dictionaries, they will be in the list of dicts
7024 * or list of lists. */
7025 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007028 ll = ll_next;
7029 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 return did_free;
7031}
7032
7033/*
7034 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 * "list_stack" is used to add lists to be marked. Can be NULL.
7036 *
7037 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 int
7040set_ref_in_ht(ht, copyID, list_stack)
7041 hashtab_T *ht;
7042 int copyID;
7043 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044{
7045 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 hashtab_T *cur_ht;
7049 ht_stack_T *ht_stack = NULL;
7050 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 cur_ht = ht;
7053 for (;;)
7054 {
7055 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 /* Mark each item in the hashtab. If the item contains a hashtab
7058 * it is added to ht_stack, if it contains a list it is added to
7059 * list_stack. */
7060 todo = (int)cur_ht->ht_used;
7061 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7062 if (!HASHITEM_EMPTY(hi))
7063 {
7064 --todo;
7065 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7066 &ht_stack, list_stack);
7067 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069
7070 if (ht_stack == NULL)
7071 break;
7072
7073 /* take an item from the stack */
7074 cur_ht = ht_stack->ht;
7075 tempitem = ht_stack;
7076 ht_stack = ht_stack->prev;
7077 free(tempitem);
7078 }
7079
7080 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081}
7082
7083/*
7084 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7086 *
7087 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 int
7090set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091 list_T *l;
7092 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 listitem_T *li;
7096 int abort = FALSE;
7097 list_T *cur_l;
7098 list_stack_T *list_stack = NULL;
7099 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 cur_l = l;
7102 for (;;)
7103 {
7104 if (!abort)
7105 /* Mark each item in the list. If the item contains a hashtab
7106 * it is added to ht_stack, if it contains a list it is added to
7107 * list_stack. */
7108 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7109 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7110 ht_stack, &list_stack);
7111 if (list_stack == NULL)
7112 break;
7113
7114 /* take an item from the stack */
7115 cur_l = list_stack->list;
7116 tempitem = list_stack;
7117 list_stack = list_stack->prev;
7118 free(tempitem);
7119 }
7120
7121 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122}
7123
7124/*
7125 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007126 * "list_stack" is used to add lists to be marked. Can be NULL.
7127 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7128 *
7129 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 int
7132set_ref_in_item(tv, copyID, ht_stack, list_stack)
7133 typval_T *tv;
7134 int copyID;
7135 ht_stack_T **ht_stack;
7136 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007137{
7138 dict_T *dd;
7139 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141
7142 switch (tv->v_type)
7143 {
7144 case VAR_DICT:
7145 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007146 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007148 /* Didn't see this dict yet. */
7149 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 if (ht_stack == NULL)
7151 {
7152 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7153 }
7154 else
7155 {
7156 ht_stack_T *newitem = (ht_stack_T*)malloc(
7157 sizeof(ht_stack_T));
7158 if (newitem == NULL)
7159 abort = TRUE;
7160 else
7161 {
7162 newitem->ht = &dd->dv_hashtab;
7163 newitem->prev = *ht_stack;
7164 *ht_stack = newitem;
7165 }
7166 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169
7170 case VAR_LIST:
7171 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007172 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174 /* Didn't see this list yet. */
7175 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007176 if (list_stack == NULL)
7177 {
7178 abort = set_ref_in_list(ll, copyID, ht_stack);
7179 }
7180 else
7181 {
7182 list_stack_T *newitem = (list_stack_T*)malloc(
7183 sizeof(list_stack_T));
7184 if (newitem == NULL)
7185 abort = TRUE;
7186 else
7187 {
7188 newitem->list = ll;
7189 newitem->prev = *list_stack;
7190 *list_stack = newitem;
7191 }
7192 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007195 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007197}
7198
7199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 * Allocate an empty header for a dictionary.
7201 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007202 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203dict_alloc()
7204{
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007209 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007210 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 if (first_dict != NULL)
7212 first_dict->dv_used_prev = d;
7213 d->dv_used_next = first_dict;
7214 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007215 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007218 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007219 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
7226/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007227 * Allocate an empty dict for a return value.
7228 * Returns OK or FAIL.
7229 */
7230 static int
7231rettv_dict_alloc(rettv)
7232 typval_T *rettv;
7233{
7234 dict_T *d = dict_alloc();
7235
7236 if (d == NULL)
7237 return FAIL;
7238
7239 rettv->vval.v_dict = d;
7240 rettv->v_type = VAR_DICT;
7241 ++d->dv_refcount;
7242 return OK;
7243}
7244
7245
7246/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 * Unreference a Dictionary: decrement the reference count and free it when it
7248 * becomes zero.
7249 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007250 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007254 if (d != NULL && --d->dv_refcount <= 0)
7255 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256}
7257
7258/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007259 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 * Ignores the reference count.
7261 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007262 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007263dict_free(d, recurse)
7264 dict_T *d;
7265 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007269 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271 /* Remove the dict from the list of dicts for garbage collection. */
7272 if (d->dv_used_prev == NULL)
7273 first_dict = d->dv_used_next;
7274 else
7275 d->dv_used_prev->dv_used_next = d->dv_used_next;
7276 if (d->dv_used_next != NULL)
7277 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7278
7279 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 if (!HASHITEM_EMPTY(hi))
7285 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 /* Remove the item before deleting it, just in case there is
7287 * something recursive causing trouble. */
7288 di = HI2DI(hi);
7289 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007290 if (recurse || (di->di_tv.v_type != VAR_LIST
7291 && di->di_tv.v_type != VAR_DICT))
7292 clear_tv(&di->di_tv);
7293 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 --todo;
7295 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 vim_free(d);
7299}
7300
7301/*
7302 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 * The "key" is copied to the new item.
7304 * Note that the value of the item "di_tv" still needs to be initialized!
7305 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007307 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308dictitem_alloc(key)
7309 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310{
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007313 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007317 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320}
7321
7322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Make a copy of a Dictionary item.
7324 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328{
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007331 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7332 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 if (di != NULL)
7334 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007336 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 copy_tv(&org->di_tv, &di->di_tv);
7338 }
7339 return di;
7340}
7341
7342/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 * Remove item "item" from Dictionary "dict" and free it.
7344 */
7345 static void
7346dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 dict_T *dict;
7348 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (HASHITEM_EMPTY(hi))
7354 EMSG2(_(e_intern2), "dictitem_remove()");
7355 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 dictitem_free(item);
7358}
7359
7360/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 * Free a dict item. Also clears the value.
7362 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007363 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007368 if (item->di_flags & DI_FLAGS_ALLOC)
7369 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370}
7371
7372/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7374 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 * Returns NULL when out of memory.
7377 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007379dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007382 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383{
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 dict_T *copy;
7385 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388
7389 if (orig == NULL)
7390 return NULL;
7391
7392 copy = dict_alloc();
7393 if (copy != NULL)
7394 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 if (copyID != 0)
7396 {
7397 orig->dv_copyID = copyID;
7398 orig->dv_copydict = copy;
7399 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007400 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 --todo;
7406
7407 di = dictitem_alloc(hi->hi_key);
7408 if (di == NULL)
7409 break;
7410 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007411 {
7412 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7413 copyID) == FAIL)
7414 {
7415 vim_free(di);
7416 break;
7417 }
7418 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 else
7420 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7421 if (dict_add(copy, di) == FAIL)
7422 {
7423 dictitem_free(di);
7424 break;
7425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007430 if (todo > 0)
7431 {
7432 dict_unref(copy);
7433 copy = NULL;
7434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 }
7436
7437 return copy;
7438}
7439
7440/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007442 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007444 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dict_T *d;
7447 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448{
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450}
7451
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007453 * Add a number or string entry to dictionary "d".
7454 * When "str" is NULL use number "nr", otherwise use "str".
7455 * Returns FAIL when out of memory and when key already exists.
7456 */
7457 int
7458dict_add_nr_str(d, key, nr, str)
7459 dict_T *d;
7460 char *key;
7461 long nr;
7462 char_u *str;
7463{
7464 dictitem_T *item;
7465
7466 item = dictitem_alloc((char_u *)key);
7467 if (item == NULL)
7468 return FAIL;
7469 item->di_tv.v_lock = 0;
7470 if (str == NULL)
7471 {
7472 item->di_tv.v_type = VAR_NUMBER;
7473 item->di_tv.vval.v_number = nr;
7474 }
7475 else
7476 {
7477 item->di_tv.v_type = VAR_STRING;
7478 item->di_tv.vval.v_string = vim_strsave(str);
7479 }
7480 if (dict_add(d, item) == FAIL)
7481 {
7482 dictitem_free(item);
7483 return FAIL;
7484 }
7485 return OK;
7486}
7487
7488/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007489 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007490 * Returns FAIL when out of memory and when key already exists.
7491 */
7492 int
7493dict_add_list(d, key, list)
7494 dict_T *d;
7495 char *key;
7496 list_T *list;
7497{
7498 dictitem_T *item;
7499
7500 item = dictitem_alloc((char_u *)key);
7501 if (item == NULL)
7502 return FAIL;
7503 item->di_tv.v_lock = 0;
7504 item->di_tv.v_type = VAR_LIST;
7505 item->di_tv.vval.v_list = list;
7506 if (dict_add(d, item) == FAIL)
7507 {
7508 dictitem_free(item);
7509 return FAIL;
7510 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007511 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007512 return OK;
7513}
7514
7515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516 * Get the number of items in a Dictionary.
7517 */
7518 static long
7519dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 if (d == NULL)
7523 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007524 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525}
7526
7527/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 * Find item "key[len]" in Dictionary "d".
7529 * If "len" is negative use strlen(key).
7530 * Returns NULL when not found.
7531 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007532 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 char_u *key;
7536 int len;
7537{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538#define AKEYLEN 200
7539 char_u buf[AKEYLEN];
7540 char_u *akey;
7541 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 if (len < 0)
7545 akey = key;
7546 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007547 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 tofree = akey = vim_strnsave(key, len);
7549 if (akey == NULL)
7550 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007551 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 else
7553 {
7554 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007555 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 akey = buf;
7557 }
7558
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 vim_free(tofree);
7561 if (HASHITEM_EMPTY(hi))
7562 return NULL;
7563 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564}
7565
7566/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007567 * Get a string item from a dictionary.
7568 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007569 * Returns NULL if the entry doesn't exist or out of memory.
7570 */
7571 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007572get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007573 dict_T *d;
7574 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007575 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007576{
7577 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579
7580 di = dict_find(d, key, -1);
7581 if (di == NULL)
7582 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007583 s = get_tv_string(&di->di_tv);
7584 if (save && s != NULL)
7585 s = vim_strsave(s);
7586 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007587}
7588
7589/*
7590 * Get a number item from a dictionary.
7591 * Returns 0 if the entry doesn't exist or out of memory.
7592 */
7593 long
7594get_dict_number(d, key)
7595 dict_T *d;
7596 char_u *key;
7597{
7598 dictitem_T *di;
7599
7600 di = dict_find(d, key, -1);
7601 if (di == NULL)
7602 return 0;
7603 return get_tv_number(&di->di_tv);
7604}
7605
7606/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 * Return an allocated string with the string representation of a Dictionary.
7608 * May return NULL.
7609 */
7610 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614{
7615 garray_T ga;
7616 int first = TRUE;
7617 char_u *tofree;
7618 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 return NULL;
7626 ga_init2(&ga, (int)sizeof(char), 80);
7627 ga_append(&ga, '{');
7628
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007629 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007630 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 --todo;
7635
7636 if (first)
7637 first = FALSE;
7638 else
7639 ga_concat(&ga, (char_u *)", ");
7640
7641 tofree = string_quote(hi->hi_key, FALSE);
7642 if (tofree != NULL)
7643 {
7644 ga_concat(&ga, tofree);
7645 vim_free(tofree);
7646 }
7647 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 if (s != NULL)
7650 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007652 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007653 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007654 line_breakcheck();
7655
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007658 if (todo > 0)
7659 {
7660 vim_free(ga.ga_data);
7661 return NULL;
7662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 ga_append(&ga, '}');
7665 ga_append(&ga, NUL);
7666 return (char_u *)ga.ga_data;
7667}
7668
7669/*
7670 * Allocate a variable for a Dictionary and fill it from "*arg".
7671 * Return OK or FAIL. Returns NOTDONE for {expr}.
7672 */
7673 static int
7674get_dict_tv(arg, rettv, evaluate)
7675 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 int evaluate;
7678{
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 dict_T *d = NULL;
7680 typval_T tvkey;
7681 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007682 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686
7687 /*
7688 * First check if it's not a curly-braces thing: {expr}.
7689 * Must do this without evaluating, otherwise a function may be called
7690 * twice. Unfortunately this means we need to call eval1() twice for the
7691 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007694 if (*start != '}')
7695 {
7696 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7697 return FAIL;
7698 if (*start == '}')
7699 return NOTDONE;
7700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701
7702 if (evaluate)
7703 {
7704 d = dict_alloc();
7705 if (d == NULL)
7706 return FAIL;
7707 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007708 tvkey.v_type = VAR_UNKNOWN;
7709 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710
7711 *arg = skipwhite(*arg + 1);
7712 while (**arg != '}' && **arg != NUL)
7713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 if (**arg != ':')
7717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007718 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007719 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 goto failret;
7721 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007722 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007724 key = get_tv_string_buf_chk(&tvkey, buf);
7725 if (key == NULL || *key == NUL)
7726 {
7727 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7728 if (key != NULL)
7729 EMSG(_(e_emptykey));
7730 clear_tv(&tvkey);
7731 goto failret;
7732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734
7735 *arg = skipwhite(*arg + 1);
7736 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7737 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007738 if (evaluate)
7739 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 goto failret;
7741 }
7742 if (evaluate)
7743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007744 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 if (item != NULL)
7746 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007747 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 clear_tv(&tv);
7750 goto failret;
7751 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 item = dictitem_alloc(key);
7753 clear_tv(&tvkey);
7754 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007757 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 if (dict_add(d, item) == FAIL)
7759 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760 }
7761 }
7762
7763 if (**arg == '}')
7764 break;
7765 if (**arg != ',')
7766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007767 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768 goto failret;
7769 }
7770 *arg = skipwhite(*arg + 1);
7771 }
7772
7773 if (**arg != '}')
7774 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007775 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007776failret:
7777 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007778 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 return FAIL;
7780 }
7781
7782 *arg = skipwhite(*arg + 1);
7783 if (evaluate)
7784 {
7785 rettv->v_type = VAR_DICT;
7786 rettv->vval.v_dict = d;
7787 ++d->dv_refcount;
7788 }
7789
7790 return OK;
7791}
7792
Bram Moolenaar8c711452005-01-14 21:53:12 +00007793/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 * Return a string with the string representation of a variable.
7795 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007796 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007797 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007798 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007799 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 */
7801 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007805 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007806 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 static int recurse = 0;
7809 char_u *r = NULL;
7810
Bram Moolenaar33570922005-01-25 22:26:29 +00007811 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007813 if (!did_echo_string_emsg)
7814 {
7815 /* Only give this message once for a recursive call to avoid
7816 * flooding the user with errors. And stop iterating over lists
7817 * and dicts. */
7818 did_echo_string_emsg = TRUE;
7819 EMSG(_("E724: variable nested too deep for displaying"));
7820 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007822 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 }
7824 ++recurse;
7825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 switch (tv->v_type)
7827 {
7828 case VAR_FUNC:
7829 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 r = tv->vval.v_string;
7831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007834 if (tv->vval.v_list == NULL)
7835 {
7836 *tofree = NULL;
7837 r = NULL;
7838 }
7839 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7840 {
7841 *tofree = NULL;
7842 r = (char_u *)"[...]";
7843 }
7844 else
7845 {
7846 tv->vval.v_list->lv_copyID = copyID;
7847 *tofree = list2string(tv, copyID);
7848 r = *tofree;
7849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007853 if (tv->vval.v_dict == NULL)
7854 {
7855 *tofree = NULL;
7856 r = NULL;
7857 }
7858 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7859 {
7860 *tofree = NULL;
7861 r = (char_u *)"{...}";
7862 }
7863 else
7864 {
7865 tv->vval.v_dict->dv_copyID = copyID;
7866 *tofree = dict2string(tv, copyID);
7867 r = *tofree;
7868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007870
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 case VAR_STRING:
7872 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007873 *tofree = NULL;
7874 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#ifdef FEAT_FLOAT
7878 case VAR_FLOAT:
7879 *tofree = NULL;
7880 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7881 r = numbuf;
7882 break;
7883#endif
7884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889
Bram Moolenaar8502c702014-06-17 12:51:16 +02007890 if (--recurse == 0)
7891 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893}
7894
7895/*
7896 * Return a string with the string representation of a variable.
7897 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7898 * "numbuf" is used for a number.
7899 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007900 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 */
7902 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007903tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 char_u **tofree;
7906 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007907 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007908{
7909 switch (tv->v_type)
7910 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 case VAR_FUNC:
7912 *tofree = string_quote(tv->vval.v_string, TRUE);
7913 return *tofree;
7914 case VAR_STRING:
7915 *tofree = string_quote(tv->vval.v_string, FALSE);
7916 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#ifdef FEAT_FLOAT
7918 case VAR_FLOAT:
7919 *tofree = NULL;
7920 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7921 return numbuf;
7922#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007923 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007926 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007930 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931}
7932
7933/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 * Return string "str" in ' quotes, doubling ' characters.
7935 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937 */
7938 static char_u *
7939string_quote(str, function)
7940 char_u *str;
7941 int function;
7942{
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 char_u *p, *r, *s;
7945
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 len = (function ? 13 : 3);
7947 if (str != NULL)
7948 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 for (p = str; *p != NUL; mb_ptr_adv(p))
7951 if (*p == '\'')
7952 ++len;
7953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 s = r = alloc(len);
7955 if (r != NULL)
7956 {
7957 if (function)
7958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 r += 10;
7961 }
7962 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 if (str != NULL)
7965 for (p = str; *p != NUL; )
7966 {
7967 if (*p == '\'')
7968 *r++ = '\'';
7969 MB_COPY_CHAR(p, r);
7970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 if (function)
7973 *r++ = ')';
7974 *r++ = NUL;
7975 }
7976 return s;
7977}
7978
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007979#ifdef FEAT_FLOAT
7980/*
7981 * Convert the string "text" to a floating point number.
7982 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7983 * this always uses a decimal point.
7984 * Returns the length of the text that was consumed.
7985 */
7986 static int
7987string2float(text, value)
7988 char_u *text;
7989 float_T *value; /* result stored here */
7990{
7991 char *s = (char *)text;
7992 float_T f;
7993
7994 f = strtod(s, &s);
7995 *value = f;
7996 return (int)((char_u *)s - text);
7997}
7998#endif
7999
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 * Get the value of an environment variable.
8002 * "arg" is pointing to the '$'. It is advanced to after the name.
8003 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008004 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 */
8006 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 int evaluate;
8011{
8012 char_u *string = NULL;
8013 int len;
8014 int cc;
8015 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008016 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 ++*arg;
8019 name = *arg;
8020 len = get_env_len(arg);
8021 if (evaluate)
8022 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008023 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008024 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008025
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008026 cc = name[len];
8027 name[len] = NUL;
8028 /* first try vim_getenv(), fast for normal environment vars */
8029 string = vim_getenv(name, &mustfree);
8030 if (string != NULL && *string != NUL)
8031 {
8032 if (!mustfree)
8033 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008035 else
8036 {
8037 if (mustfree)
8038 vim_free(string);
8039
8040 /* next try expanding things like $VIM and ${HOME} */
8041 string = expand_env_save(name - 1);
8042 if (string != NULL && *string == '$')
8043 {
8044 vim_free(string);
8045 string = NULL;
8046 }
8047 }
8048 name[len] = cc;
8049
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 rettv->v_type = VAR_STRING;
8051 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053
8054 return OK;
8055}
8056
8057/*
8058 * Array with names and number of arguments of all internal functions
8059 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8060 */
8061static struct fst
8062{
8063 char *f_name; /* function name */
8064 char f_min_argc; /* minimal number of arguments */
8065 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008067 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068} functions[] =
8069{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008074 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008075 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008076 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"append", 2, 2, f_append},
8078 {"argc", 0, 0, f_argc},
8079 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008080 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008081 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008082#ifdef FEAT_FLOAT
8083 {"asin", 1, 1, f_asin}, /* WJMc */
8084#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008085 {"assert_equal", 2, 3, f_assert_equal},
8086 {"assert_false", 1, 2, f_assert_false},
8087 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#ifdef FEAT_FLOAT
8089 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008090 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008093 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"bufexists", 1, 1, f_bufexists},
8095 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8096 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8097 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8098 {"buflisted", 1, 1, f_buflisted},
8099 {"bufloaded", 1, 1, f_bufloaded},
8100 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008101 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"bufwinnr", 1, 1, f_bufwinnr},
8103 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008104 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008105 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#ifdef FEAT_FLOAT
8108 {"ceil", 1, 1, f_ceil},
8109#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008110 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008111 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008113 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008115#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008116 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008117 {"complete_add", 1, 1, f_complete_add},
8118 {"complete_check", 0, 0, f_complete_check},
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008124 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008125#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008126 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008128 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008129 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"delete", 1, 1, f_delete},
8131 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008132 {"diff_filler", 1, 1, f_diff_filler},
8133 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008134 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008136 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"eventhandler", 0, 0, f_eventhandler},
8138 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008139 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008141#ifdef FEAT_FLOAT
8142 {"exp", 1, 1, f_exp},
8143#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008144 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008145 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008146 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8148 {"filereadable", 1, 1, f_filereadable},
8149 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008150 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008151 {"finddir", 1, 3, f_finddir},
8152 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008153#ifdef FEAT_FLOAT
8154 {"float2nr", 1, 1, f_float2nr},
8155 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008156 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008158 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"fnamemodify", 2, 2, f_fnamemodify},
8160 {"foldclosed", 1, 1, f_foldclosed},
8161 {"foldclosedend", 1, 1, f_foldclosedend},
8162 {"foldlevel", 1, 1, f_foldlevel},
8163 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008164 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008167 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008168 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008169 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008170 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"getchar", 0, 1, f_getchar},
8172 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008173 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"getcmdline", 0, 0, f_getcmdline},
8175 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008176 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008177 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008178 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008180 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008181 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"getfsize", 1, 1, f_getfsize},
8183 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008184 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008185 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008186 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008187 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008188 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008189 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008190 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008191 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008193 {"gettabvar", 2, 3, f_gettabvar},
8194 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"getwinposx", 0, 0, f_getwinposx},
8196 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008197 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008198 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008199 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008200 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008203 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008204 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8206 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8207 {"histadd", 2, 2, f_histadd},
8208 {"histdel", 1, 2, f_histdel},
8209 {"histget", 1, 2, f_histget},
8210 {"histnr", 1, 1, f_histnr},
8211 {"hlID", 1, 1, f_hlID},
8212 {"hlexists", 1, 1, f_hlexists},
8213 {"hostname", 0, 0, f_hostname},
8214 {"iconv", 3, 3, f_iconv},
8215 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008216 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008217 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008219 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"inputrestore", 0, 0, f_inputrestore},
8221 {"inputsave", 0, 0, f_inputsave},
8222 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008224 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008226 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008228 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008229 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"libcall", 3, 3, f_libcall},
8233 {"libcallnr", 3, 3, f_libcallnr},
8234 {"line", 1, 1, f_line},
8235 {"line2byte", 1, 1, f_line2byte},
8236 {"lispindent", 1, 1, f_lispindent},
8237 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240 {"log10", 1, 1, f_log10},
8241#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008242#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008243 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008244#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008246 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008247 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008248 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008249 {"matchadd", 2, 5, f_matchadd},
8250 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008251 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008252 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008253 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008254 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008255 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008256 {"max", 1, 1, f_max},
8257 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008258#ifdef vim_mkdir
8259 {"mkdir", 1, 3, f_mkdir},
8260#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008262#ifdef FEAT_MZSCHEME
8263 {"mzeval", 1, 1, f_mzeval},
8264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008266 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008267 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008268 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"pow", 2, 2, f_pow},
8271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008273 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008274 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008275#ifdef FEAT_PYTHON3
8276 {"py3eval", 1, 1, f_py3eval},
8277#endif
8278#ifdef FEAT_PYTHON
8279 {"pyeval", 1, 1, f_pyeval},
8280#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008281 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008282 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008283 {"reltime", 0, 2, f_reltime},
8284 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"remote_expr", 2, 3, f_remote_expr},
8286 {"remote_foreground", 1, 1, f_remote_foreground},
8287 {"remote_peek", 1, 2, f_remote_peek},
8288 {"remote_read", 1, 1, f_remote_read},
8289 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008290 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008292 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008294 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008295#ifdef FEAT_FLOAT
8296 {"round", 1, 1, f_round},
8297#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008298 {"screenattr", 2, 2, f_screenattr},
8299 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008300 {"screencol", 0, 0, f_screencol},
8301 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008302 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008303 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008304 {"searchpair", 3, 7, f_searchpair},
8305 {"searchpairpos", 3, 7, f_searchpairpos},
8306 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"server2client", 2, 2, f_server2client},
8308 {"serverlist", 0, 0, f_serverlist},
8309 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008310 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"setcmdpos", 1, 1, f_setcmdpos},
8312 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008313 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008314 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008315 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008316 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008318 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008319 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008321#ifdef FEAT_CRYPT
8322 {"sha256", 1, 1, f_sha256},
8323#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008324 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008325 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#ifdef FEAT_FLOAT
8328 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008329 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008331 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008332 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008333 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008334 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008335 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#ifdef FEAT_FLOAT
8337 {"sqrt", 1, 1, f_sqrt},
8338 {"str2float", 1, 1, f_str2float},
8339#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008340 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008341 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008342 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343#ifdef HAVE_STRFTIME
8344 {"strftime", 1, 2, f_strftime},
8345#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008347 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"strlen", 1, 1, f_strlen},
8349 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008350 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008352 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008353 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"substitute", 4, 4, f_substitute},
8355 {"synID", 3, 3, f_synID},
8356 {"synIDattr", 2, 3, f_synIDattr},
8357 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008358 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008359 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008360 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008361 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008362 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008363 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008364 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008365 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008366 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008367#ifdef FEAT_FLOAT
8368 {"tan", 1, 1, f_tan},
8369 {"tanh", 1, 1, f_tanh},
8370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008372 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"tolower", 1, 1, f_tolower},
8374 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008375 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008376#ifdef FEAT_FLOAT
8377 {"trunc", 1, 1, f_trunc},
8378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008380 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008381 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008382 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008383 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"virtcol", 1, 1, f_virtcol},
8385 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008386 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"winbufnr", 1, 1, f_winbufnr},
8388 {"wincol", 0, 0, f_wincol},
8389 {"winheight", 1, 1, f_winheight},
8390 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008391 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008393 {"winrestview", 1, 1, f_winrestview},
8394 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008396 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008397 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008398 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399};
8400
8401#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8402
8403/*
8404 * Function given to ExpandGeneric() to obtain the list of internal
8405 * or user defined function names.
8406 */
8407 char_u *
8408get_function_name(xp, idx)
8409 expand_T *xp;
8410 int idx;
8411{
8412 static int intidx = -1;
8413 char_u *name;
8414
8415 if (idx == 0)
8416 intidx = -1;
8417 if (intidx < 0)
8418 {
8419 name = get_user_func_name(xp, idx);
8420 if (name != NULL)
8421 return name;
8422 }
8423 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8424 {
8425 STRCPY(IObuff, functions[intidx].f_name);
8426 STRCAT(IObuff, "(");
8427 if (functions[intidx].f_max_argc == 0)
8428 STRCAT(IObuff, ")");
8429 return IObuff;
8430 }
8431
8432 return NULL;
8433}
8434
8435/*
8436 * Function given to ExpandGeneric() to obtain the list of internal or
8437 * user defined variable or function names.
8438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 char_u *
8440get_expr_name(xp, idx)
8441 expand_T *xp;
8442 int idx;
8443{
8444 static int intidx = -1;
8445 char_u *name;
8446
8447 if (idx == 0)
8448 intidx = -1;
8449 if (intidx < 0)
8450 {
8451 name = get_function_name(xp, idx);
8452 if (name != NULL)
8453 return name;
8454 }
8455 return get_user_var_name(xp, ++intidx);
8456}
8457
8458#endif /* FEAT_CMDL_COMPL */
8459
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008460#if defined(EBCDIC) || defined(PROTO)
8461/*
8462 * Compare struct fst by function name.
8463 */
8464 static int
8465compare_func_name(s1, s2)
8466 const void *s1;
8467 const void *s2;
8468{
8469 struct fst *p1 = (struct fst *)s1;
8470 struct fst *p2 = (struct fst *)s2;
8471
8472 return STRCMP(p1->f_name, p2->f_name);
8473}
8474
8475/*
8476 * Sort the function table by function name.
8477 * The sorting of the table above is ASCII dependant.
8478 * On machines using EBCDIC we have to sort it.
8479 */
8480 static void
8481sortFunctions()
8482{
8483 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8484
8485 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8486}
8487#endif
8488
8489
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490/*
8491 * Find internal function in table above.
8492 * Return index, or -1 if not found
8493 */
8494 static int
8495find_internal_func(name)
8496 char_u *name; /* name of the function */
8497{
8498 int first = 0;
8499 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8500 int cmp;
8501 int x;
8502
8503 /*
8504 * Find the function name in the table. Binary search.
8505 */
8506 while (first <= last)
8507 {
8508 x = first + ((unsigned)(last - first) >> 1);
8509 cmp = STRCMP(name, functions[x].f_name);
8510 if (cmp < 0)
8511 last = x - 1;
8512 else if (cmp > 0)
8513 first = x + 1;
8514 else
8515 return x;
8516 }
8517 return -1;
8518}
8519
8520/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8522 * name it contains, otherwise return "name".
8523 */
8524 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008525deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 char_u *name;
8527 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008528 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529{
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 int cc;
8532
8533 cc = name[*lenp];
8534 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008535 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 {
8541 *lenp = 0;
8542 return (char_u *)""; /* just in case */
8543 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008544 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008545 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008546 }
8547
8548 return name;
8549}
8550
8551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 * Allocate a variable for the result of a function.
8553 * Return OK or FAIL.
8554 */
8555 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008556get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8557 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 char_u *name; /* name of the function */
8559 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 char_u **arg; /* argument, pointing to the '(' */
8562 linenr_T firstline; /* first line of range */
8563 linenr_T lastline; /* last line of range */
8564 int *doesrange; /* return: function handled range */
8565 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 char_u *argp;
8569 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008570 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 int argcount = 0; /* number of arguments found */
8572
8573 /*
8574 * Get the arguments.
8575 */
8576 argp = *arg;
8577 while (argcount < MAX_FUNC_ARGS)
8578 {
8579 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8580 if (*argp == ')' || *argp == ',' || *argp == NUL)
8581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8583 {
8584 ret = FAIL;
8585 break;
8586 }
8587 ++argcount;
8588 if (*argp != ',')
8589 break;
8590 }
8591 if (*argp == ')')
8592 ++argp;
8593 else
8594 ret = FAIL;
8595
8596 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 {
8601 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008602 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008604 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
8610 *arg = skipwhite(argp);
8611 return ret;
8612}
8613
8614
8615/*
8616 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008617 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008618 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 */
8620 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008621call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008623 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008627 typval_T *argvars; /* vars for arguments, must have "argcount"
8628 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 linenr_T firstline; /* first line of range */
8630 linenr_T lastline; /* last line of range */
8631 int *doesrange; /* return: function handled range */
8632 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636#define ERROR_UNKNOWN 0
8637#define ERROR_TOOMANY 1
8638#define ERROR_TOOFEW 2
8639#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640#define ERROR_DICT 4
8641#define ERROR_NONE 5
8642#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 int error = ERROR_NONE;
8644 int i;
8645 int llen;
8646 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#define FLEN_FIXED 40
8648 char_u fname_buf[FLEN_FIXED + 1];
8649 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008650 char_u *name;
8651
8652 /* Make a copy of the name, if it comes from a funcref variable it could
8653 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008654 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008655 if (name == NULL)
8656 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
8658 /*
8659 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8660 * Change <SNR>123_name() to K_SNR 123_name().
8661 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 llen = eval_fname_script(name);
8664 if (llen > 0)
8665 {
8666 fname_buf[0] = K_SPECIAL;
8667 fname_buf[1] = KS_EXTRA;
8668 fname_buf[2] = (int)KE_SNR;
8669 i = 3;
8670 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8671 {
8672 if (current_SID <= 0)
8673 error = ERROR_SCRIPT;
8674 else
8675 {
8676 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8677 i = (int)STRLEN(fname_buf);
8678 }
8679 }
8680 if (i + STRLEN(name + llen) < FLEN_FIXED)
8681 {
8682 STRCPY(fname_buf + i, name + llen);
8683 fname = fname_buf;
8684 }
8685 else
8686 {
8687 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8688 if (fname == NULL)
8689 error = ERROR_OTHER;
8690 else
8691 {
8692 mch_memmove(fname, fname_buf, (size_t)i);
8693 STRCPY(fname + i, name + llen);
8694 }
8695 }
8696 }
8697 else
8698 fname = name;
8699
8700 *doesrange = FALSE;
8701
8702
8703 /* execute the function if no errors detected and executing */
8704 if (evaluate && error == ERROR_NONE)
8705 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008706 char_u *rfname = fname;
8707
8708 /* Ignore "g:" before a function name. */
8709 if (fname[0] == 'g' && fname[1] == ':')
8710 rfname = fname + 2;
8711
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008712 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 error = ERROR_UNKNOWN;
8715
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008716 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {
8718 /*
8719 * User defined function.
8720 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008724 /* Trigger FuncUndefined event, may load the function. */
8725 if (fp == NULL
8726 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008727 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008728 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008730 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008731 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 }
8733#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008734 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008735 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008736 {
8737 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 }
8740
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (fp != NULL)
8742 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008743 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008745 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008747 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008750 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 else
8752 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008753 int did_save_redo = FALSE;
8754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 /*
8756 * Call the user function.
8757 * Save and restore search patterns, script variables and
8758 * redo buffer.
8759 */
8760 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008761#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008762 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008763#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008764 {
8765 saveRedobuff();
8766 did_save_redo = TRUE;
8767 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008768 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008770 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008771 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8772 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8773 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008774 /* Function was unreferenced while being used, free it
8775 * now. */
8776 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008777 if (did_save_redo)
8778 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 restore_search_patterns();
8780 error = ERROR_NONE;
8781 }
8782 }
8783 }
8784 else
8785 {
8786 /*
8787 * Find the function name in the table, call its implementation.
8788 */
8789 i = find_internal_func(fname);
8790 if (i >= 0)
8791 {
8792 if (argcount < functions[i].f_min_argc)
8793 error = ERROR_TOOFEW;
8794 else if (argcount > functions[i].f_max_argc)
8795 error = ERROR_TOOMANY;
8796 else
8797 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008798 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 error = ERROR_NONE;
8801 }
8802 }
8803 }
8804 /*
8805 * The function call (or "FuncUndefined" autocommand sequence) might
8806 * have been aborted by an error, an interrupt, or an explicitly thrown
8807 * exception that has not been caught so far. This situation can be
8808 * tested for by calling aborting(). For an error in an internal
8809 * function or for the "E132" error in call_user_func(), however, the
8810 * throw point at which the "force_abort" flag (temporarily reset by
8811 * emsg()) is normally updated has not been reached yet. We need to
8812 * update that flag first to make aborting() reliable.
8813 */
8814 update_force_abort();
8815 }
8816 if (error == ERROR_NONE)
8817 ret = OK;
8818
8819 /*
8820 * Report an error unless the argument evaluation or function call has been
8821 * cancelled due to an aborting error, an interrupt, or an exception.
8822 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 if (!aborting())
8824 {
8825 switch (error)
8826 {
8827 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008828 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008829 break;
8830 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008831 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 break;
8833 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008834 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008835 name);
8836 break;
8837 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008838 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008839 name);
8840 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008842 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 name);
8844 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008845 }
8846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (fname != name && fname != fname_buf)
8849 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008850 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
8852 return ret;
8853}
8854
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008855/*
8856 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008857 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008858 */
8859 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008860emsg_funcname(ermsg, name)
8861 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008862 char_u *name;
8863{
8864 char_u *p;
8865
8866 if (*name == K_SPECIAL)
8867 p = concat_str((char_u *)"<SNR>", name + 3);
8868 else
8869 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008870 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008871 if (p != name)
8872 vim_free(p);
8873}
8874
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008875/*
8876 * Return TRUE for a non-zero Number and a non-empty String.
8877 */
8878 static int
8879non_zero_arg(argvars)
8880 typval_T *argvars;
8881{
8882 return ((argvars[0].v_type == VAR_NUMBER
8883 && argvars[0].vval.v_number != 0)
8884 || (argvars[0].v_type == VAR_STRING
8885 && argvars[0].vval.v_string != NULL
8886 && *argvars[0].vval.v_string != NUL));
8887}
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889/*********************************************
8890 * Implementation of the built-in functions
8891 */
8892
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008894static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8895
8896/*
8897 * Get the float value of "argvars[0]" into "f".
8898 * Returns FAIL when the argument is not a Number or Float.
8899 */
8900 static int
8901get_float_arg(argvars, f)
8902 typval_T *argvars;
8903 float_T *f;
8904{
8905 if (argvars[0].v_type == VAR_FLOAT)
8906 {
8907 *f = argvars[0].vval.v_float;
8908 return OK;
8909 }
8910 if (argvars[0].v_type == VAR_NUMBER)
8911 {
8912 *f = (float_T)argvars[0].vval.v_number;
8913 return OK;
8914 }
8915 EMSG(_("E808: Number or Float required"));
8916 return FAIL;
8917}
8918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008919/*
8920 * "abs(expr)" function
8921 */
8922 static void
8923f_abs(argvars, rettv)
8924 typval_T *argvars;
8925 typval_T *rettv;
8926{
8927 if (argvars[0].v_type == VAR_FLOAT)
8928 {
8929 rettv->v_type = VAR_FLOAT;
8930 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8931 }
8932 else
8933 {
8934 varnumber_T n;
8935 int error = FALSE;
8936
8937 n = get_tv_number_chk(&argvars[0], &error);
8938 if (error)
8939 rettv->vval.v_number = -1;
8940 else if (n > 0)
8941 rettv->vval.v_number = n;
8942 else
8943 rettv->vval.v_number = -n;
8944 }
8945}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008946
8947/*
8948 * "acos()" function
8949 */
8950 static void
8951f_acos(argvars, rettv)
8952 typval_T *argvars;
8953 typval_T *rettv;
8954{
8955 float_T f;
8956
8957 rettv->v_type = VAR_FLOAT;
8958 if (get_float_arg(argvars, &f) == OK)
8959 rettv->vval.v_float = acos(f);
8960 else
8961 rettv->vval.v_float = 0.0;
8962}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008963#endif
8964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008966 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 */
8968 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008969f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008978 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008979 && !tv_check_lock(l->lv_lock,
8980 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008981 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 }
8984 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985 EMSG(_(e_listreq));
8986}
8987
8988/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008989 * "alloc_fail(id, countdown, repeat)" function
8990 */
8991 static void
8992f_alloc_fail(argvars, rettv)
8993 typval_T *argvars;
8994 typval_T *rettv UNUSED;
8995{
8996 if (argvars[0].v_type != VAR_NUMBER
8997 || argvars[0].vval.v_number <= 0
8998 || argvars[1].v_type != VAR_NUMBER
8999 || argvars[1].vval.v_number < 0
9000 || argvars[2].v_type != VAR_NUMBER)
9001 EMSG(_(e_invarg));
9002 else
9003 {
9004 alloc_fail_id = argvars[0].vval.v_number;
9005 alloc_fail_countdown = argvars[1].vval.v_number;
9006 alloc_fail_repeat = argvars[2].vval.v_number;
9007 }
9008}
9009
9010/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009011 * "and(expr, expr)" function
9012 */
9013 static void
9014f_and(argvars, rettv)
9015 typval_T *argvars;
9016 typval_T *rettv;
9017{
9018 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9019 & get_tv_number_chk(&argvars[1], NULL);
9020}
9021
9022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 * "append(lnum, string/list)" function
9024 */
9025 static void
9026f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009029{
9030 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 list_T *l = NULL;
9033 listitem_T *li = NULL;
9034 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009035 long added = 0;
9036
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009037 /* When coming here from Insert mode, sync undo, so that this can be
9038 * undone separately from what was previously inserted. */
9039 if (u_sync_once == 2)
9040 {
9041 u_sync_once = 1; /* notify that u_sync() was called */
9042 u_sync(TRUE);
9043 }
9044
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045 lnum = get_tv_lnum(argvars);
9046 if (lnum >= 0
9047 && lnum <= curbuf->b_ml.ml_line_count
9048 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009049 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009050 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009051 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 l = argvars[1].vval.v_list;
9053 if (l == NULL)
9054 return;
9055 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 for (;;)
9058 {
9059 if (l == NULL)
9060 tv = &argvars[1]; /* append a string */
9061 else if (li == NULL)
9062 break; /* end of list */
9063 else
9064 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 line = get_tv_string_chk(tv);
9066 if (line == NULL) /* type error */
9067 {
9068 rettv->vval.v_number = 1; /* Failed */
9069 break;
9070 }
9071 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 ++added;
9073 if (l == NULL)
9074 break;
9075 li = li->li_next;
9076 }
9077
9078 appended_lines_mark(lnum, added);
9079 if (curwin->w_cursor.lnum > lnum)
9080 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009082 else
9083 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084}
9085
9086/*
9087 * "argc()" function
9088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095}
9096
9097/*
9098 * "argidx()" function
9099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009102 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106}
9107
9108/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009109 * "arglistid()" function
9110 */
9111 static void
9112f_arglistid(argvars, rettv)
9113 typval_T *argvars UNUSED;
9114 typval_T *rettv;
9115{
9116 win_T *wp;
9117 tabpage_T *tp = NULL;
9118 long n;
9119
9120 rettv->vval.v_number = -1;
9121 if (argvars[0].v_type != VAR_UNKNOWN)
9122 {
9123 if (argvars[1].v_type != VAR_UNKNOWN)
9124 {
9125 n = get_tv_number(&argvars[1]);
9126 if (n >= 0)
9127 tp = find_tabpage(n);
9128 }
9129 else
9130 tp = curtab;
9131
9132 if (tp != NULL)
9133 {
9134 wp = find_win_by_nr(&argvars[0], tp);
9135 if (wp != NULL)
9136 rettv->vval.v_number = wp->w_alist->id;
9137 }
9138 }
9139 else
9140 rettv->vval.v_number = curwin->w_alist->id;
9141}
9142
9143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 * "argv(nr)" function
9145 */
9146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *argvars;
9149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150{
9151 int idx;
9152
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009153 if (argvars[0].v_type != VAR_UNKNOWN)
9154 {
9155 idx = get_tv_number_chk(&argvars[0], NULL);
9156 if (idx >= 0 && idx < ARGCOUNT)
9157 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9158 else
9159 rettv->vval.v_string = NULL;
9160 rettv->v_type = VAR_STRING;
9161 }
9162 else if (rettv_list_alloc(rettv) == OK)
9163 for (idx = 0; idx < ARGCOUNT; ++idx)
9164 list_append_string(rettv->vval.v_list,
9165 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166}
9167
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009168static void prepare_assert_error __ARGS((garray_T*gap));
9169static 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));
9170static void assert_error __ARGS((garray_T *gap));
9171static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9172
9173/*
9174 * Prepare "gap" for an assert error and add the sourcing position.
9175 */
9176 static void
9177prepare_assert_error(gap)
9178 garray_T *gap;
9179{
9180 char buf[NUMBUFLEN];
9181
9182 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009183 if (sourcing_name != NULL)
9184 {
9185 ga_concat(gap, sourcing_name);
9186 if (sourcing_lnum > 0)
9187 ga_concat(gap, (char_u *)" ");
9188 }
9189 if (sourcing_lnum > 0)
9190 {
9191 sprintf(buf, "line %ld", (long)sourcing_lnum);
9192 ga_concat(gap, (char_u *)buf);
9193 }
9194 if (sourcing_name != NULL || sourcing_lnum > 0)
9195 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009196}
9197
9198/*
9199 * Fill "gap" with information about an assert error.
9200 */
9201 static void
9202fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9203 garray_T *gap;
9204 typval_T *opt_msg_tv;
9205 char_u *exp_str;
9206 typval_T *exp_tv;
9207 typval_T *got_tv;
9208{
9209 char_u numbuf[NUMBUFLEN];
9210 char_u *tofree;
9211
9212 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9213 {
9214 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9215 vim_free(tofree);
9216 }
9217 else
9218 {
9219 ga_concat(gap, (char_u *)"Expected ");
9220 if (exp_str == NULL)
9221 {
9222 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9223 vim_free(tofree);
9224 }
9225 else
9226 ga_concat(gap, exp_str);
9227 ga_concat(gap, (char_u *)" but got ");
9228 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9229 vim_free(tofree);
9230 }
9231}
Bram Moolenaar43345542015-11-29 17:35:35 +01009232
9233/*
9234 * Add an assert error to v:errors.
9235 */
9236 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009238 garray_T *gap;
9239{
9240 struct vimvar *vp = &vimvars[VV_ERRORS];
9241
9242 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9243 /* Make sure v:errors is a list. */
9244 set_vim_var_list(VV_ERRORS, list_alloc());
9245 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9246}
9247
Bram Moolenaar43345542015-11-29 17:35:35 +01009248/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009250 */
9251 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009252f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009253 typval_T *argvars;
9254 typval_T *rettv UNUSED;
9255{
9256 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009257
9258 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9259 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009260 prepare_assert_error(&ga);
9261 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9262 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009263 ga_clear(&ga);
9264 }
9265}
9266
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267/*
9268 * Common for assert_true() and assert_false().
9269 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009270 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009271assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009272 typval_T *argvars;
9273 int isTrue;
9274{
9275 int error = FALSE;
9276 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009277
9278 if (argvars[0].v_type != VAR_NUMBER
9279 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9280 || error)
9281 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009282 prepare_assert_error(&ga);
9283 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009284 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009285 NULL, &argvars[0]);
9286 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009287 ga_clear(&ga);
9288 }
9289}
9290
9291/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009292 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009293 */
9294 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009295f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009296 typval_T *argvars;
9297 typval_T *rettv UNUSED;
9298{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009299 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009300}
9301
9302/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009304 */
9305 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009307 typval_T *argvars;
9308 typval_T *rettv UNUSED;
9309{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009311}
9312
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009314/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009315 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009316 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009317 static void
9318f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322 float_T f;
9323
9324 rettv->v_type = VAR_FLOAT;
9325 if (get_float_arg(argvars, &f) == OK)
9326 rettv->vval.v_float = asin(f);
9327 else
9328 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009329}
9330
9331/*
9332 * "atan()" function
9333 */
9334 static void
9335f_atan(argvars, rettv)
9336 typval_T *argvars;
9337 typval_T *rettv;
9338{
9339 float_T f;
9340
9341 rettv->v_type = VAR_FLOAT;
9342 if (get_float_arg(argvars, &f) == OK)
9343 rettv->vval.v_float = atan(f);
9344 else
9345 rettv->vval.v_float = 0.0;
9346}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009347
9348/*
9349 * "atan2()" function
9350 */
9351 static void
9352f_atan2(argvars, rettv)
9353 typval_T *argvars;
9354 typval_T *rettv;
9355{
9356 float_T fx, fy;
9357
9358 rettv->v_type = VAR_FLOAT;
9359 if (get_float_arg(argvars, &fx) == OK
9360 && get_float_arg(&argvars[1], &fy) == OK)
9361 rettv->vval.v_float = atan2(fx, fy);
9362 else
9363 rettv->vval.v_float = 0.0;
9364}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365#endif
9366
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367/*
9368 * "browse(save, title, initdir, default)" function
9369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375#ifdef FEAT_BROWSE
9376 int save;
9377 char_u *title;
9378 char_u *initdir;
9379 char_u *defname;
9380 char_u buf[NUMBUFLEN];
9381 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 save = get_tv_number_chk(&argvars[0], &error);
9385 title = get_tv_string_chk(&argvars[1]);
9386 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9387 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 if (error || title == NULL || initdir == NULL || defname == NULL)
9390 rettv->vval.v_string = NULL;
9391 else
9392 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009393 do_browse(save ? BROWSE_SAVE : 0,
9394 title, defname, NULL, initdir, NULL, curbuf);
9395#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009397#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009399}
9400
9401/*
9402 * "browsedir(title, initdir)" function
9403 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009405f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009408{
9409#ifdef FEAT_BROWSE
9410 char_u *title;
9411 char_u *initdir;
9412 char_u buf[NUMBUFLEN];
9413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 title = get_tv_string_chk(&argvars[0]);
9415 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009417 if (title == NULL || initdir == NULL)
9418 rettv->vval.v_string = NULL;
9419 else
9420 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009421 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
Bram Moolenaar33570922005-01-25 22:26:29 +00009428static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009429
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430/*
9431 * Find a buffer by number or exact name.
9432 */
9433 static buf_T *
9434find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009439 if (avar->v_type == VAR_NUMBER)
9440 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009441 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009443 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009444 if (buf == NULL)
9445 {
9446 /* No full path name match, try a match with a URL or a "nofile"
9447 * buffer, these don't use the full path. */
9448 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9449 if (buf->b_fname != NULL
9450 && (path_with_url(buf->b_fname)
9451#ifdef FEAT_QUICKFIX
9452 || bt_nofile(buf)
9453#endif
9454 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009455 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009456 break;
9457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 }
9459 return buf;
9460}
9461
9462/*
9463 * "bufexists(expr)" function
9464 */
9465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
9474 * "buflisted(expr)" function
9475 */
9476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009478 typval_T *argvars;
9479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 buf_T *buf;
9482
9483 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
9488 * "bufloaded(expr)" function
9489 */
9490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009492 typval_T *argvars;
9493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 buf_T *buf;
9496
9497 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009501static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * Get buffer by number or pattern.
9505 */
9506 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009507get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009509 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 int save_magic;
9513 char_u *save_cpo;
9514 buf_T *buf;
9515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 if (tv->v_type == VAR_NUMBER)
9517 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009518 if (tv->v_type != VAR_STRING)
9519 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (name == NULL || *name == NUL)
9521 return curbuf;
9522 if (name[0] == '$' && name[1] == NUL)
9523 return lastbuf;
9524
9525 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9526 save_magic = p_magic;
9527 p_magic = TRUE;
9528 save_cpo = p_cpo;
9529 p_cpo = (char_u *)"";
9530
9531 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009532 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
9534 p_magic = save_magic;
9535 p_cpo = save_cpo;
9536
9537 /* If not found, try expanding the name, like done for bufexists(). */
9538 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 return buf;
9542}
9543
9544/*
9545 * "bufname(expr)" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 buf_T *buf;
9553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009556 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 --emsg_off;
9563}
9564
9565/*
9566 * "bufnr(expr)" function
9567 */
9568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009574 int error = FALSE;
9575 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009579 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009580 --emsg_off;
9581
9582 /* If the buffer isn't found and the second argument is not zero create a
9583 * new buffer. */
9584 if (buf == NULL
9585 && argvars[1].v_type != VAR_UNKNOWN
9586 && get_tv_number_chk(&argvars[1], &error) != 0
9587 && !error
9588 && (name = get_tv_string_chk(&argvars[0])) != NULL
9589 && !error)
9590 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596}
9597
9598/*
9599 * "bufwinnr(nr)" function
9600 */
9601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 typval_T *argvars;
9604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606#ifdef FEAT_WINDOWS
9607 win_T *wp;
9608 int winnr = 0;
9609#endif
9610 buf_T *buf;
9611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009614 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615#ifdef FEAT_WINDOWS
9616 for (wp = firstwin; wp; wp = wp->w_next)
9617 {
9618 ++winnr;
9619 if (wp->w_buffer == buf)
9620 break;
9621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626 --emsg_off;
9627}
9628
9629/*
9630 * "byte2line(byte)" function
9631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639#else
9640 long boff = 0;
9641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 (linenr_T)0, &boff);
9648#endif
9649}
9650
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009651 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009652byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009653 typval_T *argvars;
9654 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009655 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009656{
9657#ifdef FEAT_MBYTE
9658 char_u *t;
9659#endif
9660 char_u *str;
9661 long idx;
9662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 str = get_tv_string_chk(&argvars[0]);
9664 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009666 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009667 return;
9668
9669#ifdef FEAT_MBYTE
9670 t = str;
9671 for ( ; idx > 0; idx--)
9672 {
9673 if (*t == NUL) /* EOL reached */
9674 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009675 if (enc_utf8 && comp)
9676 t += utf_ptr2len(t);
9677 else
9678 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009679 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009680 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009681#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009682 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009684#endif
9685}
9686
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009687/*
9688 * "byteidx()" function
9689 */
9690 static void
9691f_byteidx(argvars, rettv)
9692 typval_T *argvars;
9693 typval_T *rettv;
9694{
9695 byteidx(argvars, rettv, FALSE);
9696}
9697
9698/*
9699 * "byteidxcomp()" function
9700 */
9701 static void
9702f_byteidxcomp(argvars, rettv)
9703 typval_T *argvars;
9704 typval_T *rettv;
9705{
9706 byteidx(argvars, rettv, TRUE);
9707}
9708
Bram Moolenaardb913952012-06-29 12:54:53 +02009709 int
9710func_call(name, args, selfdict, rettv)
9711 char_u *name;
9712 typval_T *args;
9713 dict_T *selfdict;
9714 typval_T *rettv;
9715{
9716 listitem_T *item;
9717 typval_T argv[MAX_FUNC_ARGS + 1];
9718 int argc = 0;
9719 int dummy;
9720 int r = 0;
9721
9722 for (item = args->vval.v_list->lv_first; item != NULL;
9723 item = item->li_next)
9724 {
9725 if (argc == MAX_FUNC_ARGS)
9726 {
9727 EMSG(_("E699: Too many arguments"));
9728 break;
9729 }
9730 /* Make a copy of each argument. This is needed to be able to set
9731 * v_lock to VAR_FIXED in the copy without changing the original list.
9732 */
9733 copy_tv(&item->li_tv, &argv[argc++]);
9734 }
9735
9736 if (item == NULL)
9737 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9738 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9739 &dummy, TRUE, selfdict);
9740
9741 /* Free the arguments. */
9742 while (argc > 0)
9743 clear_tv(&argv[--argc]);
9744
9745 return r;
9746}
9747
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009748/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009749 * "call(func, arglist)" function
9750 */
9751 static void
9752f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009753 typval_T *argvars;
9754 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009755{
9756 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009758
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009759 if (argvars[1].v_type != VAR_LIST)
9760 {
9761 EMSG(_(e_listreq));
9762 return;
9763 }
9764 if (argvars[1].vval.v_list == NULL)
9765 return;
9766
9767 if (argvars[0].v_type == VAR_FUNC)
9768 func = argvars[0].vval.v_string;
9769 else
9770 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 if (*func == NUL)
9772 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009773
Bram Moolenaare9a41262005-01-15 22:18:47 +00009774 if (argvars[2].v_type != VAR_UNKNOWN)
9775 {
9776 if (argvars[2].v_type != VAR_DICT)
9777 {
9778 EMSG(_(e_dictreq));
9779 return;
9780 }
9781 selfdict = argvars[2].vval.v_dict;
9782 }
9783
Bram Moolenaardb913952012-06-29 12:54:53 +02009784 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009785}
9786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009787#ifdef FEAT_FLOAT
9788/*
9789 * "ceil({float})" function
9790 */
9791 static void
9792f_ceil(argvars, rettv)
9793 typval_T *argvars;
9794 typval_T *rettv;
9795{
9796 float_T f;
9797
9798 rettv->v_type = VAR_FLOAT;
9799 if (get_float_arg(argvars, &f) == OK)
9800 rettv->vval.v_float = ceil(f);
9801 else
9802 rettv->vval.v_float = 0.0;
9803}
9804#endif
9805
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009806/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009807 * "changenr()" function
9808 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009809 static void
9810f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009811 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009812 typval_T *rettv;
9813{
9814 rettv->vval.v_number = curbuf->b_u_seq_cur;
9815}
9816
9817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 * "char2nr(string)" function
9819 */
9820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825#ifdef FEAT_MBYTE
9826 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009827 {
9828 int utf8 = 0;
9829
9830 if (argvars[1].v_type != VAR_UNKNOWN)
9831 utf8 = get_tv_number_chk(&argvars[1], NULL);
9832
9833 if (utf8)
9834 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9835 else
9836 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 else
9839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841}
9842
9843/*
9844 * "cindent(lnum)" function
9845 */
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009848 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851#ifdef FEAT_CINDENT
9852 pos_T pos;
9853 linenr_T lnum;
9854
9855 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9858 {
9859 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 curwin->w_cursor = pos;
9862 }
9863 else
9864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866}
9867
9868/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009869 * "clearmatches()" function
9870 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009871 static void
9872f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009873 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009874 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009875{
9876#ifdef FEAT_SEARCH_EXTRA
9877 clear_matches(curwin);
9878#endif
9879}
9880
9881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 * "col(string)" function
9883 */
9884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009886 typval_T *argvars;
9887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889 colnr_T col = 0;
9890 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009891 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009893 fp = var2fpos(&argvars[0], FALSE, &fnum);
9894 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 {
9896 if (fp->col == MAXCOL)
9897 {
9898 /* '> can be MAXCOL, get the length of the line then */
9899 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009900 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 else
9902 col = MAXCOL;
9903 }
9904 else
9905 {
9906 col = fp->col + 1;
9907#ifdef FEAT_VIRTUALEDIT
9908 /* col(".") when the cursor is on the NUL at the end of the line
9909 * because of "coladd" can be seen as an extra column. */
9910 if (virtual_active() && fp == &curwin->w_cursor)
9911 {
9912 char_u *p = ml_get_cursor();
9913
9914 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9915 curwin->w_virtcol - curwin->w_cursor.coladd))
9916 {
9917# ifdef FEAT_MBYTE
9918 int l;
9919
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009920 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 col += l;
9922# else
9923 if (*p != NUL && p[1] == NUL)
9924 ++col;
9925# endif
9926 }
9927 }
9928#endif
9929 }
9930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932}
9933
Bram Moolenaar572cb562005-08-05 21:35:02 +00009934#if defined(FEAT_INS_EXPAND)
9935/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009936 * "complete()" function
9937 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009938 static void
9939f_complete(argvars, rettv)
9940 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009941 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009942{
9943 int startcol;
9944
9945 if ((State & INSERT) == 0)
9946 {
9947 EMSG(_("E785: complete() can only be used in Insert mode"));
9948 return;
9949 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009950
9951 /* Check for undo allowed here, because if something was already inserted
9952 * the line was already saved for undo and this check isn't done. */
9953 if (!undo_allowed())
9954 return;
9955
Bram Moolenaarade00832006-03-10 21:46:58 +00009956 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9957 {
9958 EMSG(_(e_invarg));
9959 return;
9960 }
9961
9962 startcol = get_tv_number_chk(&argvars[0], NULL);
9963 if (startcol <= 0)
9964 return;
9965
9966 set_completion(startcol - 1, argvars[1].vval.v_list);
9967}
9968
9969/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009970 * "complete_add()" function
9971 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009972 static void
9973f_complete_add(argvars, rettv)
9974 typval_T *argvars;
9975 typval_T *rettv;
9976{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009977 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009978}
9979
9980/*
9981 * "complete_check()" function
9982 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009983 static void
9984f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009985 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009986 typval_T *rettv;
9987{
9988 int saved = RedrawingDisabled;
9989
9990 RedrawingDisabled = 0;
9991 ins_compl_check_keys(0);
9992 rettv->vval.v_number = compl_interrupted;
9993 RedrawingDisabled = saved;
9994}
9995#endif
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997/*
9998 * "confirm(message, buttons[, default [, type]])" function
9999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010002 typval_T *argvars UNUSED;
10003 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10006 char_u *message;
10007 char_u *buttons = NULL;
10008 char_u buf[NUMBUFLEN];
10009 char_u buf2[NUMBUFLEN];
10010 int def = 1;
10011 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 char_u *typestr;
10013 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 message = get_tv_string_chk(&argvars[0]);
10016 if (message == NULL)
10017 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010018 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10021 if (buttons == NULL)
10022 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010026 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10029 if (typestr == NULL)
10030 error = TRUE;
10031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 switch (TOUPPER_ASC(*typestr))
10034 {
10035 case 'E': type = VIM_ERROR; break;
10036 case 'Q': type = VIM_QUESTION; break;
10037 case 'I': type = VIM_INFO; break;
10038 case 'W': type = VIM_WARNING; break;
10039 case 'G': type = VIM_GENERIC; break;
10040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 }
10042 }
10043 }
10044 }
10045
10046 if (buttons == NULL || *buttons == NUL)
10047 buttons = (char_u *)_("&Ok");
10048
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010049 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010051 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053}
10054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010055/*
10056 * "copy()" function
10057 */
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010062{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010063 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010064}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010066#ifdef FEAT_FLOAT
10067/*
10068 * "cos()" function
10069 */
10070 static void
10071f_cos(argvars, rettv)
10072 typval_T *argvars;
10073 typval_T *rettv;
10074{
10075 float_T f;
10076
10077 rettv->v_type = VAR_FLOAT;
10078 if (get_float_arg(argvars, &f) == OK)
10079 rettv->vval.v_float = cos(f);
10080 else
10081 rettv->vval.v_float = 0.0;
10082}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010083
10084/*
10085 * "cosh()" function
10086 */
10087 static void
10088f_cosh(argvars, rettv)
10089 typval_T *argvars;
10090 typval_T *rettv;
10091{
10092 float_T f;
10093
10094 rettv->v_type = VAR_FLOAT;
10095 if (get_float_arg(argvars, &f) == OK)
10096 rettv->vval.v_float = cosh(f);
10097 else
10098 rettv->vval.v_float = 0.0;
10099}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010100#endif
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010103 * "count()" function
10104 */
10105 static void
10106f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010107 typval_T *argvars;
10108 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010109{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010110 long n = 0;
10111 int ic = FALSE;
10112
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010114 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 listitem_T *li;
10116 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010117 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010118
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 if ((l = argvars[0].vval.v_list) != NULL)
10120 {
10121 li = l->lv_first;
10122 if (argvars[2].v_type != VAR_UNKNOWN)
10123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 int error = FALSE;
10125
10126 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010127 if (argvars[3].v_type != VAR_UNKNOWN)
10128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 idx = get_tv_number_chk(&argvars[3], &error);
10130 if (!error)
10131 {
10132 li = list_find(l, idx);
10133 if (li == NULL)
10134 EMSGN(_(e_listidx), idx);
10135 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010136 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 if (error)
10138 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 }
10140
10141 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010142 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 ++n;
10144 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010145 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010146 else if (argvars[0].v_type == VAR_DICT)
10147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010148 int todo;
10149 dict_T *d;
10150 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010151
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010152 if ((d = argvars[0].vval.v_dict) != NULL)
10153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 int error = FALSE;
10155
Bram Moolenaare9a41262005-01-15 22:18:47 +000010156 if (argvars[2].v_type != VAR_UNKNOWN)
10157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 if (argvars[3].v_type != VAR_UNKNOWN)
10160 EMSG(_(e_invarg));
10161 }
10162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010163 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010164 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010165 {
10166 if (!HASHITEM_EMPTY(hi))
10167 {
10168 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010169 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010170 ++n;
10171 }
10172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 }
10174 }
10175 else
10176 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010177 rettv->vval.v_number = n;
10178}
10179
10180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10182 *
10183 * Checks the existence of a cscope connection.
10184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010187 typval_T *argvars UNUSED;
10188 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190#ifdef FEAT_CSCOPE
10191 int num = 0;
10192 char_u *dbpath = NULL;
10193 char_u *prepend = NULL;
10194 char_u buf[NUMBUFLEN];
10195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010196 if (argvars[0].v_type != VAR_UNKNOWN
10197 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 num = (int)get_tv_number(&argvars[0]);
10200 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010201 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010202 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 }
10204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206#endif
10207}
10208
10209/*
10210 * "cursor(lnum, col)" function
10211 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010212 * Moves the cursor to the specified line and column.
10213 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 typval_T *argvars;
10218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010221#ifdef FEAT_VIRTUALEDIT
10222 long coladd = 0;
10223#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010224 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010226 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010227 if (argvars[1].v_type == VAR_UNKNOWN)
10228 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010229 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010230 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010231
Bram Moolenaar493c1782014-05-28 14:34:46 +020010232 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010233 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010234 line = pos.lnum;
10235 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010236#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010237 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010238#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010239 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010240 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010241 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010242 set_curswant = FALSE;
10243 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010244 }
10245 else
10246 {
10247 line = get_tv_lnum(argvars);
10248 col = get_tv_number_chk(&argvars[1], NULL);
10249#ifdef FEAT_VIRTUALEDIT
10250 if (argvars[2].v_type != VAR_UNKNOWN)
10251 coladd = get_tv_number_chk(&argvars[2], NULL);
10252#endif
10253 }
10254 if (line < 0 || col < 0
10255#ifdef FEAT_VIRTUALEDIT
10256 || coladd < 0
10257#endif
10258 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (line > 0)
10261 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (col > 0)
10263 curwin->w_cursor.col = col - 1;
10264#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010265 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266#endif
10267
10268 /* Make sure the cursor is in a valid position. */
10269 check_cursor();
10270#ifdef FEAT_MBYTE
10271 /* Correct cursor for multi-byte character. */
10272 if (has_mbyte)
10273 mb_adjust_cursor();
10274#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010275
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010276 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010277 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278}
10279
10280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010281 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 */
10283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010285 typval_T *argvars;
10286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010288 int noref = 0;
10289
10290 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010292 if (noref < 0 || noref > 1)
10293 EMSG(_(e_invarg));
10294 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010295 {
10296 current_copyID += COPYID_INC;
10297 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299}
10300
10301/*
10302 * "delete()" function
10303 */
10304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010306 typval_T *argvars;
10307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
10309 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313}
10314
10315/*
10316 * "did_filetype()" function
10317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010320 typval_T *argvars UNUSED;
10321 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322{
10323#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325#endif
10326}
10327
10328/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010329 * "diff_filler()" function
10330 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010333 typval_T *argvars UNUSED;
10334 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010335{
10336#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010338#endif
10339}
10340
10341/*
10342 * "diff_hlID()" function
10343 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010346 typval_T *argvars UNUSED;
10347 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010348{
10349#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010351 static linenr_T prev_lnum = 0;
10352 static int changedtick = 0;
10353 static int fnum = 0;
10354 static int change_start = 0;
10355 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010356 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010357 int filler_lines;
10358 int col;
10359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010360 if (lnum < 0) /* ignore type error in {lnum} arg */
10361 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010362 if (lnum != prev_lnum
10363 || changedtick != curbuf->b_changedtick
10364 || fnum != curbuf->b_fnum)
10365 {
10366 /* New line, buffer, change: need to get the values. */
10367 filler_lines = diff_check(curwin, lnum);
10368 if (filler_lines < 0)
10369 {
10370 if (filler_lines == -1)
10371 {
10372 change_start = MAXCOL;
10373 change_end = -1;
10374 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10375 hlID = HLF_ADD; /* added line */
10376 else
10377 hlID = HLF_CHD; /* changed line */
10378 }
10379 else
10380 hlID = HLF_ADD; /* added line */
10381 }
10382 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010383 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010384 prev_lnum = lnum;
10385 changedtick = curbuf->b_changedtick;
10386 fnum = curbuf->b_fnum;
10387 }
10388
10389 if (hlID == HLF_CHD || hlID == HLF_TXD)
10390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010391 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010392 if (col >= change_start && col <= change_end)
10393 hlID = HLF_TXD; /* changed text */
10394 else
10395 hlID = HLF_CHD; /* changed line */
10396 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010397 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010398#endif
10399}
10400
10401/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010402 * "empty({expr})" function
10403 */
10404 static void
10405f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010408{
10409 int n;
10410
10411 switch (argvars[0].v_type)
10412 {
10413 case VAR_STRING:
10414 case VAR_FUNC:
10415 n = argvars[0].vval.v_string == NULL
10416 || *argvars[0].vval.v_string == NUL;
10417 break;
10418 case VAR_NUMBER:
10419 n = argvars[0].vval.v_number == 0;
10420 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010421#ifdef FEAT_FLOAT
10422 case VAR_FLOAT:
10423 n = argvars[0].vval.v_float == 0.0;
10424 break;
10425#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010426 case VAR_LIST:
10427 n = argvars[0].vval.v_list == NULL
10428 || argvars[0].vval.v_list->lv_first == NULL;
10429 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 case VAR_DICT:
10431 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010432 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010433 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010434 default:
10435 EMSG2(_(e_intern2), "f_empty()");
10436 n = 0;
10437 }
10438
10439 rettv->vval.v_number = n;
10440}
10441
10442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * "escape({string}, {chars})" function
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
10450 char_u buf[NUMBUFLEN];
10451
Bram Moolenaar758711c2005-02-02 23:11:38 +000010452 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10453 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455}
10456
10457/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010458 * "eval()" function
10459 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010460 static void
10461f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010462 typval_T *argvars;
10463 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010464{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010465 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 s = get_tv_string_chk(&argvars[0]);
10468 if (s != NULL)
10469 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010470
Bram Moolenaar615b9972015-01-14 17:15:05 +010010471 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10473 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010474 if (p != NULL && !aborting())
10475 EMSG2(_(e_invexpr2), p);
10476 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010477 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010478 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010480 else if (*s != NUL)
10481 EMSG(_(e_trailing));
10482}
10483
10484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 * "eventhandler()" function
10486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493}
10494
10495/*
10496 * "executable()" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010503 char_u *name = get_tv_string(&argvars[0]);
10504
10505 /* Check in $PATH and also check directly if there is a directory name. */
10506 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10507 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010508}
10509
10510/*
10511 * "exepath()" function
10512 */
10513 static void
10514f_exepath(argvars, rettv)
10515 typval_T *argvars;
10516 typval_T *rettv;
10517{
10518 char_u *p = NULL;
10519
Bram Moolenaarb5971142015-03-21 17:32:19 +010010520 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010521 rettv->v_type = VAR_STRING;
10522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523}
10524
10525/*
10526 * "exists()" function
10527 */
10528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 char_u *p;
10534 char_u *name;
10535 int n = FALSE;
10536 int len = 0;
10537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (*p == '$') /* environment variable */
10540 {
10541 /* first try "normal" environment variables (fast) */
10542 if (mch_getenv(p + 1) != NULL)
10543 n = TRUE;
10544 else
10545 {
10546 /* try expanding things like $VIM and ${HOME} */
10547 p = expand_env_save(p);
10548 if (p != NULL && *p != '$')
10549 n = TRUE;
10550 vim_free(p);
10551 }
10552 }
10553 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010556 if (*skipwhite(p) != NUL)
10557 n = FALSE; /* trailing garbage */
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 else if (*p == '*') /* internal or user defined function */
10560 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010561 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 }
10563 else if (*p == ':')
10564 {
10565 n = cmd_exists(p + 1);
10566 }
10567 else if (*p == '#')
10568 {
10569#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010570 if (p[1] == '#')
10571 n = autocmd_supported(p + 2);
10572 else
10573 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#endif
10575 }
10576 else /* internal variable */
10577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010578 char_u *tofree;
10579 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010581 /* get_name_len() takes care of expanding curly braces */
10582 name = p;
10583 len = get_name_len(&p, &tofree, TRUE, FALSE);
10584 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010586 if (tofree != NULL)
10587 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010588 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010589 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010591 /* handle d.key, l[idx], f(expr) */
10592 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10593 if (n)
10594 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 }
10596 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010597 if (*p != NUL)
10598 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010600 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 }
10602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010606#ifdef FEAT_FLOAT
10607/*
10608 * "exp()" function
10609 */
10610 static void
10611f_exp(argvars, rettv)
10612 typval_T *argvars;
10613 typval_T *rettv;
10614{
10615 float_T f;
10616
10617 rettv->v_type = VAR_FLOAT;
10618 if (get_float_arg(argvars, &f) == OK)
10619 rettv->vval.v_float = exp(f);
10620 else
10621 rettv->vval.v_float = 0.0;
10622}
10623#endif
10624
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625/*
10626 * "expand()" function
10627 */
10628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 char_u *s;
10634 int len;
10635 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010636 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010639 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010642 if (argvars[1].v_type != VAR_UNKNOWN
10643 && argvars[2].v_type != VAR_UNKNOWN
10644 && get_tv_number_chk(&argvars[2], &error)
10645 && !error)
10646 {
10647 rettv->v_type = VAR_LIST;
10648 rettv->vval.v_list = NULL;
10649 }
10650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 if (*s == '%' || *s == '#' || *s == '<')
10653 {
10654 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010655 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010657 if (rettv->v_type == VAR_LIST)
10658 {
10659 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10660 list_append_string(rettv->vval.v_list, result, -1);
10661 else
10662 vim_free(result);
10663 }
10664 else
10665 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667 else
10668 {
10669 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010670 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 if (argvars[1].v_type != VAR_UNKNOWN
10672 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010673 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 if (!error)
10675 {
10676 ExpandInit(&xpc);
10677 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010678 if (p_wic)
10679 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010680 if (rettv->v_type == VAR_STRING)
10681 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10682 options, WILD_ALL);
10683 else if (rettv_list_alloc(rettv) != FAIL)
10684 {
10685 int i;
10686
10687 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10688 for (i = 0; i < xpc.xp_numfiles; i++)
10689 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10690 ExpandCleanup(&xpc);
10691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 }
10693 else
10694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 }
10696}
10697
10698/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010699 * Go over all entries in "d2" and add them to "d1".
10700 * When "action" is "error" then a duplicate key is an error.
10701 * When "action" is "force" then a duplicate key is overwritten.
10702 * Otherwise duplicate keys are ignored ("action" is "keep").
10703 */
10704 void
10705dict_extend(d1, d2, action)
10706 dict_T *d1;
10707 dict_T *d2;
10708 char_u *action;
10709{
10710 dictitem_T *di1;
10711 hashitem_T *hi2;
10712 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010713 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010714
10715 todo = (int)d2->dv_hashtab.ht_used;
10716 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10717 {
10718 if (!HASHITEM_EMPTY(hi2))
10719 {
10720 --todo;
10721 di1 = dict_find(d1, hi2->hi_key, -1);
10722 if (d1->dv_scope != 0)
10723 {
10724 /* Disallow replacing a builtin function in l: and g:.
10725 * Check the key to be valid when adding to any
10726 * scope. */
10727 if (d1->dv_scope == VAR_DEF_SCOPE
10728 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10729 && var_check_func_name(hi2->hi_key,
10730 di1 == NULL))
10731 break;
10732 if (!valid_varname(hi2->hi_key))
10733 break;
10734 }
10735 if (di1 == NULL)
10736 {
10737 di1 = dictitem_copy(HI2DI(hi2));
10738 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10739 dictitem_free(di1);
10740 }
10741 else if (*action == 'e')
10742 {
10743 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10744 break;
10745 }
10746 else if (*action == 'f' && HI2DI(hi2) != di1)
10747 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010748 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10749 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010750 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010751 clear_tv(&di1->di_tv);
10752 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10753 }
10754 }
10755 }
10756}
10757
10758/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010759 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010761 */
10762 static void
10763f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010764 typval_T *argvars;
10765 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010766{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010767 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010768
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010770 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 list_T *l1, *l2;
10772 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 l1 = argvars[0].vval.v_list;
10777 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010778 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010779 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010780 {
10781 if (argvars[2].v_type != VAR_UNKNOWN)
10782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 before = get_tv_number_chk(&argvars[2], &error);
10784 if (error)
10785 return; /* type error; errmsg already given */
10786
Bram Moolenaar758711c2005-02-02 23:11:38 +000010787 if (before == l1->lv_len)
10788 item = NULL;
10789 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010790 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010791 item = list_find(l1, before);
10792 if (item == NULL)
10793 {
10794 EMSGN(_(e_listidx), before);
10795 return;
10796 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 }
10798 }
10799 else
10800 item = NULL;
10801 list_extend(l1, l2, item);
10802
Bram Moolenaare9a41262005-01-15 22:18:47 +000010803 copy_tv(&argvars[0], rettv);
10804 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10807 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010808 dict_T *d1, *d2;
10809 char_u *action;
10810 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811
10812 d1 = argvars[0].vval.v_dict;
10813 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010814 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010815 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 {
10817 /* Check the third argument. */
10818 if (argvars[2].v_type != VAR_UNKNOWN)
10819 {
10820 static char *(av[]) = {"keep", "force", "error"};
10821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 action = get_tv_string_chk(&argvars[2]);
10823 if (action == NULL)
10824 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010825 for (i = 0; i < 3; ++i)
10826 if (STRCMP(action, av[i]) == 0)
10827 break;
10828 if (i == 3)
10829 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010830 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 return;
10832 }
10833 }
10834 else
10835 action = (char_u *)"force";
10836
Bram Moolenaara9922d62013-05-30 13:01:18 +020010837 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 copy_tv(&argvars[0], rettv);
10840 }
10841 }
10842 else
10843 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010844}
10845
10846/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010847 * "feedkeys()" function
10848 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010849 static void
10850f_feedkeys(argvars, rettv)
10851 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010852 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010853{
10854 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010855 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010856 char_u *keys, *flags;
10857 char_u nbuf[NUMBUFLEN];
10858 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010859 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010860
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010861 /* This is not allowed in the sandbox. If the commands would still be
10862 * executed in the sandbox it would be OK, but it probably happens later,
10863 * when "sandbox" is no longer set. */
10864 if (check_secure())
10865 return;
10866
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010867 keys = get_tv_string(&argvars[0]);
10868 if (*keys != NUL)
10869 {
10870 if (argvars[1].v_type != VAR_UNKNOWN)
10871 {
10872 flags = get_tv_string_buf(&argvars[1], nbuf);
10873 for ( ; *flags != NUL; ++flags)
10874 {
10875 switch (*flags)
10876 {
10877 case 'n': remap = FALSE; break;
10878 case 'm': remap = TRUE; break;
10879 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010880 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010881 }
10882 }
10883 }
10884
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010885 /* Need to escape K_SPECIAL and CSI before putting the string in the
10886 * typeahead buffer. */
10887 keys_esc = vim_strsave_escape_csi(keys);
10888 if (keys_esc != NULL)
10889 {
10890 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010891 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010892 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010893 if (vgetc_busy)
10894 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010895 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010896 }
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "filereadable()" function
10901 */
10902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *argvars;
10905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010907 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 char_u *p;
10909 int n;
10910
Bram Moolenaarc236c162008-07-13 17:41:49 +000010911#ifndef O_NONBLOCK
10912# define O_NONBLOCK 0
10913#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010915 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10916 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 {
10918 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010919 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 }
10921 else
10922 n = FALSE;
10923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925}
10926
10927/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010928 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 * rights to write into.
10930 */
10931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010936 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937}
10938
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010939static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010940
10941 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010942findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010945 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010946{
10947#ifdef FEAT_SEARCHPATH
10948 char_u *fname;
10949 char_u *fresult = NULL;
10950 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10951 char_u *p;
10952 char_u pathbuf[NUMBUFLEN];
10953 int count = 1;
10954 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010955 int error = FALSE;
10956#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010957
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010958 rettv->vval.v_string = NULL;
10959 rettv->v_type = VAR_STRING;
10960
10961#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010962 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010963
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10967 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010968 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 else
10970 {
10971 if (*p != NUL)
10972 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010975 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010977 }
10978
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010979 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10980 error = TRUE;
10981
10982 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984 do
10985 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010986 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010987 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 fresult = find_file_in_path_option(first ? fname : NULL,
10989 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010990 0, first, path,
10991 find_what,
10992 curbuf->b_ffname,
10993 find_what == FINDFILE_DIR
10994 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010995 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010996
10997 if (fresult != NULL && rettv->v_type == VAR_LIST)
10998 list_append_string(rettv->vval.v_list, fresult, -1);
10999
11000 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011002
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011003 if (rettv->v_type == VAR_STRING)
11004 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011005#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011006}
11007
Bram Moolenaar33570922005-01-25 22:26:29 +000011008static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11009static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011010
11011/*
11012 * Implementation of map() and filter().
11013 */
11014 static void
11015filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *argvars;
11017 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011018 int map;
11019{
11020 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011021 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011022 listitem_T *li, *nli;
11023 list_T *l = NULL;
11024 dictitem_T *di;
11025 hashtab_T *ht;
11026 hashitem_T *hi;
11027 dict_T *d = NULL;
11028 typval_T save_val;
11029 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011031 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011032 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011033 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011034 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011035 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011036 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011037
Bram Moolenaare9a41262005-01-15 22:18:47 +000011038 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011039 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011040 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011041 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011042 return;
11043 }
11044 else if (argvars[0].v_type == VAR_DICT)
11045 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011046 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011047 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011048 return;
11049 }
11050 else
11051 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011052 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011053 return;
11054 }
11055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 expr = get_tv_string_buf_chk(&argvars[1], buf);
11057 /* On type errors, the preceding call has already displayed an error
11058 * message. Avoid a misleading error message for an empty string that
11059 * was not passed as argument. */
11060 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011062 prepare_vimvar(VV_VAL, &save_val);
11063 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011064
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011065 /* We reset "did_emsg" to be able to detect whether an error
11066 * occurred during evaluation of the expression. */
11067 save_did_emsg = did_emsg;
11068 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011069
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011070 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 vimvars[VV_KEY].vv_type = VAR_STRING;
11074
11075 ht = &d->dv_hashtab;
11076 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011077 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 if (!HASHITEM_EMPTY(hi))
11081 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011082 int r;
11083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 --todo;
11085 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011086 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011087 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11088 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 break;
11090 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011091 r = filter_map_one(&di->di_tv, expr, map, &rem);
11092 clear_tv(&vimvars[VV_KEY].vv_tv);
11093 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 break;
11095 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011096 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011097 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11098 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011099 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 }
11103 }
11104 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 }
11106 else
11107 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011108 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 for (li = l->lv_first; li != NULL; li = nli)
11111 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011112 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011113 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011115 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011116 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011117 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011118 break;
11119 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011121 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011123 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011124
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011125 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011127
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011128 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130
11131 copy_tv(&argvars[0], rettv);
11132}
11133
11134 static int
11135filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011137 char_u *expr;
11138 int map;
11139 int *remp;
11140{
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011143 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011144
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 s = expr;
11147 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011148 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011149 if (*s != NUL) /* check for trailing chars after expr */
11150 {
11151 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011152 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011153 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 }
11155 if (map)
11156 {
11157 /* map(): replace the list item value */
11158 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011159 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 *tv = rettv;
11161 }
11162 else
11163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 int error = FALSE;
11165
Bram Moolenaare9a41262005-01-15 22:18:47 +000011166 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011168 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 /* On type error, nothing has been removed; return FAIL to stop the
11170 * loop. The error message was given by get_tv_number_chk(). */
11171 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011172 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011174 retval = OK;
11175theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011176 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011177 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011178}
11179
11180/*
11181 * "filter()" function
11182 */
11183 static void
11184f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *argvars;
11186 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187{
11188 filter_map(argvars, rettv, FALSE);
11189}
11190
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192 * "finddir({fname}[, {path}[, {count}]])" function
11193 */
11194 static void
11195f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *argvars;
11197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011198{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011199 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200}
11201
11202/*
11203 * "findfile({fname}[, {path}[, {count}]])" function
11204 */
11205 static void
11206f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011207 typval_T *argvars;
11208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011210 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211}
11212
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011213#ifdef FEAT_FLOAT
11214/*
11215 * "float2nr({float})" function
11216 */
11217 static void
11218f_float2nr(argvars, rettv)
11219 typval_T *argvars;
11220 typval_T *rettv;
11221{
11222 float_T f;
11223
11224 if (get_float_arg(argvars, &f) == OK)
11225 {
11226 if (f < -0x7fffffff)
11227 rettv->vval.v_number = -0x7fffffff;
11228 else if (f > 0x7fffffff)
11229 rettv->vval.v_number = 0x7fffffff;
11230 else
11231 rettv->vval.v_number = (varnumber_T)f;
11232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011233}
11234
11235/*
11236 * "floor({float})" function
11237 */
11238 static void
11239f_floor(argvars, rettv)
11240 typval_T *argvars;
11241 typval_T *rettv;
11242{
11243 float_T f;
11244
11245 rettv->v_type = VAR_FLOAT;
11246 if (get_float_arg(argvars, &f) == OK)
11247 rettv->vval.v_float = floor(f);
11248 else
11249 rettv->vval.v_float = 0.0;
11250}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011251
11252/*
11253 * "fmod()" function
11254 */
11255 static void
11256f_fmod(argvars, rettv)
11257 typval_T *argvars;
11258 typval_T *rettv;
11259{
11260 float_T fx, fy;
11261
11262 rettv->v_type = VAR_FLOAT;
11263 if (get_float_arg(argvars, &fx) == OK
11264 && get_float_arg(&argvars[1], &fy) == OK)
11265 rettv->vval.v_float = fmod(fx, fy);
11266 else
11267 rettv->vval.v_float = 0.0;
11268}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011269#endif
11270
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011272 * "fnameescape({string})" function
11273 */
11274 static void
11275f_fnameescape(argvars, rettv)
11276 typval_T *argvars;
11277 typval_T *rettv;
11278{
11279 rettv->vval.v_string = vim_strsave_fnameescape(
11280 get_tv_string(&argvars[0]), FALSE);
11281 rettv->v_type = VAR_STRING;
11282}
11283
11284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 * "fnamemodify({fname}, {mods})" function
11286 */
11287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 typval_T *argvars;
11290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
11292 char_u *fname;
11293 char_u *mods;
11294 int usedlen = 0;
11295 int len;
11296 char_u *fbuf = NULL;
11297 char_u buf[NUMBUFLEN];
11298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 fname = get_tv_string_chk(&argvars[0]);
11300 mods = get_tv_string_buf_chk(&argvars[1], buf);
11301 if (fname == NULL || mods == NULL)
11302 fname = NULL;
11303 else
11304 {
11305 len = (int)STRLEN(fname);
11306 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 vim_free(fbuf);
11315}
11316
Bram Moolenaar33570922005-01-25 22:26:29 +000011317static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318
11319/*
11320 * "foldclosed()" function
11321 */
11322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011324 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011325 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011326 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328#ifdef FEAT_FOLDING
11329 linenr_T lnum;
11330 linenr_T first, last;
11331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11334 {
11335 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11336 {
11337 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 return;
11342 }
11343 }
11344#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346}
11347
11348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349 * "foldclosed()" function
11350 */
11351 static void
11352f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011355{
11356 foldclosed_both(argvars, rettv, FALSE);
11357}
11358
11359/*
11360 * "foldclosedend()" function
11361 */
11362 static void
11363f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *argvars;
11365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366{
11367 foldclosed_both(argvars, rettv, TRUE);
11368}
11369
11370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 * "foldlevel()" function
11372 */
11373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011375 typval_T *argvars UNUSED;
11376 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378#ifdef FEAT_FOLDING
11379 linenr_T lnum;
11380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
11388 * "foldtext()" function
11389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011392 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394{
11395#ifdef FEAT_FOLDING
11396 linenr_T lnum;
11397 char_u *s;
11398 char_u *r;
11399 int len;
11400 char *txt;
11401#endif
11402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->v_type = VAR_STRING;
11404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011406 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11407 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11408 <= curbuf->b_ml.ml_line_count
11409 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 {
11411 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11413 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 {
11415 if (!linewhite(lnum))
11416 break;
11417 ++lnum;
11418 }
11419
11420 /* Find interesting text in this line. */
11421 s = skipwhite(ml_get(lnum));
11422 /* skip C comment-start */
11423 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011426 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011427 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011428 {
11429 s = skipwhite(ml_get(lnum + 1));
11430 if (*s == '*')
11431 s = skipwhite(s + 1);
11432 }
11433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 txt = _("+-%s%3ld lines: ");
11435 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011436 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 + 20 /* for %3ld */
11438 + STRLEN(s))); /* concatenated */
11439 if (r != NULL)
11440 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011441 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11442 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11443 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 len = (int)STRLEN(r);
11445 STRCAT(r, s);
11446 /* remove 'foldmarker' and 'commentstring' */
11447 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450 }
11451#endif
11452}
11453
11454/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011455 * "foldtextresult(lnum)" function
11456 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011459 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011461{
11462#ifdef FEAT_FOLDING
11463 linenr_T lnum;
11464 char_u *text;
11465 char_u buf[51];
11466 foldinfo_T foldinfo;
11467 int fold_count;
11468#endif
11469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->v_type = VAR_STRING;
11471 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011472#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 /* treat illegal types and illegal string values for {lnum} the same */
11475 if (lnum < 0)
11476 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011477 fold_count = foldedCount(curwin, lnum, &foldinfo);
11478 if (fold_count > 0)
11479 {
11480 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11481 &foldinfo, buf);
11482 if (text == buf)
11483 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011485 }
11486#endif
11487}
11488
11489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 * "foreground()" function
11491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011494 typval_T *argvars UNUSED;
11495 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497#ifdef FEAT_GUI
11498 if (gui.in_use)
11499 gui_mch_set_foreground();
11500#else
11501# ifdef WIN32
11502 win32_set_foreground();
11503# endif
11504#endif
11505}
11506
11507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 * "function()" function
11509 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *argvars;
11513 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011514{
11515 char_u *s;
11516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011519 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011520 /* Don't check an autoload name for existence here. */
11521 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011522 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011523 else
11524 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011525 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011526 {
11527 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011528 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011529
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011530 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11531 * also be called from another script. Using trans_function_name()
11532 * would also work, but some plugins depend on the name being
11533 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011534 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011535 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011536 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011537 if (rettv->vval.v_string != NULL)
11538 {
11539 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011540 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011541 }
11542 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011543 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011544 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 }
11547}
11548
11549/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011550 * "garbagecollect()" function
11551 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011552 static void
11553f_garbagecollect(argvars, rettv)
11554 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011555 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011556{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011557 /* This is postponed until we are back at the toplevel, because we may be
11558 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11559 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011560
11561 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11562 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011563}
11564
11565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011566 * "get()" function
11567 */
11568 static void
11569f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011570 typval_T *argvars;
11571 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572{
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 listitem_T *li;
11574 list_T *l;
11575 dictitem_T *di;
11576 dict_T *d;
11577 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578
Bram Moolenaare9a41262005-01-15 22:18:47 +000011579 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 int error = FALSE;
11584
11585 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11586 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 else if (argvars[0].v_type == VAR_DICT)
11591 {
11592 if ((d = argvars[0].vval.v_dict) != NULL)
11593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011594 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 if (di != NULL)
11596 tv = &di->di_tv;
11597 }
11598 }
11599 else
11600 EMSG2(_(e_listdictarg), "get()");
11601
11602 if (tv == NULL)
11603 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011605 copy_tv(&argvars[2], rettv);
11606 }
11607 else
11608 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011609}
11610
Bram Moolenaar342337a2005-07-21 21:11:17 +000011611static 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 +000011612
11613/*
11614 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011615 * Return a range (from start to end) of lines in rettv from the specified
11616 * buffer.
11617 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011618 */
11619 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011620get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011621 buf_T *buf;
11622 linenr_T start;
11623 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011624 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011625 typval_T *rettv;
11626{
11627 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011628
Bram Moolenaar959a1432013-12-14 12:17:38 +010011629 rettv->v_type = VAR_STRING;
11630 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011631 if (retlist && rettv_list_alloc(rettv) == FAIL)
11632 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011633
11634 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11635 return;
11636
11637 if (!retlist)
11638 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011639 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11640 p = ml_get_buf(buf, start, FALSE);
11641 else
11642 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011643 rettv->vval.v_string = vim_strsave(p);
11644 }
11645 else
11646 {
11647 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011648 return;
11649
11650 if (start < 1)
11651 start = 1;
11652 if (end > buf->b_ml.ml_line_count)
11653 end = buf->b_ml.ml_line_count;
11654 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011655 if (list_append_string(rettv->vval.v_list,
11656 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011657 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011658 }
11659}
11660
11661/*
11662 * "getbufline()" function
11663 */
11664 static void
11665f_getbufline(argvars, rettv)
11666 typval_T *argvars;
11667 typval_T *rettv;
11668{
11669 linenr_T lnum;
11670 linenr_T end;
11671 buf_T *buf;
11672
11673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11674 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011675 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011676 --emsg_off;
11677
Bram Moolenaar661b1822005-07-28 22:36:45 +000011678 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011679 if (argvars[2].v_type == VAR_UNKNOWN)
11680 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011681 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011682 end = get_tv_lnum_buf(&argvars[2], buf);
11683
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011685}
11686
Bram Moolenaar0d660222005-01-07 21:51:51 +000011687/*
11688 * "getbufvar()" function
11689 */
11690 static void
11691f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694{
11695 buf_T *buf;
11696 buf_T *save_curbuf;
11697 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011698 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011699 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11702 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011704 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011705
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011706 rettv->v_type = VAR_STRING;
11707 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708
11709 if (buf != NULL && varname != NULL)
11710 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011711 /* set curbuf to be our buf, temporarily */
11712 save_curbuf = curbuf;
11713 curbuf = buf;
11714
Bram Moolenaar0d660222005-01-07 21:51:51 +000011715 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011716 {
11717 if (get_option_tv(&varname, rettv, TRUE) == OK)
11718 done = TRUE;
11719 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011720 else if (STRCMP(varname, "changedtick") == 0)
11721 {
11722 rettv->v_type = VAR_NUMBER;
11723 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011724 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011725 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011726 else
11727 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011728 /* Look up the variable. */
11729 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11730 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11731 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011732 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011733 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011735 done = TRUE;
11736 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011738
11739 /* restore previous notion of curbuf */
11740 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741 }
11742
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011743 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11744 /* use the default value */
11745 copy_tv(&argvars[2], rettv);
11746
Bram Moolenaar0d660222005-01-07 21:51:51 +000011747 --emsg_off;
11748}
11749
11750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 * "getchar()" function
11752 */
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757{
11758 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011761 /* Position the cursor. Needed after a message that ends in a space. */
11762 windgoto(msg_row, msg_col);
11763
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 ++no_mapping;
11765 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011766 for (;;)
11767 {
11768 if (argvars[0].v_type == VAR_UNKNOWN)
11769 /* getchar(): blocking wait. */
11770 n = safe_vgetc();
11771 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11772 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011773 n = vpeekc_any();
11774 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011775 /* illegal argument or getchar(0) and no char avail: return zero */
11776 n = 0;
11777 else
11778 /* getchar(0) and char avail: return char */
11779 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011780
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011781 if (n == K_IGNORE)
11782 continue;
11783 break;
11784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 --no_mapping;
11786 --allow_keys;
11787
Bram Moolenaar219b8702006-11-01 14:32:36 +000011788 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11789 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11790 vimvars[VV_MOUSE_COL].vv_nr = 0;
11791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 if (IS_SPECIAL(n) || mod_mask != 0)
11794 {
11795 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11796 int i = 0;
11797
11798 /* Turn a special key into three bytes, plus modifier. */
11799 if (mod_mask != 0)
11800 {
11801 temp[i++] = K_SPECIAL;
11802 temp[i++] = KS_MODIFIER;
11803 temp[i++] = mod_mask;
11804 }
11805 if (IS_SPECIAL(n))
11806 {
11807 temp[i++] = K_SPECIAL;
11808 temp[i++] = K_SECOND(n);
11809 temp[i++] = K_THIRD(n);
11810 }
11811#ifdef FEAT_MBYTE
11812 else if (has_mbyte)
11813 i += (*mb_char2bytes)(n, temp + i);
11814#endif
11815 else
11816 temp[i++] = n;
11817 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->v_type = VAR_STRING;
11819 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011820
11821#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011822 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011823 {
11824 int row = mouse_row;
11825 int col = mouse_col;
11826 win_T *win;
11827 linenr_T lnum;
11828# ifdef FEAT_WINDOWS
11829 win_T *wp;
11830# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011831 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011832
11833 if (row >= 0 && col >= 0)
11834 {
11835 /* Find the window at the mouse coordinates and compute the
11836 * text position. */
11837 win = mouse_find_win(&row, &col);
11838 (void)mouse_comp_pos(win, &row, &col, &lnum);
11839# ifdef FEAT_WINDOWS
11840 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011841 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011842# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011843 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011844 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11845 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11846 }
11847 }
11848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 }
11850}
11851
11852/*
11853 * "getcharmod()" function
11854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861}
11862
11863/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011864 * "getcharsearch()" function
11865 */
11866 static void
11867f_getcharsearch(argvars, rettv)
11868 typval_T *argvars UNUSED;
11869 typval_T *rettv;
11870{
11871 if (rettv_dict_alloc(rettv) != FAIL)
11872 {
11873 dict_T *dict = rettv->vval.v_dict;
11874
11875 dict_add_nr_str(dict, "char", 0L, last_csearch());
11876 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11877 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11878 }
11879}
11880
11881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 * "getcmdline()" function
11883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 rettv->v_type = VAR_STRING;
11890 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891}
11892
11893/*
11894 * "getcmdpos()" function
11895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902}
11903
11904/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011905 * "getcmdtype()" function
11906 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011907 static void
11908f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011909 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011910 typval_T *rettv;
11911{
11912 rettv->v_type = VAR_STRING;
11913 rettv->vval.v_string = alloc(2);
11914 if (rettv->vval.v_string != NULL)
11915 {
11916 rettv->vval.v_string[0] = get_cmdline_type();
11917 rettv->vval.v_string[1] = NUL;
11918 }
11919}
11920
11921/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011922 * "getcmdwintype()" function
11923 */
11924 static void
11925f_getcmdwintype(argvars, rettv)
11926 typval_T *argvars UNUSED;
11927 typval_T *rettv;
11928{
11929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = NULL;
11931#ifdef FEAT_CMDWIN
11932 rettv->vval.v_string = alloc(2);
11933 if (rettv->vval.v_string != NULL)
11934 {
11935 rettv->vval.v_string[0] = cmdwin_type;
11936 rettv->vval.v_string[1] = NUL;
11937 }
11938#endif
11939}
11940
11941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 * "getcwd()" function
11943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011946 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011949 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011952 rettv->vval.v_string = NULL;
11953 cwd = alloc(MAXPATHL);
11954 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011956 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11957 {
11958 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011960 if (rettv->vval.v_string != NULL)
11961 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011963 }
11964 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 }
11966}
11967
11968/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011969 * "getfontname()" function
11970 */
11971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011973 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->v_type = VAR_STRING;
11977 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011978#ifdef FEAT_GUI
11979 if (gui.in_use)
11980 {
11981 GuiFont font;
11982 char_u *name = NULL;
11983
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011984 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011985 {
11986 /* Get the "Normal" font. Either the name saved by
11987 * hl_set_font_name() or from the font ID. */
11988 font = gui.norm_font;
11989 name = hl_get_font_name();
11990 }
11991 else
11992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011994 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11995 return;
11996 font = gui_mch_get_font(name, FALSE);
11997 if (font == NOFONT)
11998 return; /* Invalid font name, return empty string. */
11999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012001 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012002 gui_mch_free_font(font);
12003 }
12004#endif
12005}
12006
12007/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012008 * "getfperm({fname})" function
12009 */
12010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 typval_T *argvars;
12013 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012014{
12015 char_u *fname;
12016 struct stat st;
12017 char_u *perm = NULL;
12018 char_u flags[] = "rwx";
12019 int i;
12020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012024 if (mch_stat((char *)fname, &st) >= 0)
12025 {
12026 perm = vim_strsave((char_u *)"---------");
12027 if (perm != NULL)
12028 {
12029 for (i = 0; i < 9; i++)
12030 {
12031 if (st.st_mode & (1 << (8 - i)))
12032 perm[i] = flags[i % 3];
12033 }
12034 }
12035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012037}
12038
12039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 * "getfsize({fname})" function
12041 */
12042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
12047 char_u *fname;
12048 struct stat st;
12049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
12054 if (mch_stat((char *)fname, &st) >= 0)
12055 {
12056 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012061
12062 /* non-perfect check for overflow */
12063 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12064 rettv->vval.v_number = -2;
12065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 }
12067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069}
12070
12071/*
12072 * "getftime({fname})" function
12073 */
12074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012076 typval_T *argvars;
12077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078{
12079 char_u *fname;
12080 struct stat st;
12081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083
12084 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089
12090/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012091 * "getftype({fname})" function
12092 */
12093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 typval_T *argvars;
12096 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012097{
12098 char_u *fname;
12099 struct stat st;
12100 char_u *type = NULL;
12101 char *t;
12102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012106 if (mch_lstat((char *)fname, &st) >= 0)
12107 {
12108#ifdef S_ISREG
12109 if (S_ISREG(st.st_mode))
12110 t = "file";
12111 else if (S_ISDIR(st.st_mode))
12112 t = "dir";
12113# ifdef S_ISLNK
12114 else if (S_ISLNK(st.st_mode))
12115 t = "link";
12116# endif
12117# ifdef S_ISBLK
12118 else if (S_ISBLK(st.st_mode))
12119 t = "bdev";
12120# endif
12121# ifdef S_ISCHR
12122 else if (S_ISCHR(st.st_mode))
12123 t = "cdev";
12124# endif
12125# ifdef S_ISFIFO
12126 else if (S_ISFIFO(st.st_mode))
12127 t = "fifo";
12128# endif
12129# ifdef S_ISSOCK
12130 else if (S_ISSOCK(st.st_mode))
12131 t = "fifo";
12132# endif
12133 else
12134 t = "other";
12135#else
12136# ifdef S_IFMT
12137 switch (st.st_mode & S_IFMT)
12138 {
12139 case S_IFREG: t = "file"; break;
12140 case S_IFDIR: t = "dir"; break;
12141# ifdef S_IFLNK
12142 case S_IFLNK: t = "link"; break;
12143# endif
12144# ifdef S_IFBLK
12145 case S_IFBLK: t = "bdev"; break;
12146# endif
12147# ifdef S_IFCHR
12148 case S_IFCHR: t = "cdev"; break;
12149# endif
12150# ifdef S_IFIFO
12151 case S_IFIFO: t = "fifo"; break;
12152# endif
12153# ifdef S_IFSOCK
12154 case S_IFSOCK: t = "socket"; break;
12155# endif
12156 default: t = "other";
12157 }
12158# else
12159 if (mch_isdir(fname))
12160 t = "dir";
12161 else
12162 t = "file";
12163# endif
12164#endif
12165 type = vim_strsave((char_u *)t);
12166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012168}
12169
12170/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012171 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172 */
12173 static void
12174f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012175 typval_T *argvars;
12176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012177{
12178 linenr_T lnum;
12179 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012180 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012181
12182 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012183 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012184 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012185 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012186 retlist = FALSE;
12187 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012188 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012189 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012191 retlist = TRUE;
12192 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012193
Bram Moolenaar342337a2005-07-21 21:11:17 +000012194 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195}
12196
12197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012198 * "getmatches()" function
12199 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012200 static void
12201f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012202 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012203 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012204{
12205#ifdef FEAT_SEARCH_EXTRA
12206 dict_T *dict;
12207 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012208 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012209
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012210 if (rettv_list_alloc(rettv) == OK)
12211 {
12212 while (cur != NULL)
12213 {
12214 dict = dict_alloc();
12215 if (dict == NULL)
12216 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012217 if (cur->match.regprog == NULL)
12218 {
12219 /* match added with matchaddpos() */
12220 for (i = 0; i < MAXPOSMATCH; ++i)
12221 {
12222 llpos_T *llpos;
12223 char buf[6];
12224 list_T *l;
12225
12226 llpos = &cur->pos.pos[i];
12227 if (llpos->lnum == 0)
12228 break;
12229 l = list_alloc();
12230 if (l == NULL)
12231 break;
12232 list_append_number(l, (varnumber_T)llpos->lnum);
12233 if (llpos->col > 0)
12234 {
12235 list_append_number(l, (varnumber_T)llpos->col);
12236 list_append_number(l, (varnumber_T)llpos->len);
12237 }
12238 sprintf(buf, "pos%d", i + 1);
12239 dict_add_list(dict, buf, l);
12240 }
12241 }
12242 else
12243 {
12244 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12245 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012246 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012247 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12248 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012249# ifdef FEAT_CONCEAL
12250 if (cur->conceal_char)
12251 {
12252 char_u buf[MB_MAXBYTES + 1];
12253
12254 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12255 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12256 }
12257# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012258 list_append_dict(rettv->vval.v_list, dict);
12259 cur = cur->next;
12260 }
12261 }
12262#endif
12263}
12264
12265/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012266 * "getpid()" function
12267 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012268 static void
12269f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012270 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012271 typval_T *rettv;
12272{
12273 rettv->vval.v_number = mch_get_pid();
12274}
12275
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012276static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12277
12278/*
12279 * "getcurpos()" function
12280 */
12281 static void
12282f_getcurpos(argvars, rettv)
12283 typval_T *argvars;
12284 typval_T *rettv;
12285{
12286 getpos_both(argvars, rettv, TRUE);
12287}
12288
Bram Moolenaar18081e32008-02-20 19:11:07 +000012289/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012290 * "getpos(string)" function
12291 */
12292 static void
12293f_getpos(argvars, rettv)
12294 typval_T *argvars;
12295 typval_T *rettv;
12296{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012297 getpos_both(argvars, rettv, FALSE);
12298}
12299
12300 static void
12301getpos_both(argvars, rettv, getcurpos)
12302 typval_T *argvars;
12303 typval_T *rettv;
12304 int getcurpos;
12305{
Bram Moolenaara5525202006-03-02 22:52:09 +000012306 pos_T *fp;
12307 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012308 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012309
12310 if (rettv_list_alloc(rettv) == OK)
12311 {
12312 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012313 if (getcurpos)
12314 fp = &curwin->w_cursor;
12315 else
12316 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012317 if (fnum != -1)
12318 list_append_number(l, (varnumber_T)fnum);
12319 else
12320 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012321 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12322 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012323 list_append_number(l, (fp != NULL)
12324 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012325 : (varnumber_T)0);
12326 list_append_number(l,
12327#ifdef FEAT_VIRTUALEDIT
12328 (fp != NULL) ? (varnumber_T)fp->coladd :
12329#endif
12330 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012331 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012332 list_append_number(l, curwin->w_curswant == MAXCOL ?
12333 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012334 }
12335 else
12336 rettv->vval.v_number = FALSE;
12337}
12338
12339/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012340 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012341 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012342 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012343f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012344 typval_T *argvars UNUSED;
12345 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012346{
12347#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012348 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012349#endif
12350
Bram Moolenaar2641f772005-03-25 21:58:17 +000012351#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012352 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012353 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012354 wp = NULL;
12355 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12356 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012357 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012358 if (wp == NULL)
12359 return;
12360 }
12361
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012362 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012363 }
12364#endif
12365}
12366
12367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 * "getreg()" function
12369 */
12370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012372 typval_T *argvars;
12373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374{
12375 char_u *strregname;
12376 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012377 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012378 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012381 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 strregname = get_tv_string_chk(&argvars[0]);
12384 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012385 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012388 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12389 return_list = get_tv_number_chk(&argvars[2], &error);
12390 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012393 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012394
12395 if (error)
12396 return;
12397
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 regname = (strregname == NULL ? '"' : *strregname);
12399 if (regname == 0)
12400 regname = '"';
12401
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012402 if (return_list)
12403 {
12404 rettv->v_type = VAR_LIST;
12405 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12406 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012407 if (rettv->vval.v_list != NULL)
12408 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012409 }
12410 else
12411 {
12412 rettv->v_type = VAR_STRING;
12413 rettv->vval.v_string = get_reg_contents(regname,
12414 arg2 ? GREG_EXPR_SRC : 0);
12415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416}
12417
12418/*
12419 * "getregtype()" function
12420 */
12421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012423 typval_T *argvars;
12424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425{
12426 char_u *strregname;
12427 int regname;
12428 char_u buf[NUMBUFLEN + 2];
12429 long reglen = 0;
12430
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012431 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432 {
12433 strregname = get_tv_string_chk(&argvars[0]);
12434 if (strregname == NULL) /* type error; errmsg already given */
12435 {
12436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
12438 return;
12439 }
12440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 else
12442 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012443 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444
12445 regname = (strregname == NULL ? '"' : *strregname);
12446 if (regname == 0)
12447 regname = '"';
12448
12449 buf[0] = NUL;
12450 buf[1] = NUL;
12451 switch (get_reg_type(regname, &reglen))
12452 {
12453 case MLINE: buf[0] = 'V'; break;
12454 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 case MBLOCK:
12456 buf[0] = Ctrl_V;
12457 sprintf((char *)buf + 1, "%ld", reglen + 1);
12458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->v_type = VAR_STRING;
12461 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462}
12463
12464/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012465 * "gettabvar()" function
12466 */
12467 static void
12468f_gettabvar(argvars, rettv)
12469 typval_T *argvars;
12470 typval_T *rettv;
12471{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012472 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012473 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012474 dictitem_T *v;
12475 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012476 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012477
12478 rettv->v_type = VAR_STRING;
12479 rettv->vval.v_string = NULL;
12480
12481 varname = get_tv_string_chk(&argvars[1]);
12482 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12483 if (tp != NULL && varname != NULL)
12484 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012485 /* Set tp to be our tabpage, temporarily. Also set the window to the
12486 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012487 if (switch_win(&oldcurwin, &oldtabpage,
12488 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012489 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012490 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012491 /* look up the variable */
12492 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12493 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12494 if (v != NULL)
12495 {
12496 copy_tv(&v->di_tv, rettv);
12497 done = TRUE;
12498 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012499 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012500
12501 /* restore previous notion of curwin */
12502 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012503 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012504
12505 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012506 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012507}
12508
12509/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012510 * "gettabwinvar()" function
12511 */
12512 static void
12513f_gettabwinvar(argvars, rettv)
12514 typval_T *argvars;
12515 typval_T *rettv;
12516{
12517 getwinvar(argvars, rettv, 1);
12518}
12519
12520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 * "getwinposx()" function
12522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012525 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef FEAT_GUI
12530 if (gui.in_use)
12531 {
12532 int x, y;
12533
12534 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 }
12537#endif
12538}
12539
12540/*
12541 * "getwinposy()" function
12542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012544f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012545 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549#ifdef FEAT_GUI
12550 if (gui.in_use)
12551 {
12552 int x, y;
12553
12554 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 }
12557#endif
12558}
12559
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012560/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012561 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012562 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012563 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012564find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012565 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012566 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012567{
12568#ifdef FEAT_WINDOWS
12569 win_T *wp;
12570#endif
12571 int nr;
12572
12573 nr = get_tv_number_chk(vp, NULL);
12574
12575#ifdef FEAT_WINDOWS
12576 if (nr < 0)
12577 return NULL;
12578 if (nr == 0)
12579 return curwin;
12580
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012581 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12582 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012583 if (--nr <= 0)
12584 break;
12585 return wp;
12586#else
12587 if (nr == 0 || nr == 1)
12588 return curwin;
12589 return NULL;
12590#endif
12591}
12592
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593/*
12594 * "getwinvar()" function
12595 */
12596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012597f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012598 typval_T *argvars;
12599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012601 getwinvar(argvars, rettv, 0);
12602}
12603
12604/*
12605 * getwinvar() and gettabwinvar()
12606 */
12607 static void
12608getwinvar(argvars, rettv, off)
12609 typval_T *argvars;
12610 typval_T *rettv;
12611 int off; /* 1 for gettabwinvar() */
12612{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012613 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012615 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012616 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012618#ifdef FEAT_WINDOWS
12619 win_T *oldcurwin;
12620 tabpage_T *oldtabpage;
12621 int need_switch_win;
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012624#ifdef FEAT_WINDOWS
12625 if (off == 1)
12626 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12627 else
12628 tp = curtab;
12629#endif
12630 win = find_win_by_nr(&argvars[off], tp);
12631 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012634 rettv->v_type = VAR_STRING;
12635 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636
12637 if (win != NULL && varname != NULL)
12638 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012639#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012640 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012641 * otherwise the window is not valid. Only do this when needed,
12642 * autocommands get blocked. */
12643 need_switch_win = !(tp == curtab && win == curwin);
12644 if (!need_switch_win
12645 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12646#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012647 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012648 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012649 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012650 if (get_option_tv(&varname, rettv, 1) == OK)
12651 done = TRUE;
12652 }
12653 else
12654 {
12655 /* Look up the variable. */
12656 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12657 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12658 varname, FALSE);
12659 if (v != NULL)
12660 {
12661 copy_tv(&v->di_tv, rettv);
12662 done = TRUE;
12663 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012666
Bram Moolenaarba117c22015-09-29 16:53:22 +020012667#ifdef FEAT_WINDOWS
12668 if (need_switch_win)
12669 /* restore previous notion of curwin */
12670 restore_win(oldcurwin, oldtabpage, TRUE);
12671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 }
12673
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012674 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12675 /* use the default return value */
12676 copy_tv(&argvars[off + 2], rettv);
12677
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 --emsg_off;
12679}
12680
12681/*
12682 * "glob()" function
12683 */
12684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 typval_T *argvars;
12687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012689 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012691 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012693 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012694 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012696 if (argvars[1].v_type != VAR_UNKNOWN)
12697 {
12698 if (get_tv_number_chk(&argvars[1], &error))
12699 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012701 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012702 if (get_tv_number_chk(&argvars[2], &error))
12703 {
12704 rettv->v_type = VAR_LIST;
12705 rettv->vval.v_list = NULL;
12706 }
12707 if (argvars[3].v_type != VAR_UNKNOWN
12708 && get_tv_number_chk(&argvars[3], &error))
12709 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012710 }
12711 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012712 if (!error)
12713 {
12714 ExpandInit(&xpc);
12715 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012716 if (p_wic)
12717 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012718 if (rettv->v_type == VAR_STRING)
12719 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012720 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012721 else if (rettv_list_alloc(rettv) != FAIL)
12722 {
12723 int i;
12724
12725 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12726 NULL, options, WILD_ALL_KEEP);
12727 for (i = 0; i < xpc.xp_numfiles; i++)
12728 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12729
12730 ExpandCleanup(&xpc);
12731 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012732 }
12733 else
12734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735}
12736
12737/*
12738 * "globpath()" function
12739 */
12740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012742 typval_T *argvars;
12743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012745 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012748 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012749 garray_T ga;
12750 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012752 /* When the optional second argument is non-zero, don't remove matches
12753 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012755 if (argvars[2].v_type != VAR_UNKNOWN)
12756 {
12757 if (get_tv_number_chk(&argvars[2], &error))
12758 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012759 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012760 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012761 if (get_tv_number_chk(&argvars[3], &error))
12762 {
12763 rettv->v_type = VAR_LIST;
12764 rettv->vval.v_list = NULL;
12765 }
12766 if (argvars[4].v_type != VAR_UNKNOWN
12767 && get_tv_number_chk(&argvars[4], &error))
12768 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012769 }
12770 }
12771 if (file != NULL && !error)
12772 {
12773 ga_init2(&ga, (int)sizeof(char_u *), 10);
12774 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12775 if (rettv->v_type == VAR_STRING)
12776 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12777 else if (rettv_list_alloc(rettv) != FAIL)
12778 for (i = 0; i < ga.ga_len; ++i)
12779 list_append_string(rettv->vval.v_list,
12780 ((char_u **)(ga.ga_data))[i], -1);
12781 ga_clear_strings(&ga);
12782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785}
12786
12787/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012788 * "glob2regpat()" function
12789 */
12790 static void
12791f_glob2regpat(argvars, rettv)
12792 typval_T *argvars;
12793 typval_T *rettv;
12794{
12795 char_u *pat = get_tv_string_chk(&argvars[0]);
12796
12797 rettv->v_type = VAR_STRING;
12798 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12799}
12800
12801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 * "has()" function
12803 */
12804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808{
12809 int i;
12810 char_u *name;
12811 int n = FALSE;
12812 static char *(has_list[]) =
12813 {
12814#ifdef AMIGA
12815 "amiga",
12816# ifdef FEAT_ARP
12817 "arp",
12818# endif
12819#endif
12820#ifdef __BEOS__
12821 "beos",
12822#endif
12823#ifdef MSDOS
12824# ifdef DJGPP
12825 "dos32",
12826# else
12827 "dos16",
12828# endif
12829#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012830#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 "mac",
12832#endif
12833#if defined(MACOS_X_UNIX)
12834 "macunix",
12835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836#ifdef __QNX__
12837 "qnx",
12838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839#ifdef UNIX
12840 "unix",
12841#endif
12842#ifdef VMS
12843 "vms",
12844#endif
12845#ifdef WIN16
12846 "win16",
12847#endif
12848#ifdef WIN32
12849 "win32",
12850#endif
12851#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12852 "win32unix",
12853#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012854#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 "win64",
12856#endif
12857#ifdef EBCDIC
12858 "ebcdic",
12859#endif
12860#ifndef CASE_INSENSITIVE_FILENAME
12861 "fname_case",
12862#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012863#ifdef HAVE_ACL
12864 "acl",
12865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866#ifdef FEAT_ARABIC
12867 "arabic",
12868#endif
12869#ifdef FEAT_AUTOCMD
12870 "autocmd",
12871#endif
12872#ifdef FEAT_BEVAL
12873 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012874# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12875 "balloon_multiline",
12876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#endif
12878#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12879 "builtin_terms",
12880# ifdef ALL_BUILTIN_TCAPS
12881 "all_builtin_terms",
12882# endif
12883#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012884#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12885 || defined(FEAT_GUI_W32) \
12886 || defined(FEAT_GUI_MOTIF))
12887 "browsefilter",
12888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#ifdef FEAT_BYTEOFF
12890 "byte_offset",
12891#endif
12892#ifdef FEAT_CINDENT
12893 "cindent",
12894#endif
12895#ifdef FEAT_CLIENTSERVER
12896 "clientserver",
12897#endif
12898#ifdef FEAT_CLIPBOARD
12899 "clipboard",
12900#endif
12901#ifdef FEAT_CMDL_COMPL
12902 "cmdline_compl",
12903#endif
12904#ifdef FEAT_CMDHIST
12905 "cmdline_hist",
12906#endif
12907#ifdef FEAT_COMMENTS
12908 "comments",
12909#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012910#ifdef FEAT_CONCEAL
12911 "conceal",
12912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913#ifdef FEAT_CRYPT
12914 "cryptv",
12915#endif
12916#ifdef FEAT_CSCOPE
12917 "cscope",
12918#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012919#ifdef FEAT_CURSORBIND
12920 "cursorbind",
12921#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012922#ifdef CURSOR_SHAPE
12923 "cursorshape",
12924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925#ifdef DEBUG
12926 "debug",
12927#endif
12928#ifdef FEAT_CON_DIALOG
12929 "dialog_con",
12930#endif
12931#ifdef FEAT_GUI_DIALOG
12932 "dialog_gui",
12933#endif
12934#ifdef FEAT_DIFF
12935 "diff",
12936#endif
12937#ifdef FEAT_DIGRAPHS
12938 "digraphs",
12939#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012940#ifdef FEAT_DIRECTX
12941 "directx",
12942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943#ifdef FEAT_DND
12944 "dnd",
12945#endif
12946#ifdef FEAT_EMACS_TAGS
12947 "emacs_tags",
12948#endif
12949 "eval", /* always present, of course! */
12950#ifdef FEAT_EX_EXTRA
12951 "ex_extra",
12952#endif
12953#ifdef FEAT_SEARCH_EXTRA
12954 "extra_search",
12955#endif
12956#ifdef FEAT_FKMAP
12957 "farsi",
12958#endif
12959#ifdef FEAT_SEARCHPATH
12960 "file_in_path",
12961#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012962#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012963 "filterpipe",
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965#ifdef FEAT_FIND_ID
12966 "find_in_path",
12967#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012968#ifdef FEAT_FLOAT
12969 "float",
12970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971#ifdef FEAT_FOLDING
12972 "folding",
12973#endif
12974#ifdef FEAT_FOOTER
12975 "footer",
12976#endif
12977#if !defined(USE_SYSTEM) && defined(UNIX)
12978 "fork",
12979#endif
12980#ifdef FEAT_GETTEXT
12981 "gettext",
12982#endif
12983#ifdef FEAT_GUI
12984 "gui",
12985#endif
12986#ifdef FEAT_GUI_ATHENA
12987# ifdef FEAT_GUI_NEXTAW
12988 "gui_neXtaw",
12989# else
12990 "gui_athena",
12991# endif
12992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993#ifdef FEAT_GUI_GTK
12994 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012997#ifdef FEAT_GUI_GNOME
12998 "gui_gnome",
12999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000#ifdef FEAT_GUI_MAC
13001 "gui_mac",
13002#endif
13003#ifdef FEAT_GUI_MOTIF
13004 "gui_motif",
13005#endif
13006#ifdef FEAT_GUI_PHOTON
13007 "gui_photon",
13008#endif
13009#ifdef FEAT_GUI_W16
13010 "gui_win16",
13011#endif
13012#ifdef FEAT_GUI_W32
13013 "gui_win32",
13014#endif
13015#ifdef FEAT_HANGULIN
13016 "hangul_input",
13017#endif
13018#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13019 "iconv",
13020#endif
13021#ifdef FEAT_INS_EXPAND
13022 "insert_expand",
13023#endif
13024#ifdef FEAT_JUMPLIST
13025 "jumplist",
13026#endif
13027#ifdef FEAT_KEYMAP
13028 "keymap",
13029#endif
13030#ifdef FEAT_LANGMAP
13031 "langmap",
13032#endif
13033#ifdef FEAT_LIBCALL
13034 "libcall",
13035#endif
13036#ifdef FEAT_LINEBREAK
13037 "linebreak",
13038#endif
13039#ifdef FEAT_LISP
13040 "lispindent",
13041#endif
13042#ifdef FEAT_LISTCMDS
13043 "listcmds",
13044#endif
13045#ifdef FEAT_LOCALMAP
13046 "localmap",
13047#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013048#ifdef FEAT_LUA
13049# ifndef DYNAMIC_LUA
13050 "lua",
13051# endif
13052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053#ifdef FEAT_MENU
13054 "menu",
13055#endif
13056#ifdef FEAT_SESSION
13057 "mksession",
13058#endif
13059#ifdef FEAT_MODIFY_FNAME
13060 "modify_fname",
13061#endif
13062#ifdef FEAT_MOUSE
13063 "mouse",
13064#endif
13065#ifdef FEAT_MOUSESHAPE
13066 "mouseshape",
13067#endif
13068#if defined(UNIX) || defined(VMS)
13069# ifdef FEAT_MOUSE_DEC
13070 "mouse_dec",
13071# endif
13072# ifdef FEAT_MOUSE_GPM
13073 "mouse_gpm",
13074# endif
13075# ifdef FEAT_MOUSE_JSB
13076 "mouse_jsbterm",
13077# endif
13078# ifdef FEAT_MOUSE_NET
13079 "mouse_netterm",
13080# endif
13081# ifdef FEAT_MOUSE_PTERM
13082 "mouse_pterm",
13083# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013084# ifdef FEAT_MOUSE_SGR
13085 "mouse_sgr",
13086# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013087# ifdef FEAT_SYSMOUSE
13088 "mouse_sysmouse",
13089# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013090# ifdef FEAT_MOUSE_URXVT
13091 "mouse_urxvt",
13092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093# ifdef FEAT_MOUSE_XTERM
13094 "mouse_xterm",
13095# endif
13096#endif
13097#ifdef FEAT_MBYTE
13098 "multi_byte",
13099#endif
13100#ifdef FEAT_MBYTE_IME
13101 "multi_byte_ime",
13102#endif
13103#ifdef FEAT_MULTI_LANG
13104 "multi_lang",
13105#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013106#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013107#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013108 "mzscheme",
13109#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef FEAT_OLE
13112 "ole",
13113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#ifdef FEAT_PATH_EXTRA
13115 "path_extra",
13116#endif
13117#ifdef FEAT_PERL
13118#ifndef DYNAMIC_PERL
13119 "perl",
13120#endif
13121#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013122#ifdef FEAT_PERSISTENT_UNDO
13123 "persistent_undo",
13124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef FEAT_PYTHON
13126#ifndef DYNAMIC_PYTHON
13127 "python",
13128#endif
13129#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013130#ifdef FEAT_PYTHON3
13131#ifndef DYNAMIC_PYTHON3
13132 "python3",
13133#endif
13134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#ifdef FEAT_POSTSCRIPT
13136 "postscript",
13137#endif
13138#ifdef FEAT_PRINTER
13139 "printer",
13140#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013141#ifdef FEAT_PROFILE
13142 "profile",
13143#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013144#ifdef FEAT_RELTIME
13145 "reltime",
13146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147#ifdef FEAT_QUICKFIX
13148 "quickfix",
13149#endif
13150#ifdef FEAT_RIGHTLEFT
13151 "rightleft",
13152#endif
13153#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13154 "ruby",
13155#endif
13156#ifdef FEAT_SCROLLBIND
13157 "scrollbind",
13158#endif
13159#ifdef FEAT_CMDL_INFO
13160 "showcmd",
13161 "cmdline_info",
13162#endif
13163#ifdef FEAT_SIGNS
13164 "signs",
13165#endif
13166#ifdef FEAT_SMARTINDENT
13167 "smartindent",
13168#endif
13169#ifdef FEAT_SNIFF
13170 "sniff",
13171#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013172#ifdef STARTUPTIME
13173 "startuptime",
13174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175#ifdef FEAT_STL_OPT
13176 "statusline",
13177#endif
13178#ifdef FEAT_SUN_WORKSHOP
13179 "sun_workshop",
13180#endif
13181#ifdef FEAT_NETBEANS_INTG
13182 "netbeans_intg",
13183#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013184#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013185 "spell",
13186#endif
13187#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 "syntax",
13189#endif
13190#if defined(USE_SYSTEM) || !defined(UNIX)
13191 "system",
13192#endif
13193#ifdef FEAT_TAG_BINS
13194 "tag_binary",
13195#endif
13196#ifdef FEAT_TAG_OLDSTATIC
13197 "tag_old_static",
13198#endif
13199#ifdef FEAT_TAG_ANYWHITE
13200 "tag_any_white",
13201#endif
13202#ifdef FEAT_TCL
13203# ifndef DYNAMIC_TCL
13204 "tcl",
13205# endif
13206#endif
13207#ifdef TERMINFO
13208 "terminfo",
13209#endif
13210#ifdef FEAT_TERMRESPONSE
13211 "termresponse",
13212#endif
13213#ifdef FEAT_TEXTOBJ
13214 "textobjects",
13215#endif
13216#ifdef HAVE_TGETENT
13217 "tgetent",
13218#endif
13219#ifdef FEAT_TITLE
13220 "title",
13221#endif
13222#ifdef FEAT_TOOLBAR
13223 "toolbar",
13224#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013225#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13226 "unnamedplus",
13227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#ifdef FEAT_USR_CMDS
13229 "user-commands", /* was accidentally included in 5.4 */
13230 "user_commands",
13231#endif
13232#ifdef FEAT_VIMINFO
13233 "viminfo",
13234#endif
13235#ifdef FEAT_VERTSPLIT
13236 "vertsplit",
13237#endif
13238#ifdef FEAT_VIRTUALEDIT
13239 "virtualedit",
13240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef FEAT_VISUALEXTRA
13243 "visualextra",
13244#endif
13245#ifdef FEAT_VREPLACE
13246 "vreplace",
13247#endif
13248#ifdef FEAT_WILDIGN
13249 "wildignore",
13250#endif
13251#ifdef FEAT_WILDMENU
13252 "wildmenu",
13253#endif
13254#ifdef FEAT_WINDOWS
13255 "windows",
13256#endif
13257#ifdef FEAT_WAK
13258 "winaltkeys",
13259#endif
13260#ifdef FEAT_WRITEBACKUP
13261 "writebackup",
13262#endif
13263#ifdef FEAT_XIM
13264 "xim",
13265#endif
13266#ifdef FEAT_XFONTSET
13267 "xfontset",
13268#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013269#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013270 "xpm",
13271 "xpm_w32", /* for backward compatibility */
13272#else
13273# if defined(HAVE_XPM)
13274 "xpm",
13275# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277#ifdef USE_XSMP
13278 "xsmp",
13279#endif
13280#ifdef USE_XSMP_INTERACT
13281 "xsmp_interact",
13282#endif
13283#ifdef FEAT_XCLIPBOARD
13284 "xterm_clipboard",
13285#endif
13286#ifdef FEAT_XTERM_SAVE
13287 "xterm_save",
13288#endif
13289#if defined(UNIX) && defined(FEAT_X11)
13290 "X11",
13291#endif
13292 NULL
13293 };
13294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 for (i = 0; has_list[i] != NULL; ++i)
13297 if (STRICMP(name, has_list[i]) == 0)
13298 {
13299 n = TRUE;
13300 break;
13301 }
13302
13303 if (n == FALSE)
13304 {
13305 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013306 {
13307 if (name[5] == '-'
13308 && STRLEN(name) > 11
13309 && vim_isdigit(name[6])
13310 && vim_isdigit(name[8])
13311 && vim_isdigit(name[10]))
13312 {
13313 int major = atoi((char *)name + 6);
13314 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013315
13316 /* Expect "patch-9.9.01234". */
13317 n = (major < VIM_VERSION_MAJOR
13318 || (major == VIM_VERSION_MAJOR
13319 && (minor < VIM_VERSION_MINOR
13320 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013321 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013322 }
13323 else
13324 n = has_patch(atoi((char *)name + 5));
13325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 else if (STRICMP(name, "vim_starting") == 0)
13327 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013328#ifdef FEAT_MBYTE
13329 else if (STRICMP(name, "multi_byte_encoding") == 0)
13330 n = has_mbyte;
13331#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013332#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13333 else if (STRICMP(name, "balloon_multiline") == 0)
13334 n = multiline_balloon_available();
13335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#ifdef DYNAMIC_TCL
13337 else if (STRICMP(name, "tcl") == 0)
13338 n = tcl_enabled(FALSE);
13339#endif
13340#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13341 else if (STRICMP(name, "iconv") == 0)
13342 n = iconv_enabled(FALSE);
13343#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013344#ifdef DYNAMIC_LUA
13345 else if (STRICMP(name, "lua") == 0)
13346 n = lua_enabled(FALSE);
13347#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013348#ifdef DYNAMIC_MZSCHEME
13349 else if (STRICMP(name, "mzscheme") == 0)
13350 n = mzscheme_enabled(FALSE);
13351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352#ifdef DYNAMIC_RUBY
13353 else if (STRICMP(name, "ruby") == 0)
13354 n = ruby_enabled(FALSE);
13355#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013356#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357#ifdef DYNAMIC_PYTHON
13358 else if (STRICMP(name, "python") == 0)
13359 n = python_enabled(FALSE);
13360#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013361#endif
13362#ifdef FEAT_PYTHON3
13363#ifdef DYNAMIC_PYTHON3
13364 else if (STRICMP(name, "python3") == 0)
13365 n = python3_enabled(FALSE);
13366#endif
13367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368#ifdef DYNAMIC_PERL
13369 else if (STRICMP(name, "perl") == 0)
13370 n = perl_enabled(FALSE);
13371#endif
13372#ifdef FEAT_GUI
13373 else if (STRICMP(name, "gui_running") == 0)
13374 n = (gui.in_use || gui.starting);
13375# ifdef FEAT_GUI_W32
13376 else if (STRICMP(name, "gui_win32s") == 0)
13377 n = gui_is_win32s();
13378# endif
13379# ifdef FEAT_BROWSE
13380 else if (STRICMP(name, "browse") == 0)
13381 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13382# endif
13383#endif
13384#ifdef FEAT_SYN_HL
13385 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013386 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387#endif
13388#if defined(WIN3264)
13389 else if (STRICMP(name, "win95") == 0)
13390 n = mch_windows95();
13391#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013392#ifdef FEAT_NETBEANS_INTG
13393 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013394 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 }
13397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399}
13400
13401/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013402 * "has_key()" function
13403 */
13404 static void
13405f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *argvars;
13407 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013408{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013409 if (argvars[0].v_type != VAR_DICT)
13410 {
13411 EMSG(_(e_dictreq));
13412 return;
13413 }
13414 if (argvars[0].vval.v_dict == NULL)
13415 return;
13416
13417 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013418 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013419}
13420
13421/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013422 * "haslocaldir()" function
13423 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013424 static void
13425f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013426 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013427 typval_T *rettv;
13428{
13429 rettv->vval.v_number = (curwin->w_localdir != NULL);
13430}
13431
13432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 * "hasmapto()" function
13434 */
13435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
13440 char_u *name;
13441 char_u *mode;
13442 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013443 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013446 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 mode = (char_u *)"nvo";
13448 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013451 if (argvars[2].v_type != VAR_UNKNOWN)
13452 abbr = get_tv_number(&argvars[2]);
13453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454
Bram Moolenaar2c932302006-03-18 21:42:09 +000013455 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459}
13460
13461/*
13462 * "histadd()" function
13463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013466 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468{
13469#ifdef FEAT_CMDHIST
13470 int histype;
13471 char_u *str;
13472 char_u buf[NUMBUFLEN];
13473#endif
13474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 if (check_restricted() || check_secure())
13477 return;
13478#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13480 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 if (histype >= 0)
13482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 if (*str != NUL)
13485 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013486 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 return;
13490 }
13491 }
13492#endif
13493}
13494
13495/*
13496 * "histdel()" function
13497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013500 typval_T *argvars UNUSED;
13501 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
13503#ifdef FEAT_CMDHIST
13504 int n;
13505 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13509 if (str == NULL)
13510 n = 0;
13511 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013516 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 else
13519 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 get_tv_string_buf(&argvars[1], buf));
13522 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#endif
13524}
13525
13526/*
13527 * "histget()" function
13528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013531 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
13534#ifdef FEAT_CMDHIST
13535 int type;
13536 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13540 if (str == NULL)
13541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013543 {
13544 type = get_histtype(str);
13545 if (argvars[1].v_type == VAR_UNKNOWN)
13546 idx = get_history_idx(type);
13547 else
13548 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13549 /* -1 on type error */
13550 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
13558/*
13559 * "histnr()" function
13560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565{
13566 int i;
13567
13568#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 char_u *history = get_tv_string_chk(&argvars[0]);
13570
13571 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 if (i >= HIST_CMD && i < HIST_COUNT)
13573 i = get_history_idx(i);
13574 else
13575#endif
13576 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578}
13579
13580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 * "highlightID(name)" function
13582 */
13583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589}
13590
13591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 * "highlight_exists()" function
13593 */
13594 static void
13595f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013598{
13599 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13600}
13601
13602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 * "hostname()" function
13604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609{
13610 char_u hostname[256];
13611
13612 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 rettv->v_type = VAR_STRING;
13614 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
13618 * iconv() function
13619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013622 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
13625#ifdef FEAT_MBYTE
13626 char_u buf1[NUMBUFLEN];
13627 char_u buf2[NUMBUFLEN];
13628 char_u *from, *to, *str;
13629 vimconv_T vimconv;
13630#endif
13631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->v_type = VAR_STRING;
13633 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634
13635#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 str = get_tv_string(&argvars[0]);
13637 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13638 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 vimconv.vc_type = CONV_NONE;
13640 convert_setup(&vimconv, from, to);
13641
13642 /* If the encodings are equal, no conversion needed. */
13643 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647
13648 convert_setup(&vimconv, NULL, NULL);
13649 vim_free(from);
13650 vim_free(to);
13651#endif
13652}
13653
13654/*
13655 * "indent()" function
13656 */
13657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 typval_T *argvars;
13660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661{
13662 linenr_T lnum;
13663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669}
13670
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013671/*
13672 * "index()" function
13673 */
13674 static void
13675f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013676 typval_T *argvars;
13677 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013678{
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 list_T *l;
13680 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013681 long idx = 0;
13682 int ic = FALSE;
13683
13684 rettv->vval.v_number = -1;
13685 if (argvars[0].v_type != VAR_LIST)
13686 {
13687 EMSG(_(e_listreq));
13688 return;
13689 }
13690 l = argvars[0].vval.v_list;
13691 if (l != NULL)
13692 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013693 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 int error = FALSE;
13697
Bram Moolenaar758711c2005-02-02 23:11:38 +000013698 /* Start at specified item. Use the cached index that list_find()
13699 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013701 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013702 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 ic = get_tv_number_chk(&argvars[3], &error);
13704 if (error)
13705 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013707
Bram Moolenaar758711c2005-02-02 23:11:38 +000013708 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013709 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013710 {
13711 rettv->vval.v_number = idx;
13712 break;
13713 }
13714 }
13715}
13716
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717static int inputsecret_flag = 0;
13718
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013719static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13720
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013722 * This function is used by f_input() and f_inputdialog() functions. The third
13723 * argument to f_input() specifies the type of completion to use at the
13724 * prompt. The third argument to f_inputdialog() specifies the value to return
13725 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 */
13727 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013728get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013731 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 char_u *p = NULL;
13735 int c;
13736 char_u buf[NUMBUFLEN];
13737 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013739 int xp_type = EXPAND_NOTHING;
13740 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013743 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744
13745#ifdef NO_CONSOLE_INPUT
13746 /* While starting up, there is no place to enter text. */
13747 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#endif
13750
13751 cmd_silent = FALSE; /* Want to see the prompt. */
13752 if (prompt != NULL)
13753 {
13754 /* Only the part of the message after the last NL is considered as
13755 * prompt for the command line */
13756 p = vim_strrchr(prompt, '\n');
13757 if (p == NULL)
13758 p = prompt;
13759 else
13760 {
13761 ++p;
13762 c = *p;
13763 *p = NUL;
13764 msg_start();
13765 msg_clr_eos();
13766 msg_puts_attr(prompt, echo_attr);
13767 msg_didout = FALSE;
13768 msg_starthere();
13769 *p = c;
13770 }
13771 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 if (argvars[1].v_type != VAR_UNKNOWN)
13774 {
13775 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13776 if (defstr != NULL)
13777 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013779 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013780 {
13781 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013782 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013783 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013784
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013785 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013786 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013787
Bram Moolenaar4463f292005-09-25 22:20:24 +000013788 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13789 if (xp_name == NULL)
13790 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013791
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013792 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013793
Bram Moolenaar4463f292005-09-25 22:20:24 +000013794 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13795 &xp_arg) == FAIL)
13796 return;
13797 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013798 }
13799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013801 {
13802# ifdef FEAT_EX_EXTRA
13803 int save_ex_normal_busy = ex_normal_busy;
13804 ex_normal_busy = 0;
13805# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013807 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13808 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013809# ifdef FEAT_EX_EXTRA
13810 ex_normal_busy = save_ex_normal_busy;
13811# endif
13812 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013813 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013814 && argvars[1].v_type != VAR_UNKNOWN
13815 && argvars[2].v_type != VAR_UNKNOWN)
13816 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13817 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013818
13819 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 /* since the user typed this, no need to wait for return */
13822 need_wait_return = FALSE;
13823 msg_didout = FALSE;
13824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 cmd_silent = cmd_silent_save;
13826}
13827
13828/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013829 * "input()" function
13830 * Also handles inputsecret() when inputsecret is set.
13831 */
13832 static void
13833f_input(argvars, rettv)
13834 typval_T *argvars;
13835 typval_T *rettv;
13836{
13837 get_user_input(argvars, rettv, FALSE);
13838}
13839
13840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 * "inputdialog()" function
13842 */
13843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013845 typval_T *argvars;
13846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847{
13848#if defined(FEAT_GUI_TEXTDIALOG)
13849 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13850 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13851 {
13852 char_u *message;
13853 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 message = get_tv_string_chk(&argvars[0]);
13857 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013858 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013859 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 else
13861 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 if (message != NULL && defstr != NULL
13863 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013864 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013865 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 else
13867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868 if (message != NULL && defstr != NULL
13869 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013870 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871 rettv->vval.v_string = vim_strsave(
13872 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 }
13878 else
13879#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013880 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881}
13882
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013883/*
13884 * "inputlist()" function
13885 */
13886 static void
13887f_inputlist(argvars, rettv)
13888 typval_T *argvars;
13889 typval_T *rettv;
13890{
13891 listitem_T *li;
13892 int selected;
13893 int mouse_used;
13894
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013895#ifdef NO_CONSOLE_INPUT
13896 /* While starting up, there is no place to enter text. */
13897 if (no_console_input())
13898 return;
13899#endif
13900 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13901 {
13902 EMSG2(_(e_listarg), "inputlist()");
13903 return;
13904 }
13905
13906 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013907 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013908 lines_left = Rows; /* avoid more prompt */
13909 msg_scroll = TRUE;
13910 msg_clr_eos();
13911
13912 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13913 {
13914 msg_puts(get_tv_string(&li->li_tv));
13915 msg_putchar('\n');
13916 }
13917
13918 /* Ask for choice. */
13919 selected = prompt_for_number(&mouse_used);
13920 if (mouse_used)
13921 selected -= lines_left;
13922
13923 rettv->vval.v_number = selected;
13924}
13925
13926
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13928
13929/*
13930 * "inputrestore()" function
13931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936{
13937 if (ga_userinput.ga_len > 0)
13938 {
13939 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13941 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013942 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 }
13944 else if (p_verbose > 1)
13945 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013946 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 }
13949}
13950
13951/*
13952 * "inputsave()" function
13953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013959 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 if (ga_grow(&ga_userinput, 1) == OK)
13961 {
13962 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13963 + ga_userinput.ga_len);
13964 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013965 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 }
13967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969}
13970
13971/*
13972 * "inputsecret()" function
13973 */
13974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 typval_T *argvars;
13977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978{
13979 ++cmdline_star;
13980 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 --cmdline_star;
13983 --inputsecret_flag;
13984}
13985
13986/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987 * "insert()" function
13988 */
13989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 typval_T *argvars;
13992 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013993{
13994 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013995 listitem_T *item;
13996 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013998
13999 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014001 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014002 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014003 {
14004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 before = get_tv_number_chk(&argvars[2], &error);
14006 if (error)
14007 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014008
Bram Moolenaar758711c2005-02-02 23:11:38 +000014009 if (before == l->lv_len)
14010 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014011 else
14012 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014013 item = list_find(l, before);
14014 if (item == NULL)
14015 {
14016 EMSGN(_(e_listidx), before);
14017 l = NULL;
14018 }
14019 }
14020 if (l != NULL)
14021 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014022 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014023 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014024 }
14025 }
14026}
14027
14028/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014029 * "invert(expr)" function
14030 */
14031 static void
14032f_invert(argvars, rettv)
14033 typval_T *argvars;
14034 typval_T *rettv;
14035{
14036 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14037}
14038
14039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 * "isdirectory()" function
14041 */
14042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *argvars;
14045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048}
14049
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014050/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014051 * "islocked()" function
14052 */
14053 static void
14054f_islocked(argvars, rettv)
14055 typval_T *argvars;
14056 typval_T *rettv;
14057{
14058 lval_T lv;
14059 char_u *end;
14060 dictitem_T *di;
14061
14062 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014063 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14064 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014065 if (end != NULL && lv.ll_name != NULL)
14066 {
14067 if (*end != NUL)
14068 EMSG(_(e_trailing));
14069 else
14070 {
14071 if (lv.ll_tv == NULL)
14072 {
14073 if (check_changedtick(lv.ll_name))
14074 rettv->vval.v_number = 1; /* always locked */
14075 else
14076 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014077 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014078 if (di != NULL)
14079 {
14080 /* Consider a variable locked when:
14081 * 1. the variable itself is locked
14082 * 2. the value of the variable is locked.
14083 * 3. the List or Dict value is locked.
14084 */
14085 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14086 || tv_islocked(&di->di_tv));
14087 }
14088 }
14089 }
14090 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014091 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014092 else if (lv.ll_newkey != NULL)
14093 EMSG2(_(e_dictkey), lv.ll_newkey);
14094 else if (lv.ll_list != NULL)
14095 /* List item. */
14096 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14097 else
14098 /* Dictionary item. */
14099 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14100 }
14101 }
14102
14103 clear_lval(&lv);
14104}
14105
Bram Moolenaar33570922005-01-25 22:26:29 +000014106static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107
14108/*
14109 * Turn a dict into a list:
14110 * "what" == 0: list of keys
14111 * "what" == 1: list of values
14112 * "what" == 2: list of items
14113 */
14114 static void
14115dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014118 int what;
14119{
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 list_T *l2;
14121 dictitem_T *di;
14122 hashitem_T *hi;
14123 listitem_T *li;
14124 listitem_T *li2;
14125 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014126 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127
Bram Moolenaar8c711452005-01-14 21:53:12 +000014128 if (argvars[0].v_type != VAR_DICT)
14129 {
14130 EMSG(_(e_dictreq));
14131 return;
14132 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014133 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014134 return;
14135
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014136 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014139 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014142 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014144 --todo;
14145 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014146
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 li = listitem_alloc();
14148 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014149 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014150 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014151
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014152 if (what == 0)
14153 {
14154 /* keys() */
14155 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014156 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14158 }
14159 else if (what == 1)
14160 {
14161 /* values() */
14162 copy_tv(&di->di_tv, &li->li_tv);
14163 }
14164 else
14165 {
14166 /* items() */
14167 l2 = list_alloc();
14168 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014169 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014170 li->li_tv.vval.v_list = l2;
14171 if (l2 == NULL)
14172 break;
14173 ++l2->lv_refcount;
14174
14175 li2 = listitem_alloc();
14176 if (li2 == NULL)
14177 break;
14178 list_append(l2, li2);
14179 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014180 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014181 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14182
14183 li2 = listitem_alloc();
14184 if (li2 == NULL)
14185 break;
14186 list_append(l2, li2);
14187 copy_tv(&di->di_tv, &li2->li_tv);
14188 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014189 }
14190 }
14191}
14192
14193/*
14194 * "items(dict)" function
14195 */
14196 static void
14197f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 typval_T *argvars;
14199 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200{
14201 dict_list(argvars, rettv, 2);
14202}
14203
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014205 * "join()" function
14206 */
14207 static void
14208f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 typval_T *argvars;
14210 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014211{
14212 garray_T ga;
14213 char_u *sep;
14214
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014215 if (argvars[0].v_type != VAR_LIST)
14216 {
14217 EMSG(_(e_listreq));
14218 return;
14219 }
14220 if (argvars[0].vval.v_list == NULL)
14221 return;
14222 if (argvars[1].v_type == VAR_UNKNOWN)
14223 sep = (char_u *)" ";
14224 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014226
14227 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228
14229 if (sep != NULL)
14230 {
14231 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014232 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 ga_append(&ga, NUL);
14234 rettv->vval.v_string = (char_u *)ga.ga_data;
14235 }
14236 else
14237 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014238}
14239
14240/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241 * "keys()" function
14242 */
14243 static void
14244f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 typval_T *argvars;
14246 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014247{
14248 dict_list(argvars, rettv, 0);
14249}
14250
14251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 * "last_buffer_nr()" function.
14253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014256 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258{
14259 int n = 0;
14260 buf_T *buf;
14261
14262 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14263 if (n < buf->b_fnum)
14264 n = buf->b_fnum;
14265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014267}
14268
14269/*
14270 * "len()" function
14271 */
14272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 typval_T *argvars;
14275 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014276{
14277 switch (argvars[0].v_type)
14278 {
14279 case VAR_STRING:
14280 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->vval.v_number = (varnumber_T)STRLEN(
14282 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014283 break;
14284 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014287 case VAR_DICT:
14288 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14289 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014291 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014292 break;
14293 }
14294}
14295
Bram Moolenaar33570922005-01-25 22:26:29 +000014296static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014297
14298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 typval_T *argvars;
14301 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014302 int type;
14303{
14304#ifdef FEAT_LIBCALL
14305 char_u *string_in;
14306 char_u **string_result;
14307 int nr_result;
14308#endif
14309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014311 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014313
14314 if (check_restricted() || check_secure())
14315 return;
14316
14317#ifdef FEAT_LIBCALL
14318 /* The first two args must be strings, otherwise its meaningless */
14319 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14320 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014321 string_in = NULL;
14322 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014323 string_in = argvars[2].vval.v_string;
14324 if (type == VAR_NUMBER)
14325 string_result = NULL;
14326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014328 if (mch_libcall(argvars[0].vval.v_string,
14329 argvars[1].vval.v_string,
14330 string_in,
14331 argvars[2].vval.v_number,
14332 string_result,
14333 &nr_result) == OK
14334 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014335 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014336 }
14337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338}
14339
14340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 * "libcall()" function
14342 */
14343 static void
14344f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014345 typval_T *argvars;
14346 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347{
14348 libcall_common(argvars, rettv, VAR_STRING);
14349}
14350
14351/*
14352 * "libcallnr()" function
14353 */
14354 static void
14355f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *argvars;
14357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358{
14359 libcall_common(argvars, rettv, VAR_NUMBER);
14360}
14361
14362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 * "line(string)" function
14364 */
14365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014367 typval_T *argvars;
14368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369{
14370 linenr_T lnum = 0;
14371 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014372 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014374 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 if (fp != NULL)
14376 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378}
14379
14380/*
14381 * "line2byte(lnum)" function
14382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387{
14388#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390#else
14391 linenr_T lnum;
14392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14398 if (rettv->vval.v_number >= 0)
14399 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400#endif
14401}
14402
14403/*
14404 * "lispindent(lnum)" function
14405 */
14406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411#ifdef FEAT_LISP
14412 pos_T pos;
14413 linenr_T lnum;
14414
14415 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14418 {
14419 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 curwin->w_cursor = pos;
14422 }
14423 else
14424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426}
14427
14428/*
14429 * "localtime()" function
14430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014433 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437}
14438
Bram Moolenaar33570922005-01-25 22:26:29 +000014439static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
14441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 typval_T *argvars;
14444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 int exact;
14446{
14447 char_u *keys;
14448 char_u *which;
14449 char_u buf[NUMBUFLEN];
14450 char_u *keys_buf = NULL;
14451 char_u *rhs;
14452 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014453 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014454 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014455 mapblock_T *mp;
14456 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
14458 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->v_type = VAR_STRING;
14460 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 if (*keys == NUL)
14464 return;
14465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014469 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014470 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014471 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014472 if (argvars[3].v_type != VAR_UNKNOWN)
14473 get_dict = get_tv_number(&argvars[3]);
14474 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 else
14477 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014478 if (which == NULL)
14479 return;
14480
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 mode = get_map_mode(&which, 0);
14482
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014483 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014484 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014486
14487 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014489 /* Return a string. */
14490 if (rhs != NULL)
14491 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492
Bram Moolenaarbd743252010-10-20 21:23:33 +020014493 }
14494 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14495 {
14496 /* Return a dictionary. */
14497 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14498 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14499 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500
Bram Moolenaarbd743252010-10-20 21:23:33 +020014501 dict_add_nr_str(dict, "lhs", 0L, lhs);
14502 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14503 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14504 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14505 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14506 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14507 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014508 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014509 dict_add_nr_str(dict, "mode", 0L, mapmode);
14510
14511 vim_free(lhs);
14512 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 }
14514}
14515
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014516#ifdef FEAT_FLOAT
14517/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014518 * "log()" function
14519 */
14520 static void
14521f_log(argvars, rettv)
14522 typval_T *argvars;
14523 typval_T *rettv;
14524{
14525 float_T f;
14526
14527 rettv->v_type = VAR_FLOAT;
14528 if (get_float_arg(argvars, &f) == OK)
14529 rettv->vval.v_float = log(f);
14530 else
14531 rettv->vval.v_float = 0.0;
14532}
14533
14534/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014535 * "log10()" function
14536 */
14537 static void
14538f_log10(argvars, rettv)
14539 typval_T *argvars;
14540 typval_T *rettv;
14541{
14542 float_T f;
14543
14544 rettv->v_type = VAR_FLOAT;
14545 if (get_float_arg(argvars, &f) == OK)
14546 rettv->vval.v_float = log10(f);
14547 else
14548 rettv->vval.v_float = 0.0;
14549}
14550#endif
14551
Bram Moolenaar1dced572012-04-05 16:54:08 +020014552#ifdef FEAT_LUA
14553/*
14554 * "luaeval()" function
14555 */
14556 static void
14557f_luaeval(argvars, rettv)
14558 typval_T *argvars;
14559 typval_T *rettv;
14560{
14561 char_u *str;
14562 char_u buf[NUMBUFLEN];
14563
14564 str = get_tv_string_buf(&argvars[0], buf);
14565 do_luaeval(str, argvars + 1, rettv);
14566}
14567#endif
14568
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014570 * "map()" function
14571 */
14572 static void
14573f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 typval_T *argvars;
14575 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014576{
14577 filter_map(argvars, rettv, TRUE);
14578}
14579
14580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 */
14583 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 typval_T *argvars;
14586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
14591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 */
14594 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *argvars;
14597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600}
14601
Bram Moolenaar33570922005-01-25 22:26:29 +000014602static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603
14604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 int type;
14609{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014610 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014611 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014612 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 char_u *pat;
14614 regmatch_T regmatch;
14615 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014616 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 char_u *save_cpo;
14618 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014619 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014620 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014621 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 list_T *l = NULL;
14623 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014624 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014625 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626
14627 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14628 save_cpo = p_cpo;
14629 p_cpo = (char_u *)"";
14630
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014631 rettv->vval.v_number = -1;
14632 if (type == 3)
14633 {
14634 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014636 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014637 }
14638 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->v_type = VAR_STRING;
14641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014644 if (argvars[0].v_type == VAR_LIST)
14645 {
14646 if ((l = argvars[0].vval.v_list) == NULL)
14647 goto theend;
14648 li = l->lv_first;
14649 }
14650 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014651 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014652 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014653 len = (long)STRLEN(str);
14654 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14657 if (pat == NULL)
14658 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014659
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 int error = FALSE;
14663
14664 start = get_tv_number_chk(&argvars[2], &error);
14665 if (error)
14666 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014667 if (l != NULL)
14668 {
14669 li = list_find(l, start);
14670 if (li == NULL)
14671 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014672 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014673 }
14674 else
14675 {
14676 if (start < 0)
14677 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014678 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014679 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014680 /* When "count" argument is there ignore matches before "start",
14681 * otherwise skip part of the string. Differs when pattern is "^"
14682 * or "\<". */
14683 if (argvars[3].v_type != VAR_UNKNOWN)
14684 startcol = start;
14685 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014686 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014687 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014688 len -= start;
14689 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014690 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014691
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014692 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 nth = get_tv_number_chk(&argvars[3], &error);
14694 if (error)
14695 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 }
14697
14698 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14699 if (regmatch.regprog != NULL)
14700 {
14701 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014702
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014703 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014704 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014705 if (l != NULL)
14706 {
14707 if (li == NULL)
14708 {
14709 match = FALSE;
14710 break;
14711 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014713 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014714 if (str == NULL)
14715 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014716 }
14717
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014718 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014719
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014720 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014721 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722 if (l == NULL && !match)
14723 break;
14724
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014725 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014726 if (l != NULL)
14727 {
14728 li = li->li_next;
14729 ++idx;
14730 }
14731 else
14732 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014733#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014734 startcol = (colnr_T)(regmatch.startp[0]
14735 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014736#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014737 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014738#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014739 if (startcol > (colnr_T)len
14740 || str + startcol <= regmatch.startp[0])
14741 {
14742 match = FALSE;
14743 break;
14744 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014745 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014746 }
14747
14748 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014751 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 int i;
14753
14754 /* return list with matched string and submatches */
14755 for (i = 0; i < NSUBEXP; ++i)
14756 {
14757 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014758 {
14759 if (list_append_string(rettv->vval.v_list,
14760 (char_u *)"", 0) == FAIL)
14761 break;
14762 }
14763 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014764 regmatch.startp[i],
14765 (int)(regmatch.endp[i] - regmatch.startp[i]))
14766 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014768 }
14769 }
14770 else if (type == 2)
14771 {
14772 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014773 if (l != NULL)
14774 copy_tv(&li->li_tv, rettv);
14775 else
14776 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014778 }
14779 else if (l != NULL)
14780 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 else
14782 {
14783 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 (varnumber_T)(regmatch.startp[0] - str);
14786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014789 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 }
14791 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014792 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 }
14794
14795theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014796 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797 p_cpo = save_cpo;
14798}
14799
14800/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014801 * "match()" function
14802 */
14803 static void
14804f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 typval_T *argvars;
14806 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014807{
14808 find_some_match(argvars, rettv, 1);
14809}
14810
14811/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014812 * "matchadd()" function
14813 */
14814 static void
14815f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014816 typval_T *argvars UNUSED;
14817 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014818{
14819#ifdef FEAT_SEARCH_EXTRA
14820 char_u buf[NUMBUFLEN];
14821 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14822 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14823 int prio = 10; /* default priority */
14824 int id = -1;
14825 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014826 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014827
14828 rettv->vval.v_number = -1;
14829
14830 if (grp == NULL || pat == NULL)
14831 return;
14832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014833 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014834 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014835 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014836 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014837 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014838 if (argvars[4].v_type != VAR_UNKNOWN)
14839 {
14840 if (argvars[4].v_type != VAR_DICT)
14841 {
14842 EMSG(_(e_dictreq));
14843 return;
14844 }
14845 if (dict_find(argvars[4].vval.v_dict,
14846 (char_u *)"conceal", -1) != NULL)
14847 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14848 (char_u *)"conceal", FALSE);
14849 }
14850 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014851 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014852 if (error == TRUE)
14853 return;
14854 if (id >= 1 && id <= 3)
14855 {
14856 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14857 return;
14858 }
14859
Bram Moolenaar6561d522015-07-21 15:48:27 +020014860 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14861 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014862#endif
14863}
14864
14865/*
14866 * "matchaddpos()" function
14867 */
14868 static void
14869f_matchaddpos(argvars, rettv)
14870 typval_T *argvars UNUSED;
14871 typval_T *rettv UNUSED;
14872{
14873#ifdef FEAT_SEARCH_EXTRA
14874 char_u buf[NUMBUFLEN];
14875 char_u *group;
14876 int prio = 10;
14877 int id = -1;
14878 int error = FALSE;
14879 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014880 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014881
14882 rettv->vval.v_number = -1;
14883
14884 group = get_tv_string_buf_chk(&argvars[0], buf);
14885 if (group == NULL)
14886 return;
14887
14888 if (argvars[1].v_type != VAR_LIST)
14889 {
14890 EMSG2(_(e_listarg), "matchaddpos()");
14891 return;
14892 }
14893 l = argvars[1].vval.v_list;
14894 if (l == NULL)
14895 return;
14896
14897 if (argvars[2].v_type != VAR_UNKNOWN)
14898 {
14899 prio = get_tv_number_chk(&argvars[2], &error);
14900 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014901 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014902 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014903 if (argvars[4].v_type != VAR_UNKNOWN)
14904 {
14905 if (argvars[4].v_type != VAR_DICT)
14906 {
14907 EMSG(_(e_dictreq));
14908 return;
14909 }
14910 if (dict_find(argvars[4].vval.v_dict,
14911 (char_u *)"conceal", -1) != NULL)
14912 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14913 (char_u *)"conceal", FALSE);
14914 }
14915 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014916 }
14917 if (error == TRUE)
14918 return;
14919
14920 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14921 if (id == 1 || id == 2)
14922 {
14923 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14924 return;
14925 }
14926
Bram Moolenaar6561d522015-07-21 15:48:27 +020014927 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14928 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014929#endif
14930}
14931
14932/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014933 * "matcharg()" function
14934 */
14935 static void
14936f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014937 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014938 typval_T *rettv;
14939{
14940 if (rettv_list_alloc(rettv) == OK)
14941 {
14942#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014943 int id = get_tv_number(&argvars[0]);
14944 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014945
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014946 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014947 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014948 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14949 {
14950 list_append_string(rettv->vval.v_list,
14951 syn_id2name(m->hlg_id), -1);
14952 list_append_string(rettv->vval.v_list, m->pattern, -1);
14953 }
14954 else
14955 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014956 list_append_string(rettv->vval.v_list, NULL, -1);
14957 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014958 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014959 }
14960#endif
14961 }
14962}
14963
14964/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014965 * "matchdelete()" function
14966 */
14967 static void
14968f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014969 typval_T *argvars UNUSED;
14970 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014971{
14972#ifdef FEAT_SEARCH_EXTRA
14973 rettv->vval.v_number = match_delete(curwin,
14974 (int)get_tv_number(&argvars[0]), TRUE);
14975#endif
14976}
14977
14978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979 * "matchend()" function
14980 */
14981 static void
14982f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 typval_T *argvars;
14984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985{
14986 find_some_match(argvars, rettv, 0);
14987}
14988
14989/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014990 * "matchlist()" function
14991 */
14992 static void
14993f_matchlist(argvars, rettv)
14994 typval_T *argvars;
14995 typval_T *rettv;
14996{
14997 find_some_match(argvars, rettv, 3);
14998}
14999
15000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001 * "matchstr()" function
15002 */
15003 static void
15004f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 typval_T *argvars;
15006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007{
15008 find_some_match(argvars, rettv, 2);
15009}
15010
Bram Moolenaar33570922005-01-25 22:26:29 +000015011static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015012
15013 static void
15014max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *argvars;
15016 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015017 int domax;
15018{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015019 long n = 0;
15020 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015022
15023 if (argvars[0].v_type == VAR_LIST)
15024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015025 list_T *l;
15026 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015027
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015028 l = argvars[0].vval.v_list;
15029 if (l != NULL)
15030 {
15031 li = l->lv_first;
15032 if (li != NULL)
15033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015035 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015036 {
15037 li = li->li_next;
15038 if (li == NULL)
15039 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015040 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015041 if (domax ? i > n : i < n)
15042 n = i;
15043 }
15044 }
15045 }
15046 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015047 else if (argvars[0].v_type == VAR_DICT)
15048 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015050 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015052 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015053
15054 d = argvars[0].vval.v_dict;
15055 if (d != NULL)
15056 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015057 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015060 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015062 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015064 if (first)
15065 {
15066 n = i;
15067 first = FALSE;
15068 }
15069 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015070 n = i;
15071 }
15072 }
15073 }
15074 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015075 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015076 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015077 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015078}
15079
15080/*
15081 * "max()" function
15082 */
15083 static void
15084f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *argvars;
15086 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015087{
15088 max_min(argvars, rettv, TRUE);
15089}
15090
15091/*
15092 * "min()" function
15093 */
15094 static void
15095f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *argvars;
15097 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015098{
15099 max_min(argvars, rettv, FALSE);
15100}
15101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015102static int mkdir_recurse __ARGS((char_u *dir, int prot));
15103
15104/*
15105 * Create the directory in which "dir" is located, and higher levels when
15106 * needed.
15107 */
15108 static int
15109mkdir_recurse(dir, prot)
15110 char_u *dir;
15111 int prot;
15112{
15113 char_u *p;
15114 char_u *updir;
15115 int r = FAIL;
15116
15117 /* Get end of directory name in "dir".
15118 * We're done when it's "/" or "c:/". */
15119 p = gettail_sep(dir);
15120 if (p <= get_past_head(dir))
15121 return OK;
15122
15123 /* If the directory exists we're done. Otherwise: create it.*/
15124 updir = vim_strnsave(dir, (int)(p - dir));
15125 if (updir == NULL)
15126 return FAIL;
15127 if (mch_isdir(updir))
15128 r = OK;
15129 else if (mkdir_recurse(updir, prot) == OK)
15130 r = vim_mkdir_emsg(updir, prot);
15131 vim_free(updir);
15132 return r;
15133}
15134
15135#ifdef vim_mkdir
15136/*
15137 * "mkdir()" function
15138 */
15139 static void
15140f_mkdir(argvars, rettv)
15141 typval_T *argvars;
15142 typval_T *rettv;
15143{
15144 char_u *dir;
15145 char_u buf[NUMBUFLEN];
15146 int prot = 0755;
15147
15148 rettv->vval.v_number = FAIL;
15149 if (check_restricted() || check_secure())
15150 return;
15151
15152 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015153 if (*dir == NUL)
15154 rettv->vval.v_number = FAIL;
15155 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015156 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015157 if (*gettail(dir) == NUL)
15158 /* remove trailing slashes */
15159 *gettail_sep(dir) = NUL;
15160
15161 if (argvars[1].v_type != VAR_UNKNOWN)
15162 {
15163 if (argvars[2].v_type != VAR_UNKNOWN)
15164 prot = get_tv_number_chk(&argvars[2], NULL);
15165 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15166 mkdir_recurse(dir, prot);
15167 }
15168 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015169 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015170}
15171#endif
15172
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 * "mode()" function
15175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015177f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015178 typval_T *argvars;
15179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015181 char_u buf[3];
15182
15183 buf[1] = NUL;
15184 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 if (VIsual_active)
15187 {
15188 if (VIsual_select)
15189 buf[0] = VIsual_mode + 's' - 'v';
15190 else
15191 buf[0] = VIsual_mode;
15192 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015193 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015194 || State == CONFIRM)
15195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015197 if (State == ASKMORE)
15198 buf[1] = 'm';
15199 else if (State == CONFIRM)
15200 buf[1] = '?';
15201 }
15202 else if (State == EXTERNCMD)
15203 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 else if (State & INSERT)
15205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015206#ifdef FEAT_VREPLACE
15207 if (State & VREPLACE_FLAG)
15208 {
15209 buf[0] = 'R';
15210 buf[1] = 'v';
15211 }
15212 else
15213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 if (State & REPLACE_FLAG)
15215 buf[0] = 'R';
15216 else
15217 buf[0] = 'i';
15218 }
15219 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015222 if (exmode_active)
15223 buf[1] = 'v';
15224 }
15225 else if (exmode_active)
15226 {
15227 buf[0] = 'c';
15228 buf[1] = 'e';
15229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015233 if (finish_op)
15234 buf[1] = 'o';
15235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015237 /* Clear out the minor mode when the argument is not a non-zero number or
15238 * non-empty string. */
15239 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015240 buf[1] = NUL;
15241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->vval.v_string = vim_strsave(buf);
15243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244}
15245
Bram Moolenaar429fa852013-04-15 12:27:36 +020015246#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015247/*
15248 * "mzeval()" function
15249 */
15250 static void
15251f_mzeval(argvars, rettv)
15252 typval_T *argvars;
15253 typval_T *rettv;
15254{
15255 char_u *str;
15256 char_u buf[NUMBUFLEN];
15257
15258 str = get_tv_string_buf(&argvars[0], buf);
15259 do_mzeval(str, rettv);
15260}
Bram Moolenaar75676462013-01-30 14:55:42 +010015261
15262 void
15263mzscheme_call_vim(name, args, rettv)
15264 char_u *name;
15265 typval_T *args;
15266 typval_T *rettv;
15267{
15268 typval_T argvars[3];
15269
15270 argvars[0].v_type = VAR_STRING;
15271 argvars[0].vval.v_string = name;
15272 copy_tv(args, &argvars[1]);
15273 argvars[2].v_type = VAR_UNKNOWN;
15274 f_call(argvars, rettv);
15275 clear_tv(&argvars[1]);
15276}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015277#endif
15278
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 * "nextnonblank()" function
15281 */
15282 static void
15283f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 typval_T *argvars;
15285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286{
15287 linenr_T lnum;
15288
15289 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292 {
15293 lnum = 0;
15294 break;
15295 }
15296 if (*skipwhite(ml_get(lnum)) != NUL)
15297 break;
15298 }
15299 rettv->vval.v_number = lnum;
15300}
15301
15302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 * "nr2char()" function
15304 */
15305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309{
15310 char_u buf[NUMBUFLEN];
15311
15312#ifdef FEAT_MBYTE
15313 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015314 {
15315 int utf8 = 0;
15316
15317 if (argvars[1].v_type != VAR_UNKNOWN)
15318 utf8 = get_tv_number_chk(&argvars[1], NULL);
15319 if (utf8)
15320 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15321 else
15322 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 else
15325#endif
15326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 buf[1] = NUL;
15329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330 rettv->v_type = VAR_STRING;
15331 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015332}
15333
15334/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015335 * "or(expr, expr)" function
15336 */
15337 static void
15338f_or(argvars, rettv)
15339 typval_T *argvars;
15340 typval_T *rettv;
15341{
15342 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15343 | get_tv_number_chk(&argvars[1], NULL);
15344}
15345
15346/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015347 * "pathshorten()" function
15348 */
15349 static void
15350f_pathshorten(argvars, rettv)
15351 typval_T *argvars;
15352 typval_T *rettv;
15353{
15354 char_u *p;
15355
15356 rettv->v_type = VAR_STRING;
15357 p = get_tv_string_chk(&argvars[0]);
15358 if (p == NULL)
15359 rettv->vval.v_string = NULL;
15360 else
15361 {
15362 p = vim_strsave(p);
15363 rettv->vval.v_string = p;
15364 if (p != NULL)
15365 shorten_dir(p);
15366 }
15367}
15368
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015369#ifdef FEAT_FLOAT
15370/*
15371 * "pow()" function
15372 */
15373 static void
15374f_pow(argvars, rettv)
15375 typval_T *argvars;
15376 typval_T *rettv;
15377{
15378 float_T fx, fy;
15379
15380 rettv->v_type = VAR_FLOAT;
15381 if (get_float_arg(argvars, &fx) == OK
15382 && get_float_arg(&argvars[1], &fy) == OK)
15383 rettv->vval.v_float = pow(fx, fy);
15384 else
15385 rettv->vval.v_float = 0.0;
15386}
15387#endif
15388
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015390 * "prevnonblank()" function
15391 */
15392 static void
15393f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 typval_T *argvars;
15395 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396{
15397 linenr_T lnum;
15398
15399 lnum = get_tv_lnum(argvars);
15400 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15401 lnum = 0;
15402 else
15403 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15404 --lnum;
15405 rettv->vval.v_number = lnum;
15406}
15407
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015408#ifdef HAVE_STDARG_H
15409/* This dummy va_list is here because:
15410 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15411 * - locally in the function results in a "used before set" warning
15412 * - using va_start() to initialize it gives "function with fixed args" error */
15413static va_list ap;
15414#endif
15415
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015417 * "printf()" function
15418 */
15419 static void
15420f_printf(argvars, rettv)
15421 typval_T *argvars;
15422 typval_T *rettv;
15423{
15424 rettv->v_type = VAR_STRING;
15425 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015426#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015427 {
15428 char_u buf[NUMBUFLEN];
15429 int len;
15430 char_u *s;
15431 int saved_did_emsg = did_emsg;
15432 char *fmt;
15433
15434 /* Get the required length, allocate the buffer and do it for real. */
15435 did_emsg = FALSE;
15436 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015437 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015438 if (!did_emsg)
15439 {
15440 s = alloc(len + 1);
15441 if (s != NULL)
15442 {
15443 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015444 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015445 }
15446 }
15447 did_emsg |= saved_did_emsg;
15448 }
15449#endif
15450}
15451
15452/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015453 * "pumvisible()" function
15454 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015455 static void
15456f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015457 typval_T *argvars UNUSED;
15458 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015459{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015460#ifdef FEAT_INS_EXPAND
15461 if (pum_visible())
15462 rettv->vval.v_number = 1;
15463#endif
15464}
15465
Bram Moolenaardb913952012-06-29 12:54:53 +020015466#ifdef FEAT_PYTHON3
15467/*
15468 * "py3eval()" function
15469 */
15470 static void
15471f_py3eval(argvars, rettv)
15472 typval_T *argvars;
15473 typval_T *rettv;
15474{
15475 char_u *str;
15476 char_u buf[NUMBUFLEN];
15477
15478 str = get_tv_string_buf(&argvars[0], buf);
15479 do_py3eval(str, rettv);
15480}
15481#endif
15482
15483#ifdef FEAT_PYTHON
15484/*
15485 * "pyeval()" function
15486 */
15487 static void
15488f_pyeval(argvars, rettv)
15489 typval_T *argvars;
15490 typval_T *rettv;
15491{
15492 char_u *str;
15493 char_u buf[NUMBUFLEN];
15494
15495 str = get_tv_string_buf(&argvars[0], buf);
15496 do_pyeval(str, rettv);
15497}
15498#endif
15499
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015500/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015501 * "range()" function
15502 */
15503 static void
15504f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *argvars;
15506 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015507{
15508 long start;
15509 long end;
15510 long stride = 1;
15511 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015512 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015515 if (argvars[1].v_type == VAR_UNKNOWN)
15516 {
15517 end = start - 1;
15518 start = 0;
15519 }
15520 else
15521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015524 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015525 }
15526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 if (error)
15528 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015529 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015530 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015531 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015532 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015533 else
15534 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015535 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015536 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015537 if (list_append_number(rettv->vval.v_list,
15538 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015539 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015540 }
15541}
15542
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543/*
15544 * "readfile()" function
15545 */
15546 static void
15547f_readfile(argvars, rettv)
15548 typval_T *argvars;
15549 typval_T *rettv;
15550{
15551 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015552 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015553 char_u *fname;
15554 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015555 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15556 int io_size = sizeof(buf);
15557 int readlen; /* size of last fread() */
15558 char_u *prev = NULL; /* previously read bytes, if any */
15559 long prevlen = 0; /* length of data in prev */
15560 long prevsize = 0; /* size of prev buffer */
15561 long maxline = MAXLNUM;
15562 long cnt = 0;
15563 char_u *p; /* position in buf */
15564 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015565
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015566 if (argvars[1].v_type != VAR_UNKNOWN)
15567 {
15568 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15569 binary = TRUE;
15570 if (argvars[2].v_type != VAR_UNKNOWN)
15571 maxline = get_tv_number(&argvars[2]);
15572 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015574 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015575 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576
15577 /* Always open the file in binary mode, library functions have a mind of
15578 * their own about CR-LF conversion. */
15579 fname = get_tv_string(&argvars[0]);
15580 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15581 {
15582 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15583 return;
15584 }
15585
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015586 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015587 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015588 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015589
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015590 /* This for loop processes what was read, but is also entered at end
15591 * of file so that either:
15592 * - an incomplete line gets written
15593 * - a "binary" file gets an empty line at the end if it ends in a
15594 * newline. */
15595 for (p = buf, start = buf;
15596 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15597 ++p)
15598 {
15599 if (*p == '\n' || readlen <= 0)
15600 {
15601 listitem_T *li;
15602 char_u *s = NULL;
15603 long_u len = p - start;
15604
15605 /* Finished a line. Remove CRs before NL. */
15606 if (readlen > 0 && !binary)
15607 {
15608 while (len > 0 && start[len - 1] == '\r')
15609 --len;
15610 /* removal may cross back to the "prev" string */
15611 if (len == 0)
15612 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15613 --prevlen;
15614 }
15615 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015616 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015617 else
15618 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015619 /* Change "prev" buffer to be the right size. This way
15620 * the bytes are only copied once, and very long lines are
15621 * allocated only once. */
15622 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015623 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015624 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015625 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 prev = NULL; /* the list will own the string */
15627 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015628 }
15629 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015630 if (s == NULL)
15631 {
15632 do_outofmem_msg((long_u) prevlen + len + 1);
15633 failed = TRUE;
15634 break;
15635 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015637 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015638 {
15639 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015640 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641 break;
15642 }
15643 li->li_tv.v_type = VAR_STRING;
15644 li->li_tv.v_lock = 0;
15645 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015646 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015647
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015648 start = p + 1; /* step over newline */
15649 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015650 break;
15651 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015652 else if (*p == NUL)
15653 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015654#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015655 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15656 * when finding the BF and check the previous two bytes. */
15657 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015658 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015659 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15660 * + 1, these may be in the "prev" string. */
15661 char_u back1 = p >= buf + 1 ? p[-1]
15662 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15663 char_u back2 = p >= buf + 2 ? p[-2]
15664 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15665 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015666
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015667 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015668 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015669 char_u *dest = p - 2;
15670
15671 /* Usually a BOM is at the beginning of a file, and so at
15672 * the beginning of a line; then we can just step over it.
15673 */
15674 if (start == dest)
15675 start = p + 1;
15676 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015677 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015678 /* have to shuffle buf to close gap */
15679 int adjust_prevlen = 0;
15680
15681 if (dest < buf)
15682 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015683 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015684 dest = buf;
15685 }
15686 if (readlen > p - buf + 1)
15687 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15688 readlen -= 3 - adjust_prevlen;
15689 prevlen -= adjust_prevlen;
15690 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015691 }
15692 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015694#endif
15695 } /* for */
15696
15697 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15698 break;
15699 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015700 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015701 /* There's part of a line in buf, store it in "prev". */
15702 if (p - start + prevlen >= prevsize)
15703 {
15704 /* need bigger "prev" buffer */
15705 char_u *newprev;
15706
15707 /* A common use case is ordinary text files and "prev" gets a
15708 * fragment of a line, so the first allocation is made
15709 * small, to avoid repeatedly 'allocing' large and
15710 * 'reallocing' small. */
15711 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015712 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015713 else
15714 {
15715 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015716 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015717 prevsize = grow50pc > growmin ? grow50pc : growmin;
15718 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015719 newprev = prev == NULL ? alloc(prevsize)
15720 : vim_realloc(prev, prevsize);
15721 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015722 {
15723 do_outofmem_msg((long_u)prevsize);
15724 failed = TRUE;
15725 break;
15726 }
15727 prev = newprev;
15728 }
15729 /* Add the line part to end of "prev". */
15730 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015731 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015732 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015733 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015735 /*
15736 * For a negative line count use only the lines at the end of the file,
15737 * free the rest.
15738 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015739 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015740 while (cnt > -maxline)
15741 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015742 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015743 --cnt;
15744 }
15745
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015746 if (failed)
15747 {
15748 list_free(rettv->vval.v_list, TRUE);
15749 /* readfile doc says an empty list is returned on error */
15750 rettv->vval.v_list = list_alloc();
15751 }
15752
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015753 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015754 fclose(fd);
15755}
15756
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015757#if defined(FEAT_RELTIME)
15758static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15759
15760/*
15761 * Convert a List to proftime_T.
15762 * Return FAIL when there is something wrong.
15763 */
15764 static int
15765list2proftime(arg, tm)
15766 typval_T *arg;
15767 proftime_T *tm;
15768{
15769 long n1, n2;
15770 int error = FALSE;
15771
15772 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15773 || arg->vval.v_list->lv_len != 2)
15774 return FAIL;
15775 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15776 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15777# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015778 tm->HighPart = n1;
15779 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015780# else
15781 tm->tv_sec = n1;
15782 tm->tv_usec = n2;
15783# endif
15784 return error ? FAIL : OK;
15785}
15786#endif /* FEAT_RELTIME */
15787
15788/*
15789 * "reltime()" function
15790 */
15791 static void
15792f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015793 typval_T *argvars UNUSED;
15794 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015795{
15796#ifdef FEAT_RELTIME
15797 proftime_T res;
15798 proftime_T start;
15799
15800 if (argvars[0].v_type == VAR_UNKNOWN)
15801 {
15802 /* No arguments: get current time. */
15803 profile_start(&res);
15804 }
15805 else if (argvars[1].v_type == VAR_UNKNOWN)
15806 {
15807 if (list2proftime(&argvars[0], &res) == FAIL)
15808 return;
15809 profile_end(&res);
15810 }
15811 else
15812 {
15813 /* Two arguments: compute the difference. */
15814 if (list2proftime(&argvars[0], &start) == FAIL
15815 || list2proftime(&argvars[1], &res) == FAIL)
15816 return;
15817 profile_sub(&res, &start);
15818 }
15819
15820 if (rettv_list_alloc(rettv) == OK)
15821 {
15822 long n1, n2;
15823
15824# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015825 n1 = res.HighPart;
15826 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015827# else
15828 n1 = res.tv_sec;
15829 n2 = res.tv_usec;
15830# endif
15831 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15832 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15833 }
15834#endif
15835}
15836
15837/*
15838 * "reltimestr()" function
15839 */
15840 static void
15841f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015842 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015843 typval_T *rettv;
15844{
15845#ifdef FEAT_RELTIME
15846 proftime_T tm;
15847#endif
15848
15849 rettv->v_type = VAR_STRING;
15850 rettv->vval.v_string = NULL;
15851#ifdef FEAT_RELTIME
15852 if (list2proftime(&argvars[0], &tm) == OK)
15853 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15854#endif
15855}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015856
Bram Moolenaar0d660222005-01-07 21:51:51 +000015857#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15858static void make_connection __ARGS((void));
15859static int check_connection __ARGS((void));
15860
15861 static void
15862make_connection()
15863{
15864 if (X_DISPLAY == NULL
15865# ifdef FEAT_GUI
15866 && !gui.in_use
15867# endif
15868 )
15869 {
15870 x_force_connect = TRUE;
15871 setup_term_clip();
15872 x_force_connect = FALSE;
15873 }
15874}
15875
15876 static int
15877check_connection()
15878{
15879 make_connection();
15880 if (X_DISPLAY == NULL)
15881 {
15882 EMSG(_("E240: No connection to Vim server"));
15883 return FAIL;
15884 }
15885 return OK;
15886}
15887#endif
15888
15889#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015890static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891
15892 static void
15893remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 typval_T *argvars;
15895 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015896 int expr;
15897{
15898 char_u *server_name;
15899 char_u *keys;
15900 char_u *r = NULL;
15901 char_u buf[NUMBUFLEN];
15902# ifdef WIN32
15903 HWND w;
15904# else
15905 Window w;
15906# endif
15907
15908 if (check_restricted() || check_secure())
15909 return;
15910
15911# ifdef FEAT_X11
15912 if (check_connection() == FAIL)
15913 return;
15914# endif
15915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015916 server_name = get_tv_string_chk(&argvars[0]);
15917 if (server_name == NULL)
15918 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919 keys = get_tv_string_buf(&argvars[1], buf);
15920# ifdef WIN32
15921 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15922# else
15923 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15924 < 0)
15925# endif
15926 {
15927 if (r != NULL)
15928 EMSG(r); /* sending worked but evaluation failed */
15929 else
15930 EMSG2(_("E241: Unable to send to %s"), server_name);
15931 return;
15932 }
15933
15934 rettv->vval.v_string = r;
15935
15936 if (argvars[2].v_type != VAR_UNKNOWN)
15937 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015939 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015940 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015942 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 v.di_tv.v_type = VAR_STRING;
15944 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 idvar = get_tv_string_chk(&argvars[2]);
15946 if (idvar != NULL)
15947 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015949 }
15950}
15951#endif
15952
15953/*
15954 * "remote_expr()" function
15955 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956 static void
15957f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015958 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015959 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015960{
15961 rettv->v_type = VAR_STRING;
15962 rettv->vval.v_string = NULL;
15963#ifdef FEAT_CLIENTSERVER
15964 remote_common(argvars, rettv, TRUE);
15965#endif
15966}
15967
15968/*
15969 * "remote_foreground()" function
15970 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971 static void
15972f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015973 typval_T *argvars UNUSED;
15974 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976#ifdef FEAT_CLIENTSERVER
15977# ifdef WIN32
15978 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015979 {
15980 char_u *server_name = get_tv_string_chk(&argvars[0]);
15981
15982 if (server_name != NULL)
15983 serverForeground(server_name);
15984 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015985# else
15986 /* Send a foreground() expression to the server. */
15987 argvars[1].v_type = VAR_STRING;
15988 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15989 argvars[2].v_type = VAR_UNKNOWN;
15990 remote_common(argvars, rettv, TRUE);
15991 vim_free(argvars[1].vval.v_string);
15992# endif
15993#endif
15994}
15995
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996 static void
15997f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016000{
16001#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003 char_u *s = NULL;
16004# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016005 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016006# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016007 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016008
16009 if (check_restricted() || check_secure())
16010 {
16011 rettv->vval.v_number = -1;
16012 return;
16013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016014 serverid = get_tv_string_chk(&argvars[0]);
16015 if (serverid == NULL)
16016 {
16017 rettv->vval.v_number = -1;
16018 return; /* type error; errmsg already given */
16019 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016021 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016022 if (n == 0)
16023 rettv->vval.v_number = -1;
16024 else
16025 {
16026 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16027 rettv->vval.v_number = (s != NULL);
16028 }
16029# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016030 if (check_connection() == FAIL)
16031 return;
16032
16033 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035# endif
16036
16037 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016039 char_u *retvar;
16040
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 v.di_tv.v_type = VAR_STRING;
16042 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 retvar = get_tv_string_chk(&argvars[1]);
16044 if (retvar != NULL)
16045 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047 }
16048#else
16049 rettv->vval.v_number = -1;
16050#endif
16051}
16052
Bram Moolenaar0d660222005-01-07 21:51:51 +000016053 static void
16054f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016055 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016057{
16058 char_u *r = NULL;
16059
16060#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016061 char_u *serverid = get_tv_string_chk(&argvars[0]);
16062
16063 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016064 {
16065# ifdef WIN32
16066 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016067 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016068
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016069 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 if (n != 0)
16071 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16072 if (r == NULL)
16073# else
16074 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016076# endif
16077 EMSG(_("E277: Unable to read a server reply"));
16078 }
16079#endif
16080 rettv->v_type = VAR_STRING;
16081 rettv->vval.v_string = r;
16082}
16083
16084/*
16085 * "remote_send()" function
16086 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 static void
16088f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016091{
16092 rettv->v_type = VAR_STRING;
16093 rettv->vval.v_string = NULL;
16094#ifdef FEAT_CLIENTSERVER
16095 remote_common(argvars, rettv, FALSE);
16096#endif
16097}
16098
16099/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016100 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 */
16102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016103f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 typval_T *argvars;
16105 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016106{
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 list_T *l;
16108 listitem_T *item, *item2;
16109 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016110 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016111 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016112 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 dict_T *d;
16114 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016115 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016116
Bram Moolenaar8c711452005-01-14 21:53:12 +000016117 if (argvars[0].v_type == VAR_DICT)
16118 {
16119 if (argvars[2].v_type != VAR_UNKNOWN)
16120 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016121 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016122 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016124 key = get_tv_string_chk(&argvars[1]);
16125 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 di = dict_find(d, key, -1);
16128 if (di == NULL)
16129 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016130 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16131 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 {
16133 *rettv = di->di_tv;
16134 init_tv(&di->di_tv);
16135 dictitem_remove(d, di);
16136 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016137 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016138 }
16139 }
16140 else if (argvars[0].v_type != VAR_LIST)
16141 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016142 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016143 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 int error = FALSE;
16146
16147 idx = get_tv_number_chk(&argvars[1], &error);
16148 if (error)
16149 ; /* type error: do nothing, errmsg already given */
16150 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016151 EMSGN(_(e_listidx), idx);
16152 else
16153 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016154 if (argvars[2].v_type == VAR_UNKNOWN)
16155 {
16156 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016157 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016158 *rettv = item->li_tv;
16159 vim_free(item);
16160 }
16161 else
16162 {
16163 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016164 end = get_tv_number_chk(&argvars[2], &error);
16165 if (error)
16166 ; /* type error: do nothing */
16167 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016168 EMSGN(_(e_listidx), end);
16169 else
16170 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016171 int cnt = 0;
16172
16173 for (li = item; li != NULL; li = li->li_next)
16174 {
16175 ++cnt;
16176 if (li == item2)
16177 break;
16178 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016179 if (li == NULL) /* didn't find "item2" after "item" */
16180 EMSG(_(e_invrange));
16181 else
16182 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016183 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016184 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016185 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016186 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016187 l->lv_first = item;
16188 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016189 item->li_prev = NULL;
16190 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016191 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016192 }
16193 }
16194 }
16195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016196 }
16197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198}
16199
16200/*
16201 * "rename({from}, {to})" function
16202 */
16203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 typval_T *argvars;
16206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207{
16208 char_u buf[NUMBUFLEN];
16209
16210 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16214 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215}
16216
16217/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016218 * "repeat()" function
16219 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016220 static void
16221f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *argvars;
16223 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016224{
16225 char_u *p;
16226 int n;
16227 int slen;
16228 int len;
16229 char_u *r;
16230 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016231
16232 n = get_tv_number(&argvars[1]);
16233 if (argvars[0].v_type == VAR_LIST)
16234 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016235 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016236 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016237 if (list_extend(rettv->vval.v_list,
16238 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016239 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016240 }
16241 else
16242 {
16243 p = get_tv_string(&argvars[0]);
16244 rettv->v_type = VAR_STRING;
16245 rettv->vval.v_string = NULL;
16246
16247 slen = (int)STRLEN(p);
16248 len = slen * n;
16249 if (len <= 0)
16250 return;
16251
16252 r = alloc(len + 1);
16253 if (r != NULL)
16254 {
16255 for (i = 0; i < n; i++)
16256 mch_memmove(r + i * slen, p, (size_t)slen);
16257 r[len] = NUL;
16258 }
16259
16260 rettv->vval.v_string = r;
16261 }
16262}
16263
16264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 * "resolve()" function
16266 */
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
16272 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016273#ifdef HAVE_READLINK
16274 char_u *buf = NULL;
16275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278#ifdef FEAT_SHORTCUT
16279 {
16280 char_u *v = NULL;
16281
16282 v = mch_resolve_shortcut(p);
16283 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016284 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 }
16288#else
16289# ifdef HAVE_READLINK
16290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 char_u *cpy;
16292 int len;
16293 char_u *remain = NULL;
16294 char_u *q;
16295 int is_relative_to_current = FALSE;
16296 int has_trailing_pathsep = FALSE;
16297 int limit = 100;
16298
16299 p = vim_strsave(p);
16300
16301 if (p[0] == '.' && (vim_ispathsep(p[1])
16302 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16303 is_relative_to_current = TRUE;
16304
16305 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016306 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016309 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311
16312 q = getnextcomp(p);
16313 if (*q != NUL)
16314 {
16315 /* Separate the first path component in "p", and keep the
16316 * remainder (beginning with the path separator). */
16317 remain = vim_strsave(q - 1);
16318 q[-1] = NUL;
16319 }
16320
Bram Moolenaard9462e32011-04-11 21:35:11 +020016321 buf = alloc(MAXPATHL + 1);
16322 if (buf == NULL)
16323 goto fail;
16324
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 for (;;)
16326 {
16327 for (;;)
16328 {
16329 len = readlink((char *)p, (char *)buf, MAXPATHL);
16330 if (len <= 0)
16331 break;
16332 buf[len] = NUL;
16333
16334 if (limit-- == 0)
16335 {
16336 vim_free(p);
16337 vim_free(remain);
16338 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 goto fail;
16341 }
16342
16343 /* Ensure that the result will have a trailing path separator
16344 * if the argument has one. */
16345 if (remain == NULL && has_trailing_pathsep)
16346 add_pathsep(buf);
16347
16348 /* Separate the first path component in the link value and
16349 * concatenate the remainders. */
16350 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16351 if (*q != NUL)
16352 {
16353 if (remain == NULL)
16354 remain = vim_strsave(q - 1);
16355 else
16356 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016357 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 if (cpy != NULL)
16359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 vim_free(remain);
16361 remain = cpy;
16362 }
16363 }
16364 q[-1] = NUL;
16365 }
16366
16367 q = gettail(p);
16368 if (q > p && *q == NUL)
16369 {
16370 /* Ignore trailing path separator. */
16371 q[-1] = NUL;
16372 q = gettail(p);
16373 }
16374 if (q > p && !mch_isFullName(buf))
16375 {
16376 /* symlink is relative to directory of argument */
16377 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16378 if (cpy != NULL)
16379 {
16380 STRCPY(cpy, p);
16381 STRCPY(gettail(cpy), buf);
16382 vim_free(p);
16383 p = cpy;
16384 }
16385 }
16386 else
16387 {
16388 vim_free(p);
16389 p = vim_strsave(buf);
16390 }
16391 }
16392
16393 if (remain == NULL)
16394 break;
16395
16396 /* Append the first path component of "remain" to "p". */
16397 q = getnextcomp(remain + 1);
16398 len = q - remain - (*q != NUL);
16399 cpy = vim_strnsave(p, STRLEN(p) + len);
16400 if (cpy != NULL)
16401 {
16402 STRNCAT(cpy, remain, len);
16403 vim_free(p);
16404 p = cpy;
16405 }
16406 /* Shorten "remain". */
16407 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016408 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 else
16410 {
16411 vim_free(remain);
16412 remain = NULL;
16413 }
16414 }
16415
16416 /* If the result is a relative path name, make it explicitly relative to
16417 * the current directory if and only if the argument had this form. */
16418 if (!vim_ispathsep(*p))
16419 {
16420 if (is_relative_to_current
16421 && *p != NUL
16422 && !(p[0] == '.'
16423 && (p[1] == NUL
16424 || vim_ispathsep(p[1])
16425 || (p[1] == '.'
16426 && (p[2] == NUL
16427 || vim_ispathsep(p[2]))))))
16428 {
16429 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016430 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 if (cpy != NULL)
16432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 vim_free(p);
16434 p = cpy;
16435 }
16436 }
16437 else if (!is_relative_to_current)
16438 {
16439 /* Strip leading "./". */
16440 q = p;
16441 while (q[0] == '.' && vim_ispathsep(q[1]))
16442 q += 2;
16443 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016444 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 }
16446 }
16447
16448 /* Ensure that the result will have no trailing path separator
16449 * if the argument had none. But keep "/" or "//". */
16450 if (!has_trailing_pathsep)
16451 {
16452 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016453 if (after_pathsep(p, q))
16454 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 }
16456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016457 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 }
16459# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016460 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461# endif
16462#endif
16463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465
16466#ifdef HAVE_READLINK
16467fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016468 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016470 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471}
16472
16473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 */
16476 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 typval_T *argvars;
16479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480{
Bram Moolenaar33570922005-01-25 22:26:29 +000016481 list_T *l;
16482 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 if (argvars[0].v_type != VAR_LIST)
16485 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016486 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016487 && !tv_check_lock(l->lv_lock,
16488 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 {
16490 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016491 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016492 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493 while (li != NULL)
16494 {
16495 ni = li->li_prev;
16496 list_append(l, li);
16497 li = ni;
16498 }
16499 rettv->vval.v_list = l;
16500 rettv->v_type = VAR_LIST;
16501 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016502 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504}
16505
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016506#define SP_NOMOVE 0x01 /* don't move cursor */
16507#define SP_REPEAT 0x02 /* repeat to find outer pair */
16508#define SP_RETCOUNT 0x04 /* return matchcount */
16509#define SP_SETPCMARK 0x08 /* set previous context mark */
16510#define SP_START 0x10 /* accept match at start position */
16511#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16512#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016513#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016514
Bram Moolenaar33570922005-01-25 22:26:29 +000016515static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516
16517/*
16518 * Get flags for a search function.
16519 * Possibly sets "p_ws".
16520 * Returns BACKWARD, FORWARD or zero (for an error).
16521 */
16522 static int
16523get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 int *flagsp;
16526{
16527 int dir = FORWARD;
16528 char_u *flags;
16529 char_u nbuf[NUMBUFLEN];
16530 int mask;
16531
16532 if (varp->v_type != VAR_UNKNOWN)
16533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 flags = get_tv_string_buf_chk(varp, nbuf);
16535 if (flags == NULL)
16536 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537 while (*flags != NUL)
16538 {
16539 switch (*flags)
16540 {
16541 case 'b': dir = BACKWARD; break;
16542 case 'w': p_ws = TRUE; break;
16543 case 'W': p_ws = FALSE; break;
16544 default: mask = 0;
16545 if (flagsp != NULL)
16546 switch (*flags)
16547 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016548 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016549 case 'e': mask = SP_END; break;
16550 case 'm': mask = SP_RETCOUNT; break;
16551 case 'n': mask = SP_NOMOVE; break;
16552 case 'p': mask = SP_SUBPAT; break;
16553 case 'r': mask = SP_REPEAT; break;
16554 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016555 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556 }
16557 if (mask == 0)
16558 {
16559 EMSG2(_(e_invarg2), flags);
16560 dir = 0;
16561 }
16562 else
16563 *flagsp |= mask;
16564 }
16565 if (dir == 0)
16566 break;
16567 ++flags;
16568 }
16569 }
16570 return dir;
16571}
16572
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016574 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016577search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016578 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016580 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016582 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 char_u *pat;
16584 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016585 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 int save_p_ws = p_ws;
16587 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016588 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016589 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016590 proftime_T tm;
16591#ifdef FEAT_RELTIME
16592 long time_limit = 0;
16593#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016594 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016595 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016597 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016598 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016599 if (dir == 0)
16600 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016601 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016602 if (flags & SP_START)
16603 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016604 if (flags & SP_END)
16605 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016606 if (flags & SP_COLUMN)
16607 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016608
Bram Moolenaar76929292008-01-06 19:07:36 +000016609 /* Optional arguments: line number to stop searching and timeout. */
16610 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016611 {
16612 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16613 if (lnum_stop < 0)
16614 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016615#ifdef FEAT_RELTIME
16616 if (argvars[3].v_type != VAR_UNKNOWN)
16617 {
16618 time_limit = get_tv_number_chk(&argvars[3], NULL);
16619 if (time_limit < 0)
16620 goto theend;
16621 }
16622#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016623 }
16624
Bram Moolenaar76929292008-01-06 19:07:36 +000016625#ifdef FEAT_RELTIME
16626 /* Set the time limit, if there is one. */
16627 profile_setlimit(time_limit, &tm);
16628#endif
16629
Bram Moolenaar231334e2005-07-25 20:46:57 +000016630 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016631 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016632 * Check to make sure only those flags are set.
16633 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16634 * flags cannot be set. Check for that condition also.
16635 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016636 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016637 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016639 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016640 goto theend;
16641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016643 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016644 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016645 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016646 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016648 if (flags & SP_SUBPAT)
16649 retval = subpatnum;
16650 else
16651 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016652 if (flags & SP_SETPCMARK)
16653 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016655 if (match_pos != NULL)
16656 {
16657 /* Store the match cursor position */
16658 match_pos->lnum = pos.lnum;
16659 match_pos->col = pos.col + 1;
16660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 /* "/$" will put the cursor after the end of the line, may need to
16662 * correct that here */
16663 check_cursor();
16664 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016665
16666 /* If 'n' flag is used: restore cursor position. */
16667 if (flags & SP_NOMOVE)
16668 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016669 else
16670 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016671theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016673
16674 return retval;
16675}
16676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016677#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016678
16679/*
16680 * round() is not in C90, use ceil() or floor() instead.
16681 */
16682 float_T
16683vim_round(f)
16684 float_T f;
16685{
16686 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16687}
16688
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016689/*
16690 * "round({float})" function
16691 */
16692 static void
16693f_round(argvars, rettv)
16694 typval_T *argvars;
16695 typval_T *rettv;
16696{
16697 float_T f;
16698
16699 rettv->v_type = VAR_FLOAT;
16700 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016701 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016702 else
16703 rettv->vval.v_float = 0.0;
16704}
16705#endif
16706
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016707/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016708 * "screenattr()" function
16709 */
16710 static void
16711f_screenattr(argvars, rettv)
16712 typval_T *argvars UNUSED;
16713 typval_T *rettv;
16714{
16715 int row;
16716 int col;
16717 int c;
16718
16719 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16720 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16721 if (row < 0 || row >= screen_Rows
16722 || col < 0 || col >= screen_Columns)
16723 c = -1;
16724 else
16725 c = ScreenAttrs[LineOffset[row] + col];
16726 rettv->vval.v_number = c;
16727}
16728
16729/*
16730 * "screenchar()" function
16731 */
16732 static void
16733f_screenchar(argvars, rettv)
16734 typval_T *argvars UNUSED;
16735 typval_T *rettv;
16736{
16737 int row;
16738 int col;
16739 int off;
16740 int c;
16741
16742 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16743 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16744 if (row < 0 || row >= screen_Rows
16745 || col < 0 || col >= screen_Columns)
16746 c = -1;
16747 else
16748 {
16749 off = LineOffset[row] + col;
16750#ifdef FEAT_MBYTE
16751 if (enc_utf8 && ScreenLinesUC[off] != 0)
16752 c = ScreenLinesUC[off];
16753 else
16754#endif
16755 c = ScreenLines[off];
16756 }
16757 rettv->vval.v_number = c;
16758}
16759
16760/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016761 * "screencol()" function
16762 *
16763 * First column is 1 to be consistent with virtcol().
16764 */
16765 static void
16766f_screencol(argvars, rettv)
16767 typval_T *argvars UNUSED;
16768 typval_T *rettv;
16769{
16770 rettv->vval.v_number = screen_screencol() + 1;
16771}
16772
16773/*
16774 * "screenrow()" function
16775 */
16776 static void
16777f_screenrow(argvars, rettv)
16778 typval_T *argvars UNUSED;
16779 typval_T *rettv;
16780{
16781 rettv->vval.v_number = screen_screenrow() + 1;
16782}
16783
16784/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016785 * "search()" function
16786 */
16787 static void
16788f_search(argvars, rettv)
16789 typval_T *argvars;
16790 typval_T *rettv;
16791{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016792 int flags = 0;
16793
16794 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016798 * "searchdecl()" function
16799 */
16800 static void
16801f_searchdecl(argvars, rettv)
16802 typval_T *argvars;
16803 typval_T *rettv;
16804{
16805 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016806 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016807 int error = FALSE;
16808 char_u *name;
16809
16810 rettv->vval.v_number = 1; /* default: FAIL */
16811
16812 name = get_tv_string_chk(&argvars[0]);
16813 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016814 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016815 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016816 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16817 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16818 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016819 if (!error && name != NULL)
16820 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016821 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016822}
16823
16824/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016825 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016827 static int
16828searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016830 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831{
16832 char_u *spat, *mpat, *epat;
16833 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 int dir;
16836 int flags = 0;
16837 char_u nbuf1[NUMBUFLEN];
16838 char_u nbuf2[NUMBUFLEN];
16839 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016840 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016841 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016842 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016845 spat = get_tv_string_chk(&argvars[0]);
16846 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16847 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16848 if (spat == NULL || mpat == NULL || epat == NULL)
16849 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 /* Handle the optional fourth argument: flags */
16852 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016853 if (dir == 0)
16854 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016855
16856 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016857 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16858 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016859 if ((flags & (SP_END | SP_SUBPAT)) != 0
16860 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016861 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016862 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016863 goto theend;
16864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016866 /* Using 'r' implies 'W', otherwise it doesn't work. */
16867 if (flags & SP_REPEAT)
16868 p_ws = FALSE;
16869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016870 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016871 if (argvars[3].v_type == VAR_UNKNOWN
16872 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 skip = (char_u *)"";
16874 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016877 if (argvars[5].v_type != VAR_UNKNOWN)
16878 {
16879 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16880 if (lnum_stop < 0)
16881 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016882#ifdef FEAT_RELTIME
16883 if (argvars[6].v_type != VAR_UNKNOWN)
16884 {
16885 time_limit = get_tv_number_chk(&argvars[6], NULL);
16886 if (time_limit < 0)
16887 goto theend;
16888 }
16889#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016890 }
16891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 if (skip == NULL)
16893 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016895 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016896 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016897
16898theend:
16899 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016900
16901 return retval;
16902}
16903
16904/*
16905 * "searchpair()" function
16906 */
16907 static void
16908f_searchpair(argvars, rettv)
16909 typval_T *argvars;
16910 typval_T *rettv;
16911{
16912 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16913}
16914
16915/*
16916 * "searchpairpos()" function
16917 */
16918 static void
16919f_searchpairpos(argvars, rettv)
16920 typval_T *argvars;
16921 typval_T *rettv;
16922{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016923 pos_T match_pos;
16924 int lnum = 0;
16925 int col = 0;
16926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016928 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016929
16930 if (searchpair_cmn(argvars, &match_pos) > 0)
16931 {
16932 lnum = match_pos.lnum;
16933 col = match_pos.col;
16934 }
16935
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016936 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16937 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016938}
16939
16940/*
16941 * Search for a start/middle/end thing.
16942 * Used by searchpair(), see its documentation for the details.
16943 * Returns 0 or -1 for no match,
16944 */
16945 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016946do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16947 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016948 char_u *spat; /* start pattern */
16949 char_u *mpat; /* middle pattern */
16950 char_u *epat; /* end pattern */
16951 int dir; /* BACKWARD or FORWARD */
16952 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016953 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016954 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016956 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016957{
16958 char_u *save_cpo;
16959 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16960 long retval = 0;
16961 pos_T pos;
16962 pos_T firstpos;
16963 pos_T foundpos;
16964 pos_T save_cursor;
16965 pos_T save_pos;
16966 int n;
16967 int r;
16968 int nest = 1;
16969 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016970 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016971 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016972
16973 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16974 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016975 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016976
Bram Moolenaar76929292008-01-06 19:07:36 +000016977#ifdef FEAT_RELTIME
16978 /* Set the time limit, if there is one. */
16979 profile_setlimit(time_limit, &tm);
16980#endif
16981
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016982 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16983 * start/middle/end (pat3, for the top pair). */
16984 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16985 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16986 if (pat2 == NULL || pat3 == NULL)
16987 goto theend;
16988 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16989 if (*mpat == NUL)
16990 STRCPY(pat3, pat2);
16991 else
16992 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16993 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016994 if (flags & SP_START)
16995 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016996
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 save_cursor = curwin->w_cursor;
16998 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016999 clearpos(&firstpos);
17000 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 pat = pat3;
17002 for (;;)
17003 {
17004 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017005 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17007 /* didn't find it or found the first match again: FAIL */
17008 break;
17009
17010 if (firstpos.lnum == 0)
17011 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017012 if (equalpos(pos, foundpos))
17013 {
17014 /* Found the same position again. Can happen with a pattern that
17015 * has "\zs" at the end and searching backwards. Advance one
17016 * character and try again. */
17017 if (dir == BACKWARD)
17018 decl(&pos);
17019 else
17020 incl(&pos);
17021 }
17022 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017024 /* clear the start flag to avoid getting stuck here */
17025 options &= ~SEARCH_START;
17026
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 /* If the skip pattern matches, ignore this match. */
17028 if (*skip != NUL)
17029 {
17030 save_pos = curwin->w_cursor;
17031 curwin->w_cursor = pos;
17032 r = eval_to_bool(skip, &err, NULL, FALSE);
17033 curwin->w_cursor = save_pos;
17034 if (err)
17035 {
17036 /* Evaluating {skip} caused an error, break here. */
17037 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017038 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 break;
17040 }
17041 if (r)
17042 continue;
17043 }
17044
17045 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17046 {
17047 /* Found end when searching backwards or start when searching
17048 * forward: nested pair. */
17049 ++nest;
17050 pat = pat2; /* nested, don't search for middle */
17051 }
17052 else
17053 {
17054 /* Found end when searching forward or start when searching
17055 * backward: end of (nested) pair; or found middle in outer pair. */
17056 if (--nest == 1)
17057 pat = pat3; /* outer level, search for middle */
17058 }
17059
17060 if (nest == 0)
17061 {
17062 /* Found the match: return matchcount or line number. */
17063 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017064 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017066 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017067 if (flags & SP_SETPCMARK)
17068 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 curwin->w_cursor = pos;
17070 if (!(flags & SP_REPEAT))
17071 break;
17072 nest = 1; /* search for next unmatched */
17073 }
17074 }
17075
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017076 if (match_pos != NULL)
17077 {
17078 /* Store the match cursor position */
17079 match_pos->lnum = curwin->w_cursor.lnum;
17080 match_pos->col = curwin->w_cursor.col + 1;
17081 }
17082
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017084 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 curwin->w_cursor = save_cursor;
17086
17087theend:
17088 vim_free(pat2);
17089 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017090 if (p_cpo == empty_option)
17091 p_cpo = save_cpo;
17092 else
17093 /* Darn, evaluating the {skip} expression changed the value. */
17094 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017095
17096 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097}
17098
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017099/*
17100 * "searchpos()" function
17101 */
17102 static void
17103f_searchpos(argvars, rettv)
17104 typval_T *argvars;
17105 typval_T *rettv;
17106{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017107 pos_T match_pos;
17108 int lnum = 0;
17109 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017110 int n;
17111 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017113 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017114 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017115
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017116 n = search_cmn(argvars, &match_pos, &flags);
17117 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017118 {
17119 lnum = match_pos.lnum;
17120 col = match_pos.col;
17121 }
17122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017123 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17124 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017125 if (flags & SP_SUBPAT)
17126 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017127}
17128
17129
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 static void
17131f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017132 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135#ifdef FEAT_CLIENTSERVER
17136 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 char_u *server = get_tv_string_chk(&argvars[0]);
17138 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 if (server == NULL || reply == NULL)
17142 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 if (check_restricted() || check_secure())
17144 return;
17145# ifdef FEAT_X11
17146 if (check_connection() == FAIL)
17147 return;
17148# endif
17149
17150 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 EMSG(_("E258: Unable to send to client"));
17153 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155 rettv->vval.v_number = 0;
17156#else
17157 rettv->vval.v_number = -1;
17158#endif
17159}
17160
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 static void
17162f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165{
17166 char_u *r = NULL;
17167
17168#ifdef FEAT_CLIENTSERVER
17169# ifdef WIN32
17170 r = serverGetVimNames();
17171# else
17172 make_connection();
17173 if (X_DISPLAY != NULL)
17174 r = serverGetVimNames(X_DISPLAY);
17175# endif
17176#endif
17177 rettv->v_type = VAR_STRING;
17178 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179}
17180
17181/*
17182 * "setbufvar()" function
17183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017186 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017187 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188{
17189 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 char_u nbuf[NUMBUFLEN];
17194
17195 if (check_restricted() || check_secure())
17196 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017197 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17198 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017199 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 varp = &argvars[2];
17201
17202 if (buf != NULL && varname != NULL && varp != NULL)
17203 {
17204 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206
17207 if (*varname == '&')
17208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 long numval;
17210 char_u *strval;
17211 int error = FALSE;
17212
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 numval = get_tv_number_chk(varp, &error);
17215 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017216 if (!error && strval != NULL)
17217 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 }
17219 else
17220 {
17221 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17222 if (bufvarname != NULL)
17223 {
17224 STRCPY(bufvarname, "b:");
17225 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017226 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 vim_free(bufvarname);
17228 }
17229 }
17230
17231 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234}
17235
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017236 static void
17237f_setcharsearch(argvars, rettv)
17238 typval_T *argvars;
17239 typval_T *rettv UNUSED;
17240{
17241 dict_T *d;
17242 dictitem_T *di;
17243 char_u *csearch;
17244
17245 if (argvars[0].v_type != VAR_DICT)
17246 {
17247 EMSG(_(e_dictreq));
17248 return;
17249 }
17250
17251 if ((d = argvars[0].vval.v_dict) != NULL)
17252 {
17253 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17254 if (csearch != NULL)
17255 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017256#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017257 if (enc_utf8)
17258 {
17259 int pcc[MAX_MCO];
17260 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017261
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017262 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17263 }
17264 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017265#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017266 set_last_csearch(PTR2CHAR(csearch),
17267 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017268 }
17269
17270 di = dict_find(d, (char_u *)"forward", -1);
17271 if (di != NULL)
17272 set_csearch_direction(get_tv_number(&di->di_tv)
17273 ? FORWARD : BACKWARD);
17274
17275 di = dict_find(d, (char_u *)"until", -1);
17276 if (di != NULL)
17277 set_csearch_until(!!get_tv_number(&di->di_tv));
17278 }
17279}
17280
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281/*
17282 * "setcmdpos()" function
17283 */
17284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 typval_T *argvars;
17287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017289 int pos = (int)get_tv_number(&argvars[0]) - 1;
17290
17291 if (pos >= 0)
17292 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293}
17294
17295/*
17296 * "setline()" function
17297 */
17298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 typval_T *argvars;
17301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
17303 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017304 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017305 list_T *l = NULL;
17306 listitem_T *li = NULL;
17307 long added = 0;
17308 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017310 lnum = get_tv_lnum(&argvars[0]);
17311 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017313 l = argvars[1].vval.v_list;
17314 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017316 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017317 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017318
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017319 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017320 for (;;)
17321 {
17322 if (l != NULL)
17323 {
17324 /* list argument, get next string */
17325 if (li == NULL)
17326 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017327 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017328 li = li->li_next;
17329 }
17330
17331 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017333 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017334
17335 /* When coming here from Insert mode, sync undo, so that this can be
17336 * undone separately from what was previously inserted. */
17337 if (u_sync_once == 2)
17338 {
17339 u_sync_once = 1; /* notify that u_sync() was called */
17340 u_sync(TRUE);
17341 }
17342
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017343 if (lnum <= curbuf->b_ml.ml_line_count)
17344 {
17345 /* existing line, replace it */
17346 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17347 {
17348 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017349 if (lnum == curwin->w_cursor.lnum)
17350 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017351 rettv->vval.v_number = 0; /* OK */
17352 }
17353 }
17354 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17355 {
17356 /* lnum is one past the last line, append the line */
17357 ++added;
17358 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17359 rettv->vval.v_number = 0; /* OK */
17360 }
17361
17362 if (l == NULL) /* only one string argument */
17363 break;
17364 ++lnum;
17365 }
17366
17367 if (added > 0)
17368 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369}
17370
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017371static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17372
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017374 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017375 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017376 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017377set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017378 win_T *wp UNUSED;
17379 typval_T *list_arg UNUSED;
17380 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017381 typval_T *rettv;
17382{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017383#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017384 char_u *act;
17385 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017386#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017387
Bram Moolenaar2641f772005-03-25 21:58:17 +000017388 rettv->vval.v_number = -1;
17389
17390#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017391 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017392 EMSG(_(e_listreq));
17393 else
17394 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017395 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017396
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017397 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017398 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017399 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017400 if (act == NULL)
17401 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017402 if (*act == 'a' || *act == 'r')
17403 action = *act;
17404 }
17405
Bram Moolenaar81484f42012-12-05 15:16:47 +010017406 if (l != NULL && set_errorlist(wp, l, action,
17407 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017408 rettv->vval.v_number = 0;
17409 }
17410#endif
17411}
17412
17413/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017414 * "setloclist()" function
17415 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017416 static void
17417f_setloclist(argvars, rettv)
17418 typval_T *argvars;
17419 typval_T *rettv;
17420{
17421 win_T *win;
17422
17423 rettv->vval.v_number = -1;
17424
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017425 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017426 if (win != NULL)
17427 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17428}
17429
17430/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017431 * "setmatches()" function
17432 */
17433 static void
17434f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017435 typval_T *argvars UNUSED;
17436 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017437{
17438#ifdef FEAT_SEARCH_EXTRA
17439 list_T *l;
17440 listitem_T *li;
17441 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017442 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017443
17444 rettv->vval.v_number = -1;
17445 if (argvars[0].v_type != VAR_LIST)
17446 {
17447 EMSG(_(e_listreq));
17448 return;
17449 }
17450 if ((l = argvars[0].vval.v_list) != NULL)
17451 {
17452
17453 /* To some extent make sure that we are dealing with a list from
17454 * "getmatches()". */
17455 li = l->lv_first;
17456 while (li != NULL)
17457 {
17458 if (li->li_tv.v_type != VAR_DICT
17459 || (d = li->li_tv.vval.v_dict) == NULL)
17460 {
17461 EMSG(_(e_invarg));
17462 return;
17463 }
17464 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017465 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17466 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017467 && dict_find(d, (char_u *)"priority", -1) != NULL
17468 && dict_find(d, (char_u *)"id", -1) != NULL))
17469 {
17470 EMSG(_(e_invarg));
17471 return;
17472 }
17473 li = li->li_next;
17474 }
17475
17476 clear_matches(curwin);
17477 li = l->lv_first;
17478 while (li != NULL)
17479 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017480 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017481 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017482 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017483 char_u *group;
17484 int priority;
17485 int id;
17486 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017487
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017488 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017489 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17490 {
17491 if (s == NULL)
17492 {
17493 s = list_alloc();
17494 if (s == NULL)
17495 return;
17496 }
17497
17498 /* match from matchaddpos() */
17499 for (i = 1; i < 9; i++)
17500 {
17501 sprintf((char *)buf, (char *)"pos%d", i);
17502 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17503 {
17504 if (di->di_tv.v_type != VAR_LIST)
17505 return;
17506
17507 list_append_tv(s, &di->di_tv);
17508 s->lv_refcount++;
17509 }
17510 else
17511 break;
17512 }
17513 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017514
17515 group = get_dict_string(d, (char_u *)"group", FALSE);
17516 priority = (int)get_dict_number(d, (char_u *)"priority");
17517 id = (int)get_dict_number(d, (char_u *)"id");
17518 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17519 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17520 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017521 if (i == 0)
17522 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017523 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017524 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017525 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017526 }
17527 else
17528 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017529 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017530 list_unref(s);
17531 s = NULL;
17532 }
17533
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017534 li = li->li_next;
17535 }
17536 rettv->vval.v_number = 0;
17537 }
17538#endif
17539}
17540
17541/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017542 * "setpos()" function
17543 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017544 static void
17545f_setpos(argvars, rettv)
17546 typval_T *argvars;
17547 typval_T *rettv;
17548{
17549 pos_T pos;
17550 int fnum;
17551 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017552 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017553
Bram Moolenaar08250432008-02-13 11:42:46 +000017554 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017555 name = get_tv_string_chk(argvars);
17556 if (name != NULL)
17557 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017558 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017559 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017560 if (--pos.col < 0)
17561 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017562 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017563 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017564 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017565 if (fnum == curbuf->b_fnum)
17566 {
17567 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017568 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017569 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017570 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017571 curwin->w_set_curswant = FALSE;
17572 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017573 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017574 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017575 }
17576 else
17577 EMSG(_(e_invarg));
17578 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017579 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17580 {
17581 /* set mark */
17582 if (setmark_pos(name[1], &pos, fnum) == OK)
17583 rettv->vval.v_number = 0;
17584 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017585 else
17586 EMSG(_(e_invarg));
17587 }
17588 }
17589}
17590
17591/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017592 * "setqflist()" function
17593 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017594 static void
17595f_setqflist(argvars, rettv)
17596 typval_T *argvars;
17597 typval_T *rettv;
17598{
17599 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17600}
17601
17602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 * "setreg()" function
17604 */
17605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 typval_T *argvars;
17608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609{
17610 int regname;
17611 char_u *strregname;
17612 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017613 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 int append;
17615 char_u yank_type;
17616 long block_len;
17617
17618 block_len = -1;
17619 yank_type = MAUTO;
17620 append = FALSE;
17621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017622 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017625 if (strregname == NULL)
17626 return; /* type error; errmsg already given */
17627 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 if (regname == 0 || regname == '@')
17629 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017633 stropt = get_tv_string_chk(&argvars[2]);
17634 if (stropt == NULL)
17635 return; /* type error */
17636 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 switch (*stropt)
17638 {
17639 case 'a': case 'A': /* append */
17640 append = TRUE;
17641 break;
17642 case 'v': case 'c': /* character-wise selection */
17643 yank_type = MCHAR;
17644 break;
17645 case 'V': case 'l': /* line-wise selection */
17646 yank_type = MLINE;
17647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 case 'b': case Ctrl_V: /* block-wise selection */
17649 yank_type = MBLOCK;
17650 if (VIM_ISDIGIT(stropt[1]))
17651 {
17652 ++stropt;
17653 block_len = getdigits(&stropt) - 1;
17654 --stropt;
17655 }
17656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
17658 }
17659
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017660 if (argvars[1].v_type == VAR_LIST)
17661 {
17662 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017663 char_u **allocval;
17664 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017665 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017666 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017667 int len = argvars[1].vval.v_list->lv_len;
17668 listitem_T *li;
17669
Bram Moolenaar7d647822014-04-05 21:28:56 +020017670 /* First half: use for pointers to result lines; second half: use for
17671 * pointers to allocated copies. */
17672 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017673 if (lstval == NULL)
17674 return;
17675 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017676 allocval = lstval + len + 2;
17677 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017678
17679 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17680 li = li->li_next)
17681 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017682 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017683 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017684 goto free_lstval;
17685 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017686 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017687 /* Need to make a copy, next get_tv_string_buf_chk() will
17688 * overwrite the string. */
17689 strval = vim_strsave(buf);
17690 if (strval == NULL)
17691 goto free_lstval;
17692 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017693 }
17694 *curval++ = strval;
17695 }
17696 *curval++ = NULL;
17697
17698 write_reg_contents_lst(regname, lstval, -1,
17699 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017700free_lstval:
17701 while (curallocval > allocval)
17702 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017703 vim_free(lstval);
17704 }
17705 else
17706 {
17707 strval = get_tv_string_chk(&argvars[1]);
17708 if (strval == NULL)
17709 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017710 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714}
17715
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017716/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017717 * "settabvar()" function
17718 */
17719 static void
17720f_settabvar(argvars, rettv)
17721 typval_T *argvars;
17722 typval_T *rettv;
17723{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017724#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017725 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017726 tabpage_T *tp;
17727#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017728 char_u *varname, *tabvarname;
17729 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017730
17731 rettv->vval.v_number = 0;
17732
17733 if (check_restricted() || check_secure())
17734 return;
17735
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017736#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017737 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017738#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017739 varname = get_tv_string_chk(&argvars[1]);
17740 varp = &argvars[2];
17741
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017742 if (varname != NULL && varp != NULL
17743#ifdef FEAT_WINDOWS
17744 && tp != NULL
17745#endif
17746 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017747 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017748#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017749 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017750 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017751#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017752
17753 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17754 if (tabvarname != NULL)
17755 {
17756 STRCPY(tabvarname, "t:");
17757 STRCPY(tabvarname + 2, varname);
17758 set_var(tabvarname, varp, TRUE);
17759 vim_free(tabvarname);
17760 }
17761
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017762#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017763 /* Restore current tabpage */
17764 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017765 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017766#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017767 }
17768}
17769
17770/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017771 * "settabwinvar()" function
17772 */
17773 static void
17774f_settabwinvar(argvars, rettv)
17775 typval_T *argvars;
17776 typval_T *rettv;
17777{
17778 setwinvar(argvars, rettv, 1);
17779}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780
17781/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017782 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 typval_T *argvars;
17787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017789 setwinvar(argvars, rettv, 0);
17790}
17791
17792/*
17793 * "setwinvar()" and "settabwinvar()" functions
17794 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017795
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017796 static void
17797setwinvar(argvars, rettv, off)
17798 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017799 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017800 int off;
17801{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 win_T *win;
17803#ifdef FEAT_WINDOWS
17804 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017805 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017806 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807#endif
17808 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017809 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017811 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812
17813 if (check_restricted() || check_secure())
17814 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017815
17816#ifdef FEAT_WINDOWS
17817 if (off == 1)
17818 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17819 else
17820 tp = curtab;
17821#endif
17822 win = find_win_by_nr(&argvars[off], tp);
17823 varname = get_tv_string_chk(&argvars[off + 1]);
17824 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825
17826 if (win != NULL && varname != NULL && varp != NULL)
17827 {
17828#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017829 need_switch_win = !(tp == curtab && win == curwin);
17830 if (!need_switch_win
17831 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017834 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017836 long numval;
17837 char_u *strval;
17838 int error = FALSE;
17839
17840 ++varname;
17841 numval = get_tv_number_chk(varp, &error);
17842 strval = get_tv_string_buf_chk(varp, nbuf);
17843 if (!error && strval != NULL)
17844 set_option_value(varname, numval, strval, OPT_LOCAL);
17845 }
17846 else
17847 {
17848 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17849 if (winvarname != NULL)
17850 {
17851 STRCPY(winvarname, "w:");
17852 STRCPY(winvarname + 2, varname);
17853 set_var(winvarname, varp, TRUE);
17854 vim_free(winvarname);
17855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 }
17857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017859 if (need_switch_win)
17860 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861#endif
17862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863}
17864
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017865#ifdef FEAT_CRYPT
17866/*
17867 * "sha256({string})" function
17868 */
17869 static void
17870f_sha256(argvars, rettv)
17871 typval_T *argvars;
17872 typval_T *rettv;
17873{
17874 char_u *p;
17875
17876 p = get_tv_string(&argvars[0]);
17877 rettv->vval.v_string = vim_strsave(
17878 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17879 rettv->v_type = VAR_STRING;
17880}
17881#endif /* FEAT_CRYPT */
17882
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017884 * "shellescape({string})" function
17885 */
17886 static void
17887f_shellescape(argvars, rettv)
17888 typval_T *argvars;
17889 typval_T *rettv;
17890{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017891 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017892 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017893 rettv->v_type = VAR_STRING;
17894}
17895
17896/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017897 * shiftwidth() function
17898 */
17899 static void
17900f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017901 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017902 typval_T *rettv;
17903{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017904 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017905}
17906
17907/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 */
17910 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 typval_T *argvars;
17913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017915 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 p = get_tv_string(&argvars[0]);
17918 rettv->vval.v_string = vim_strsave(p);
17919 simplify_filename(rettv->vval.v_string); /* simplify in place */
17920 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921}
17922
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017923#ifdef FEAT_FLOAT
17924/*
17925 * "sin()" function
17926 */
17927 static void
17928f_sin(argvars, rettv)
17929 typval_T *argvars;
17930 typval_T *rettv;
17931{
17932 float_T f;
17933
17934 rettv->v_type = VAR_FLOAT;
17935 if (get_float_arg(argvars, &f) == OK)
17936 rettv->vval.v_float = sin(f);
17937 else
17938 rettv->vval.v_float = 0.0;
17939}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017940
17941/*
17942 * "sinh()" function
17943 */
17944 static void
17945f_sinh(argvars, rettv)
17946 typval_T *argvars;
17947 typval_T *rettv;
17948{
17949 float_T f;
17950
17951 rettv->v_type = VAR_FLOAT;
17952 if (get_float_arg(argvars, &f) == OK)
17953 rettv->vval.v_float = sinh(f);
17954 else
17955 rettv->vval.v_float = 0.0;
17956}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017957#endif
17958
Bram Moolenaar0d660222005-01-07 21:51:51 +000017959static int
17960#ifdef __BORLANDC__
17961 _RTLENTRYF
17962#endif
17963 item_compare __ARGS((const void *s1, const void *s2));
17964static int
17965#ifdef __BORLANDC__
17966 _RTLENTRYF
17967#endif
17968 item_compare2 __ARGS((const void *s1, const void *s2));
17969
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017970/* struct used in the array that's given to qsort() */
17971typedef struct
17972{
17973 listitem_T *item;
17974 int idx;
17975} sortItem_T;
17976
Bram Moolenaar0d660222005-01-07 21:51:51 +000017977static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017978static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017979static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017980static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017981static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017982static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017983static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017984static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017985#define ITEM_COMPARE_FAIL 999
17986
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017988 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990 static int
17991#ifdef __BORLANDC__
17992_RTLENTRYF
17993#endif
17994item_compare(s1, s2)
17995 const void *s1;
17996 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017998 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017999 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018001 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018002 int res;
18003 char_u numbuf1[NUMBUFLEN];
18004 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018006 si1 = (sortItem_T *)s1;
18007 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018008 tv1 = &si1->item->li_tv;
18009 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018010
18011 if (item_compare_numbers)
18012 {
18013 long v1 = get_tv_number(tv1);
18014 long v2 = get_tv_number(tv2);
18015
18016 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18017 }
18018
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018019 /* tv2string() puts quotes around a string and allocates memory. Don't do
18020 * that for string variables. Use a single quote when comparing with a
18021 * non-string to do what the docs promise. */
18022 if (tv1->v_type == VAR_STRING)
18023 {
18024 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18025 p1 = (char_u *)"'";
18026 else
18027 p1 = tv1->vval.v_string;
18028 }
18029 else
18030 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18031 if (tv2->v_type == VAR_STRING)
18032 {
18033 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18034 p2 = (char_u *)"'";
18035 else
18036 p2 = tv2->vval.v_string;
18037 }
18038 else
18039 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018040 if (p1 == NULL)
18041 p1 = (char_u *)"";
18042 if (p2 == NULL)
18043 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018044 if (!item_compare_numeric)
18045 {
18046 if (item_compare_ic)
18047 res = STRICMP(p1, p2);
18048 else
18049 res = STRCMP(p1, p2);
18050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018052 {
18053 double n1, n2;
18054 n1 = strtod((char *)p1, (char **)&p1);
18055 n2 = strtod((char *)p2, (char **)&p2);
18056 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18057 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018058
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018059 /* When the result would be zero, compare the item indexes. Makes the
18060 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018061 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018062 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018063
Bram Moolenaar0d660222005-01-07 21:51:51 +000018064 vim_free(tofree1);
18065 vim_free(tofree2);
18066 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
18069 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070#ifdef __BORLANDC__
18071_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073item_compare2(s1, s2)
18074 const void *s1;
18075 const void *s2;
18076{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018077 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018080 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 /* shortcut after failure in previous call; compare all items equal */
18084 if (item_compare_func_err)
18085 return 0;
18086
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018087 si1 = (sortItem_T *)s1;
18088 si2 = (sortItem_T *)s2;
18089
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018090 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018091 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018092 copy_tv(&si1->item->li_tv, &argv[0]);
18093 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094
18095 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018096 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018097 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18098 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 clear_tv(&argv[0]);
18100 clear_tv(&argv[1]);
18101
18102 if (res == FAIL)
18103 res = ITEM_COMPARE_FAIL;
18104 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18106 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018107 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018109
18110 /* When the result would be zero, compare the pointers themselves. Makes
18111 * the sort stable. */
18112 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018113 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018114
Bram Moolenaar0d660222005-01-07 21:51:51 +000018115 return res;
18116}
18117
18118/*
18119 * "sort({list})" function
18120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018122do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018123 typval_T *argvars;
18124 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018125 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126{
Bram Moolenaar33570922005-01-25 22:26:29 +000018127 list_T *l;
18128 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018129 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018130 long len;
18131 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132
Bram Moolenaar0d660222005-01-07 21:51:51 +000018133 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018134 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 else
18136 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018137 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018138 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018139 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18140 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018141 return;
18142 rettv->vval.v_list = l;
18143 rettv->v_type = VAR_LIST;
18144 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146 len = list_len(l);
18147 if (len <= 1)
18148 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149
Bram Moolenaar0d660222005-01-07 21:51:51 +000018150 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018151 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018152 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018153 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018154 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 if (argvars[1].v_type != VAR_UNKNOWN)
18156 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018157 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018158 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018159 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 else
18161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018162 int error = FALSE;
18163
18164 i = get_tv_number_chk(&argvars[1], &error);
18165 if (error)
18166 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018167 if (i == 1)
18168 item_compare_ic = TRUE;
18169 else
18170 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018171 if (item_compare_func != NULL)
18172 {
18173 if (STRCMP(item_compare_func, "n") == 0)
18174 {
18175 item_compare_func = NULL;
18176 item_compare_numeric = TRUE;
18177 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018178 else if (STRCMP(item_compare_func, "N") == 0)
18179 {
18180 item_compare_func = NULL;
18181 item_compare_numbers = TRUE;
18182 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018183 else if (STRCMP(item_compare_func, "i") == 0)
18184 {
18185 item_compare_func = NULL;
18186 item_compare_ic = TRUE;
18187 }
18188 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018189 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018190
18191 if (argvars[2].v_type != VAR_UNKNOWN)
18192 {
18193 /* optional third argument: {dict} */
18194 if (argvars[2].v_type != VAR_DICT)
18195 {
18196 EMSG(_(e_dictreq));
18197 return;
18198 }
18199 item_compare_selfdict = argvars[2].vval.v_dict;
18200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018204 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018205 if (ptrs == NULL)
18206 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207
Bram Moolenaar327aa022014-03-25 18:24:23 +010018208 i = 0;
18209 if (sort)
18210 {
18211 /* sort(): ptrs will be the list to sort */
18212 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018213 {
18214 ptrs[i].item = li;
18215 ptrs[i].idx = i;
18216 ++i;
18217 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018218
18219 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018220 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018221 /* test the compare function */
18222 if (item_compare_func != NULL
18223 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018225 EMSG(_("E702: Sort compare function failed"));
18226 else
18227 {
18228 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018229 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018230 item_compare_func == NULL ? item_compare : item_compare2);
18231
18232 if (!item_compare_func_err)
18233 {
18234 /* Clear the List and append the items in sorted order. */
18235 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18236 l->lv_len = 0;
18237 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018238 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018239 }
18240 }
18241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018244 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18245
18246 /* f_uniq(): ptrs will be a stack of items to remove */
18247 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018248 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018249 item_compare_func_ptr = item_compare_func
18250 ? item_compare2 : item_compare;
18251
18252 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18253 li = li->li_next)
18254 {
18255 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18256 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018257 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018258 if (item_compare_func_err)
18259 {
18260 EMSG(_("E882: Uniq compare function failed"));
18261 break;
18262 }
18263 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018265 if (!item_compare_func_err)
18266 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018267 while (--i >= 0)
18268 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018269 li = ptrs[i].item->li_next;
18270 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018271 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018272 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018273 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018275 list_fix_watch(l, li);
18276 listitem_free(li);
18277 l->lv_len--;
18278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018279 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 }
18281
18282 vim_free(ptrs);
18283 }
18284}
18285
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018286/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018287 * "sort({list})" function
18288 */
18289 static void
18290f_sort(argvars, rettv)
18291 typval_T *argvars;
18292 typval_T *rettv;
18293{
18294 do_sort_uniq(argvars, rettv, TRUE);
18295}
18296
18297/*
18298 * "uniq({list})" function
18299 */
18300 static void
18301f_uniq(argvars, rettv)
18302 typval_T *argvars;
18303 typval_T *rettv;
18304{
18305 do_sort_uniq(argvars, rettv, FALSE);
18306}
18307
18308/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018309 * "soundfold({word})" function
18310 */
18311 static void
18312f_soundfold(argvars, rettv)
18313 typval_T *argvars;
18314 typval_T *rettv;
18315{
18316 char_u *s;
18317
18318 rettv->v_type = VAR_STRING;
18319 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018320#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018321 rettv->vval.v_string = eval_soundfold(s);
18322#else
18323 rettv->vval.v_string = vim_strsave(s);
18324#endif
18325}
18326
18327/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018328 * "spellbadword()" function
18329 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018330 static void
18331f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018332 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018333 typval_T *rettv;
18334{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018335 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018336 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018337 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018338
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018339 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018340 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018341
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018342#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018343 if (argvars[0].v_type == VAR_UNKNOWN)
18344 {
18345 /* Find the start and length of the badly spelled word. */
18346 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18347 if (len != 0)
18348 word = ml_get_cursor();
18349 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018350 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018351 {
18352 char_u *str = get_tv_string_chk(&argvars[0]);
18353 int capcol = -1;
18354
18355 if (str != NULL)
18356 {
18357 /* Check the argument for spelling. */
18358 while (*str != NUL)
18359 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018360 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018361 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018362 {
18363 word = str;
18364 break;
18365 }
18366 str += len;
18367 }
18368 }
18369 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018370#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018372 list_append_string(rettv->vval.v_list, word, len);
18373 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018374 attr == HLF_SPB ? "bad" :
18375 attr == HLF_SPR ? "rare" :
18376 attr == HLF_SPL ? "local" :
18377 attr == HLF_SPC ? "caps" :
18378 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018379}
18380
18381/*
18382 * "spellsuggest()" function
18383 */
18384 static void
18385f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018386 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018387 typval_T *rettv;
18388{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018389#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018390 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018391 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018392 int maxcount;
18393 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018394 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018395 listitem_T *li;
18396 int need_capital = FALSE;
18397#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018398
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018399 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018400 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018401
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018402#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018403 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018404 {
18405 str = get_tv_string(&argvars[0]);
18406 if (argvars[1].v_type != VAR_UNKNOWN)
18407 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018408 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018409 if (maxcount <= 0)
18410 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018411 if (argvars[2].v_type != VAR_UNKNOWN)
18412 {
18413 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18414 if (typeerr)
18415 return;
18416 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018417 }
18418 else
18419 maxcount = 25;
18420
Bram Moolenaar4770d092006-01-12 23:22:24 +000018421 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018422
18423 for (i = 0; i < ga.ga_len; ++i)
18424 {
18425 str = ((char_u **)ga.ga_data)[i];
18426
18427 li = listitem_alloc();
18428 if (li == NULL)
18429 vim_free(str);
18430 else
18431 {
18432 li->li_tv.v_type = VAR_STRING;
18433 li->li_tv.v_lock = 0;
18434 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018435 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018436 }
18437 }
18438 ga_clear(&ga);
18439 }
18440#endif
18441}
18442
Bram Moolenaar0d660222005-01-07 21:51:51 +000018443 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018444f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 typval_T *argvars;
18446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447{
18448 char_u *str;
18449 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018450 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018451 regmatch_T regmatch;
18452 char_u patbuf[NUMBUFLEN];
18453 char_u *save_cpo;
18454 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018456 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458
18459 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18460 save_cpo = p_cpo;
18461 p_cpo = (char_u *)"";
18462
18463 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018464 if (argvars[1].v_type != VAR_UNKNOWN)
18465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018466 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18467 if (pat == NULL)
18468 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018469 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018470 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018471 }
18472 if (pat == NULL || *pat == NUL)
18473 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018474
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018475 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018477 if (typeerr)
18478 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479
Bram Moolenaar0d660222005-01-07 21:51:51 +000018480 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18481 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018484 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018485 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018486 if (*str == NUL)
18487 match = FALSE; /* empty item at the end */
18488 else
18489 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018490 if (match)
18491 end = regmatch.startp[0];
18492 else
18493 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018494 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18495 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018497 if (list_append_string(rettv->vval.v_list, str,
18498 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018499 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018500 }
18501 if (!match)
18502 break;
18503 /* Advance to just after the match. */
18504 if (regmatch.endp[0] > str)
18505 col = 0;
18506 else
18507 {
18508 /* Don't get stuck at the same match. */
18509#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018510 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018511#else
18512 col = 1;
18513#endif
18514 }
18515 str = regmatch.endp[0];
18516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517
Bram Moolenaar473de612013-06-08 18:19:48 +020018518 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522}
18523
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018524#ifdef FEAT_FLOAT
18525/*
18526 * "sqrt()" function
18527 */
18528 static void
18529f_sqrt(argvars, rettv)
18530 typval_T *argvars;
18531 typval_T *rettv;
18532{
18533 float_T f;
18534
18535 rettv->v_type = VAR_FLOAT;
18536 if (get_float_arg(argvars, &f) == OK)
18537 rettv->vval.v_float = sqrt(f);
18538 else
18539 rettv->vval.v_float = 0.0;
18540}
18541
18542/*
18543 * "str2float()" function
18544 */
18545 static void
18546f_str2float(argvars, rettv)
18547 typval_T *argvars;
18548 typval_T *rettv;
18549{
18550 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18551
18552 if (*p == '+')
18553 p = skipwhite(p + 1);
18554 (void)string2float(p, &rettv->vval.v_float);
18555 rettv->v_type = VAR_FLOAT;
18556}
18557#endif
18558
Bram Moolenaar2c932302006-03-18 21:42:09 +000018559/*
18560 * "str2nr()" function
18561 */
18562 static void
18563f_str2nr(argvars, rettv)
18564 typval_T *argvars;
18565 typval_T *rettv;
18566{
18567 int base = 10;
18568 char_u *p;
18569 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018570 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018571
18572 if (argvars[1].v_type != VAR_UNKNOWN)
18573 {
18574 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018575 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018576 {
18577 EMSG(_(e_invarg));
18578 return;
18579 }
18580 }
18581
18582 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018583 if (*p == '+')
18584 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018585 switch (base)
18586 {
18587 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18588 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18589 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18590 default: what = 0;
18591 }
18592 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018593 rettv->vval.v_number = n;
18594}
18595
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596#ifdef HAVE_STRFTIME
18597/*
18598 * "strftime({format}[, {time}])" function
18599 */
18600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018601f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018602 typval_T *argvars;
18603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604{
18605 char_u result_buf[256];
18606 struct tm *curtime;
18607 time_t seconds;
18608 char_u *p;
18609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018612 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018613 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 seconds = time(NULL);
18615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 curtime = localtime(&seconds);
18618 /* MSVC returns NULL for an invalid value of seconds. */
18619 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 else
18622 {
18623# ifdef FEAT_MBYTE
18624 vimconv_T conv;
18625 char_u *enc;
18626
18627 conv.vc_type = CONV_NONE;
18628 enc = enc_locale();
18629 convert_setup(&conv, p_enc, enc);
18630 if (conv.vc_type != CONV_NONE)
18631 p = string_convert(&conv, p, NULL);
18632# endif
18633 if (p != NULL)
18634 (void)strftime((char *)result_buf, sizeof(result_buf),
18635 (char *)p, curtime);
18636 else
18637 result_buf[0] = NUL;
18638
18639# ifdef FEAT_MBYTE
18640 if (conv.vc_type != CONV_NONE)
18641 vim_free(p);
18642 convert_setup(&conv, enc, p_enc);
18643 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 else
18646# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648
18649# ifdef FEAT_MBYTE
18650 /* Release conversion descriptors */
18651 convert_setup(&conv, NULL, NULL);
18652 vim_free(enc);
18653# endif
18654 }
18655}
18656#endif
18657
18658/*
18659 * "stridx()" function
18660 */
18661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 typval_T *argvars;
18664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665{
18666 char_u buf[NUMBUFLEN];
18667 char_u *needle;
18668 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018673 needle = get_tv_string_chk(&argvars[1]);
18674 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018676 if (needle == NULL || haystack == NULL)
18677 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
Bram Moolenaar33570922005-01-25 22:26:29 +000018679 if (argvars[2].v_type != VAR_UNKNOWN)
18680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018681 int error = FALSE;
18682
18683 start_idx = get_tv_number_chk(&argvars[2], &error);
18684 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018685 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018686 if (start_idx >= 0)
18687 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018688 }
18689
18690 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18691 if (pos != NULL)
18692 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693}
18694
18695/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018696 * "string()" function
18697 */
18698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018700 typval_T *argvars;
18701 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018702{
18703 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018704 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018707 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018708 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018709 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711}
18712
18713/*
18714 * "strlen()" function
18715 */
18716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 typval_T *argvars;
18719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018721 rettv->vval.v_number = (varnumber_T)(STRLEN(
18722 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723}
18724
18725/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018726 * "strchars()" function
18727 */
18728 static void
18729f_strchars(argvars, rettv)
18730 typval_T *argvars;
18731 typval_T *rettv;
18732{
18733 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018734 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018735#ifdef FEAT_MBYTE
18736 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018737 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018738#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018739
18740 if (argvars[1].v_type != VAR_UNKNOWN)
18741 skipcc = get_tv_number_chk(&argvars[1], NULL);
18742 if (skipcc < 0 || skipcc > 1)
18743 EMSG(_(e_invarg));
18744 else
18745 {
18746#ifdef FEAT_MBYTE
18747 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18748 while (*s != NUL)
18749 {
18750 func_mb_ptr2char_adv(&s);
18751 ++len;
18752 }
18753 rettv->vval.v_number = len;
18754#else
18755 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18756#endif
18757 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018758}
18759
18760/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018761 * "strdisplaywidth()" function
18762 */
18763 static void
18764f_strdisplaywidth(argvars, rettv)
18765 typval_T *argvars;
18766 typval_T *rettv;
18767{
18768 char_u *s = get_tv_string(&argvars[0]);
18769 int col = 0;
18770
18771 if (argvars[1].v_type != VAR_UNKNOWN)
18772 col = get_tv_number(&argvars[1]);
18773
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018774 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018775}
18776
18777/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018778 * "strwidth()" function
18779 */
18780 static void
18781f_strwidth(argvars, rettv)
18782 typval_T *argvars;
18783 typval_T *rettv;
18784{
18785 char_u *s = get_tv_string(&argvars[0]);
18786
18787 rettv->vval.v_number = (varnumber_T)(
18788#ifdef FEAT_MBYTE
18789 mb_string2cells(s, -1)
18790#else
18791 STRLEN(s)
18792#endif
18793 );
18794}
18795
18796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 * "strpart()" function
18798 */
18799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018801 typval_T *argvars;
18802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803{
18804 char_u *p;
18805 int n;
18806 int len;
18807 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 slen = (int)STRLEN(p);
18812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 n = get_tv_number_chk(&argvars[1], &error);
18814 if (error)
18815 len = 0;
18816 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018817 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 else
18819 len = slen - n; /* default len: all bytes that are available. */
18820
18821 /*
18822 * Only return the overlap between the specified part and the actual
18823 * string.
18824 */
18825 if (n < 0)
18826 {
18827 len += n;
18828 n = 0;
18829 }
18830 else if (n > slen)
18831 n = slen;
18832 if (len < 0)
18833 len = 0;
18834 else if (n + len > slen)
18835 len = slen - n;
18836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837 rettv->v_type = VAR_STRING;
18838 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839}
18840
18841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018842 * "strridx()" function
18843 */
18844 static void
18845f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018846 typval_T *argvars;
18847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018848{
18849 char_u buf[NUMBUFLEN];
18850 char_u *needle;
18851 char_u *haystack;
18852 char_u *rest;
18853 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018854 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018856 needle = get_tv_string_chk(&argvars[1]);
18857 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018858
18859 rettv->vval.v_number = -1;
18860 if (needle == NULL || haystack == NULL)
18861 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018862
18863 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018864 if (argvars[2].v_type != VAR_UNKNOWN)
18865 {
18866 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018867 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018868 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018869 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018870 }
18871 else
18872 end_idx = haystack_len;
18873
Bram Moolenaar0d660222005-01-07 21:51:51 +000018874 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018875 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018877 lastmatch = haystack + end_idx;
18878 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018879 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018880 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018881 for (rest = haystack; *rest != '\0'; ++rest)
18882 {
18883 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018884 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 break;
18886 lastmatch = rest;
18887 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889
18890 if (lastmatch == NULL)
18891 rettv->vval.v_number = -1;
18892 else
18893 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18894}
18895
18896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 * "strtrans()" function
18898 */
18899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018900f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 typval_T *argvars;
18902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904 rettv->v_type = VAR_STRING;
18905 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906}
18907
18908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909 * "submatch()" function
18910 */
18911 static void
18912f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018913 typval_T *argvars;
18914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915{
Bram Moolenaar41571762014-04-02 19:00:58 +020018916 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018917 int no;
18918 int retList = 0;
18919
18920 no = (int)get_tv_number_chk(&argvars[0], &error);
18921 if (error)
18922 return;
18923 error = FALSE;
18924 if (argvars[1].v_type != VAR_UNKNOWN)
18925 retList = get_tv_number_chk(&argvars[1], &error);
18926 if (error)
18927 return;
18928
18929 if (retList == 0)
18930 {
18931 rettv->v_type = VAR_STRING;
18932 rettv->vval.v_string = reg_submatch(no);
18933 }
18934 else
18935 {
18936 rettv->v_type = VAR_LIST;
18937 rettv->vval.v_list = reg_submatch_list(no);
18938 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939}
18940
18941/*
18942 * "substitute()" function
18943 */
18944 static void
18945f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 typval_T *argvars;
18947 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018948{
18949 char_u patbuf[NUMBUFLEN];
18950 char_u subbuf[NUMBUFLEN];
18951 char_u flagsbuf[NUMBUFLEN];
18952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018953 char_u *str = get_tv_string_chk(&argvars[0]);
18954 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18955 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18956 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18957
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018959 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18960 rettv->vval.v_string = NULL;
18961 else
18962 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963}
18964
18965/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018966 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972{
18973 int id = 0;
18974#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018975 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 long col;
18977 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018978 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018980 lnum = get_tv_lnum(argvars); /* -1 on type error */
18981 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18982 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018984 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018985 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018986 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987#endif
18988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990}
18991
18992/*
18993 * "synIDattr(id, what [, mode])" function
18994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018996f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018997 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999{
19000 char_u *p = NULL;
19001#ifdef FEAT_SYN_HL
19002 int id;
19003 char_u *what;
19004 char_u *mode;
19005 char_u modebuf[NUMBUFLEN];
19006 int modec;
19007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019008 id = get_tv_number(&argvars[0]);
19009 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019012 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019014 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 modec = 0; /* replace invalid with current */
19016 }
19017 else
19018 {
19019#ifdef FEAT_GUI
19020 if (gui.in_use)
19021 modec = 'g';
19022 else
19023#endif
19024 if (t_colors > 1)
19025 modec = 'c';
19026 else
19027 modec = 't';
19028 }
19029
19030
19031 switch (TOLOWER_ASC(what[0]))
19032 {
19033 case 'b':
19034 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19035 p = highlight_color(id, what, modec);
19036 else /* bold */
19037 p = highlight_has_attr(id, HL_BOLD, modec);
19038 break;
19039
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019040 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 p = highlight_color(id, what, modec);
19042 break;
19043
19044 case 'i':
19045 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19046 p = highlight_has_attr(id, HL_INVERSE, modec);
19047 else /* italic */
19048 p = highlight_has_attr(id, HL_ITALIC, modec);
19049 break;
19050
19051 case 'n': /* name */
19052 p = get_highlight_name(NULL, id - 1);
19053 break;
19054
19055 case 'r': /* reverse */
19056 p = highlight_has_attr(id, HL_INVERSE, modec);
19057 break;
19058
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019059 case 's':
19060 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19061 p = highlight_color(id, what, modec);
19062 else /* standout */
19063 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 break;
19065
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019066 case 'u':
19067 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19068 /* underline */
19069 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19070 else
19071 /* undercurl */
19072 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 break;
19074 }
19075
19076 if (p != NULL)
19077 p = vim_strsave(p);
19078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 rettv->v_type = VAR_STRING;
19080 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081}
19082
19083/*
19084 * "synIDtrans(id)" function
19085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019088 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090{
19091 int id;
19092
19093#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095
19096 if (id > 0)
19097 id = syn_get_final_id(id);
19098 else
19099#endif
19100 id = 0;
19101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103}
19104
19105/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019106 * "synconcealed(lnum, col)" function
19107 */
19108 static void
19109f_synconcealed(argvars, rettv)
19110 typval_T *argvars UNUSED;
19111 typval_T *rettv;
19112{
19113#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19114 long lnum;
19115 long col;
19116 int syntax_flags = 0;
19117 int cchar;
19118 int matchid = 0;
19119 char_u str[NUMBUFLEN];
19120#endif
19121
19122 rettv->v_type = VAR_LIST;
19123 rettv->vval.v_list = NULL;
19124
19125#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19126 lnum = get_tv_lnum(argvars); /* -1 on type error */
19127 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19128
19129 vim_memset(str, NUL, sizeof(str));
19130
19131 if (rettv_list_alloc(rettv) != FAIL)
19132 {
19133 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19134 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19135 && curwin->w_p_cole > 0)
19136 {
19137 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19138 syntax_flags = get_syntax_info(&matchid);
19139
19140 /* get the conceal character */
19141 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19142 {
19143 cchar = syn_get_sub_char();
19144 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19145 cchar = lcs_conceal;
19146 if (cchar != NUL)
19147 {
19148# ifdef FEAT_MBYTE
19149 if (has_mbyte)
19150 (*mb_char2bytes)(cchar, str);
19151 else
19152# endif
19153 str[0] = cchar;
19154 }
19155 }
19156 }
19157
19158 list_append_number(rettv->vval.v_list,
19159 (syntax_flags & HL_CONCEAL) != 0);
19160 /* -1 to auto-determine strlen */
19161 list_append_string(rettv->vval.v_list, str, -1);
19162 list_append_number(rettv->vval.v_list, matchid);
19163 }
19164#endif
19165}
19166
19167/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019168 * "synstack(lnum, col)" function
19169 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019170 static void
19171f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019172 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019173 typval_T *rettv;
19174{
19175#ifdef FEAT_SYN_HL
19176 long lnum;
19177 long col;
19178 int i;
19179 int id;
19180#endif
19181
19182 rettv->v_type = VAR_LIST;
19183 rettv->vval.v_list = NULL;
19184
19185#ifdef FEAT_SYN_HL
19186 lnum = get_tv_lnum(argvars); /* -1 on type error */
19187 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19188
19189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019190 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019191 && rettv_list_alloc(rettv) != FAIL)
19192 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019193 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019194 for (i = 0; ; ++i)
19195 {
19196 id = syn_get_stack_item(i);
19197 if (id < 0)
19198 break;
19199 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19200 break;
19201 }
19202 }
19203#endif
19204}
19205
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019207get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 typval_T *argvars;
19209 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019210 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019212 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019214 char_u *infile = NULL;
19215 char_u buf[NUMBUFLEN];
19216 int err = FALSE;
19217 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019218 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019219 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019221 rettv->v_type = VAR_STRING;
19222 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019223 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019224 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019225
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019227 {
19228 /*
19229 * Write the string to a temp file, to be used for input of the shell
19230 * command.
19231 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019232 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019233 {
19234 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019235 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019236 }
19237
19238 fd = mch_fopen((char *)infile, WRITEBIN);
19239 if (fd == NULL)
19240 {
19241 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019242 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019243 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019244 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019245 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019246 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19247 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019248 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019249 else
19250 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019251 size_t len;
19252
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019253 p = get_tv_string_buf_chk(&argvars[1], buf);
19254 if (p == NULL)
19255 {
19256 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019257 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019258 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019259 len = STRLEN(p);
19260 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019261 err = TRUE;
19262 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019263 if (fclose(fd) != 0)
19264 err = TRUE;
19265 if (err)
19266 {
19267 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019268 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019269 }
19270 }
19271
Bram Moolenaar52a72462014-08-29 15:53:52 +020019272 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19273 * echoes typeahead, that messes up the display. */
19274 if (!msg_silent)
19275 flags += SHELL_COOKED;
19276
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019277 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019279 int len;
19280 listitem_T *li;
19281 char_u *s = NULL;
19282 char_u *start;
19283 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285
Bram Moolenaar52a72462014-08-29 15:53:52 +020019286 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019287 if (res == NULL)
19288 goto errret;
19289
19290 list = list_alloc();
19291 if (list == NULL)
19292 goto errret;
19293
19294 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019296 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019297 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019298 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019299 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019300
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019301 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019302 if (s == NULL)
19303 goto errret;
19304
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019305 for (p = s; start < end; ++p, ++start)
19306 *p = *start == NUL ? NL : *start;
19307 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308
19309 li = listitem_alloc();
19310 if (li == NULL)
19311 {
19312 vim_free(s);
19313 goto errret;
19314 }
19315 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019316 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317 li->li_tv.vval.v_string = s;
19318 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019320
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019321 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322 rettv->v_type = VAR_LIST;
19323 rettv->vval.v_list = list;
19324 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019326 else
19327 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019328 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019329#ifdef USE_CR
19330 /* translate <CR> into <NL> */
19331 if (res != NULL)
19332 {
19333 char_u *s;
19334
19335 for (s = res; *s; ++s)
19336 {
19337 if (*s == CAR)
19338 *s = NL;
19339 }
19340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341#else
19342# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019343 /* translate <CR><NL> into <NL> */
19344 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019346 char_u *s, *d;
19347
19348 d = res;
19349 for (s = res; *s; ++s)
19350 {
19351 if (s[0] == CAR && s[1] == NL)
19352 ++s;
19353 *d++ = *s;
19354 }
19355 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357# endif
19358#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019359 rettv->vval.v_string = res;
19360 res = NULL;
19361 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019362
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019363errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019364 if (infile != NULL)
19365 {
19366 mch_remove(infile);
19367 vim_free(infile);
19368 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019369 if (res != NULL)
19370 vim_free(res);
19371 if (list != NULL)
19372 list_free(list, TRUE);
19373}
19374
19375/*
19376 * "system()" function
19377 */
19378 static void
19379f_system(argvars, rettv)
19380 typval_T *argvars;
19381 typval_T *rettv;
19382{
19383 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19384}
19385
19386/*
19387 * "systemlist()" function
19388 */
19389 static void
19390f_systemlist(argvars, rettv)
19391 typval_T *argvars;
19392 typval_T *rettv;
19393{
19394 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395}
19396
19397/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019398 * "tabpagebuflist()" function
19399 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019400 static void
19401f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019402 typval_T *argvars UNUSED;
19403 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019404{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019405#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019406 tabpage_T *tp;
19407 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019408
19409 if (argvars[0].v_type == VAR_UNKNOWN)
19410 wp = firstwin;
19411 else
19412 {
19413 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19414 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019415 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019416 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019417 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019418 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019419 for (; wp != NULL; wp = wp->w_next)
19420 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019421 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019422 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019423 }
19424#endif
19425}
19426
19427
19428/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019429 * "tabpagenr()" function
19430 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019431 static void
19432f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019433 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019434 typval_T *rettv;
19435{
19436 int nr = 1;
19437#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019438 char_u *arg;
19439
19440 if (argvars[0].v_type != VAR_UNKNOWN)
19441 {
19442 arg = get_tv_string_chk(&argvars[0]);
19443 nr = 0;
19444 if (arg != NULL)
19445 {
19446 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019447 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019448 else
19449 EMSG2(_(e_invexpr2), arg);
19450 }
19451 }
19452 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019453 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019454#endif
19455 rettv->vval.v_number = nr;
19456}
19457
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019458
19459#ifdef FEAT_WINDOWS
19460static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19461
19462/*
19463 * Common code for tabpagewinnr() and winnr().
19464 */
19465 static int
19466get_winnr(tp, argvar)
19467 tabpage_T *tp;
19468 typval_T *argvar;
19469{
19470 win_T *twin;
19471 int nr = 1;
19472 win_T *wp;
19473 char_u *arg;
19474
19475 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19476 if (argvar->v_type != VAR_UNKNOWN)
19477 {
19478 arg = get_tv_string_chk(argvar);
19479 if (arg == NULL)
19480 nr = 0; /* type error; errmsg already given */
19481 else if (STRCMP(arg, "$") == 0)
19482 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19483 else if (STRCMP(arg, "#") == 0)
19484 {
19485 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19486 if (twin == NULL)
19487 nr = 0;
19488 }
19489 else
19490 {
19491 EMSG2(_(e_invexpr2), arg);
19492 nr = 0;
19493 }
19494 }
19495
19496 if (nr > 0)
19497 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19498 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019499 {
19500 if (wp == NULL)
19501 {
19502 /* didn't find it in this tabpage */
19503 nr = 0;
19504 break;
19505 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019506 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019507 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019508 return nr;
19509}
19510#endif
19511
19512/*
19513 * "tabpagewinnr()" function
19514 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019515 static void
19516f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019517 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019518 typval_T *rettv;
19519{
19520 int nr = 1;
19521#ifdef FEAT_WINDOWS
19522 tabpage_T *tp;
19523
19524 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19525 if (tp == NULL)
19526 nr = 0;
19527 else
19528 nr = get_winnr(tp, &argvars[1]);
19529#endif
19530 rettv->vval.v_number = nr;
19531}
19532
19533
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019534/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019535 * "tagfiles()" function
19536 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019537 static void
19538f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019539 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019540 typval_T *rettv;
19541{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019542 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019543 tagname_T tn;
19544 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019546 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019547 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019548 fname = alloc(MAXPATHL);
19549 if (fname == NULL)
19550 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019551
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019552 for (first = TRUE; ; first = FALSE)
19553 if (get_tagfname(&tn, first, fname) == FAIL
19554 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019555 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019556 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019557 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019558}
19559
19560/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019561 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019562 */
19563 static void
19564f_taglist(argvars, rettv)
19565 typval_T *argvars;
19566 typval_T *rettv;
19567{
19568 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019569
19570 tag_pattern = get_tv_string(&argvars[0]);
19571
19572 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019573 if (*tag_pattern == NUL)
19574 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019575
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019576 if (rettv_list_alloc(rettv) == OK)
19577 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019578}
19579
19580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 * "tempname()" function
19582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587{
19588 static int x = 'A';
19589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019591 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592
19593 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19594 * names. Skip 'I' and 'O', they are used for shell redirection. */
19595 do
19596 {
19597 if (x == 'Z')
19598 x = '0';
19599 else if (x == '9')
19600 x = 'A';
19601 else
19602 {
19603#ifdef EBCDIC
19604 if (x == 'I')
19605 x = 'J';
19606 else if (x == 'R')
19607 x = 'S';
19608 else
19609#endif
19610 ++x;
19611 }
19612 } while (x == 'I' || x == 'O');
19613}
19614
19615/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019616 * "test(list)" function: Just checking the walls...
19617 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019618 static void
19619f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019620 typval_T *argvars UNUSED;
19621 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019622{
19623 /* Used for unit testing. Change the code below to your liking. */
19624#if 0
19625 listitem_T *li;
19626 list_T *l;
19627 char_u *bad, *good;
19628
19629 if (argvars[0].v_type != VAR_LIST)
19630 return;
19631 l = argvars[0].vval.v_list;
19632 if (l == NULL)
19633 return;
19634 li = l->lv_first;
19635 if (li == NULL)
19636 return;
19637 bad = get_tv_string(&li->li_tv);
19638 li = li->li_next;
19639 if (li == NULL)
19640 return;
19641 good = get_tv_string(&li->li_tv);
19642 rettv->vval.v_number = test_edit_score(bad, good);
19643#endif
19644}
19645
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019646#ifdef FEAT_FLOAT
19647/*
19648 * "tan()" function
19649 */
19650 static void
19651f_tan(argvars, rettv)
19652 typval_T *argvars;
19653 typval_T *rettv;
19654{
19655 float_T f;
19656
19657 rettv->v_type = VAR_FLOAT;
19658 if (get_float_arg(argvars, &f) == OK)
19659 rettv->vval.v_float = tan(f);
19660 else
19661 rettv->vval.v_float = 0.0;
19662}
19663
19664/*
19665 * "tanh()" function
19666 */
19667 static void
19668f_tanh(argvars, rettv)
19669 typval_T *argvars;
19670 typval_T *rettv;
19671{
19672 float_T f;
19673
19674 rettv->v_type = VAR_FLOAT;
19675 if (get_float_arg(argvars, &f) == OK)
19676 rettv->vval.v_float = tanh(f);
19677 else
19678 rettv->vval.v_float = 0.0;
19679}
19680#endif
19681
Bram Moolenaard52d9742005-08-21 22:20:28 +000019682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 * "tolower(string)" function
19684 */
19685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 typval_T *argvars;
19688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689{
19690 char_u *p;
19691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 p = vim_strsave(get_tv_string(&argvars[0]));
19693 rettv->v_type = VAR_STRING;
19694 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695
19696 if (p != NULL)
19697 while (*p != NUL)
19698 {
19699#ifdef FEAT_MBYTE
19700 int l;
19701
19702 if (enc_utf8)
19703 {
19704 int c, lc;
19705
19706 c = utf_ptr2char(p);
19707 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019708 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 /* TODO: reallocate string when byte count changes. */
19710 if (utf_char2len(lc) == l)
19711 utf_char2bytes(lc, p);
19712 p += l;
19713 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019714 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 p += l; /* skip multi-byte character */
19716 else
19717#endif
19718 {
19719 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19720 ++p;
19721 }
19722 }
19723}
19724
19725/*
19726 * "toupper(string)" function
19727 */
19728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 typval_T *argvars;
19731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019734 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019738 * "tr(string, fromstr, tostr)" function
19739 */
19740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 typval_T *argvars;
19743 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019744{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019745 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019746 char_u *fromstr;
19747 char_u *tostr;
19748 char_u *p;
19749#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019750 int inlen;
19751 int fromlen;
19752 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019753 int idx;
19754 char_u *cpstr;
19755 int cplen;
19756 int first = TRUE;
19757#endif
19758 char_u buf[NUMBUFLEN];
19759 char_u buf2[NUMBUFLEN];
19760 garray_T ga;
19761
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019762 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019763 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19764 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019765
19766 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767 rettv->v_type = VAR_STRING;
19768 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019769 if (fromstr == NULL || tostr == NULL)
19770 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019771 ga_init2(&ga, (int)sizeof(char), 80);
19772
19773#ifdef FEAT_MBYTE
19774 if (!has_mbyte)
19775#endif
19776 /* not multi-byte: fromstr and tostr must be the same length */
19777 if (STRLEN(fromstr) != STRLEN(tostr))
19778 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019779#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019780error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019781#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019782 EMSG2(_(e_invarg2), fromstr);
19783 ga_clear(&ga);
19784 return;
19785 }
19786
19787 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019788 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019789 {
19790#ifdef FEAT_MBYTE
19791 if (has_mbyte)
19792 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019793 inlen = (*mb_ptr2len)(in_str);
19794 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019795 cplen = inlen;
19796 idx = 0;
19797 for (p = fromstr; *p != NUL; p += fromlen)
19798 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019799 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019800 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019801 {
19802 for (p = tostr; *p != NUL; p += tolen)
19803 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019804 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019805 if (idx-- == 0)
19806 {
19807 cplen = tolen;
19808 cpstr = p;
19809 break;
19810 }
19811 }
19812 if (*p == NUL) /* tostr is shorter than fromstr */
19813 goto error;
19814 break;
19815 }
19816 ++idx;
19817 }
19818
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019819 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019820 {
19821 /* Check that fromstr and tostr have the same number of
19822 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019823 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019824 first = FALSE;
19825 for (p = tostr; *p != NUL; p += tolen)
19826 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019827 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019828 --idx;
19829 }
19830 if (idx != 0)
19831 goto error;
19832 }
19833
Bram Moolenaarcde88542015-08-11 19:14:00 +020019834 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019835 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019836 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019837
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019838 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019839 }
19840 else
19841#endif
19842 {
19843 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019844 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019845 if (p != NULL)
19846 ga_append(&ga, tostr[p - fromstr]);
19847 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019848 ga_append(&ga, *in_str);
19849 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019850 }
19851 }
19852
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019853 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019854 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019855 ga_append(&ga, NUL);
19856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019858}
19859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019860#ifdef FEAT_FLOAT
19861/*
19862 * "trunc({float})" function
19863 */
19864 static void
19865f_trunc(argvars, rettv)
19866 typval_T *argvars;
19867 typval_T *rettv;
19868{
19869 float_T f;
19870
19871 rettv->v_type = VAR_FLOAT;
19872 if (get_float_arg(argvars, &f) == OK)
19873 /* trunc() is not in C90, use floor() or ceil() instead. */
19874 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19875 else
19876 rettv->vval.v_float = 0.0;
19877}
19878#endif
19879
Bram Moolenaar8299df92004-07-10 09:47:34 +000019880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 * "type(expr)" function
19882 */
19883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019885 typval_T *argvars;
19886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019888 int n;
19889
19890 switch (argvars[0].v_type)
19891 {
19892 case VAR_NUMBER: n = 0; break;
19893 case VAR_STRING: n = 1; break;
19894 case VAR_FUNC: n = 2; break;
19895 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019896 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019897#ifdef FEAT_FLOAT
19898 case VAR_FLOAT: n = 5; break;
19899#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019900 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19901 }
19902 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903}
19904
19905/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019906 * "undofile(name)" function
19907 */
19908 static void
19909f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019910 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019911 typval_T *rettv;
19912{
19913 rettv->v_type = VAR_STRING;
19914#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019915 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019916 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019917
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019918 if (*fname == NUL)
19919 {
19920 /* If there is no file name there will be no undo file. */
19921 rettv->vval.v_string = NULL;
19922 }
19923 else
19924 {
19925 char_u *ffname = FullName_save(fname, FALSE);
19926
19927 if (ffname != NULL)
19928 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19929 vim_free(ffname);
19930 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019931 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019932#else
19933 rettv->vval.v_string = NULL;
19934#endif
19935}
19936
19937/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019938 * "undotree()" function
19939 */
19940 static void
19941f_undotree(argvars, rettv)
19942 typval_T *argvars UNUSED;
19943 typval_T *rettv;
19944{
19945 if (rettv_dict_alloc(rettv) == OK)
19946 {
19947 dict_T *dict = rettv->vval.v_dict;
19948 list_T *list;
19949
Bram Moolenaar730cde92010-06-27 05:18:54 +020019950 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019951 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019952 dict_add_nr_str(dict, "save_last",
19953 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019954 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19955 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019956 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019957
19958 list = list_alloc();
19959 if (list != NULL)
19960 {
19961 u_eval_tree(curbuf->b_u_oldhead, list);
19962 dict_add_list(dict, "entries", list);
19963 }
19964 }
19965}
19966
19967/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019968 * "values(dict)" function
19969 */
19970 static void
19971f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 typval_T *argvars;
19973 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019974{
19975 dict_list(argvars, rettv, 1);
19976}
19977
19978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 * "virtcol(string)" function
19980 */
19981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 typval_T *argvars;
19984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985{
19986 colnr_T vcol = 0;
19987 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019988 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019990 fp = var2fpos(&argvars[0], FALSE, &fnum);
19991 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19992 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 {
19994 getvvcol(curwin, fp, NULL, NULL, &vcol);
19995 ++vcol;
19996 }
19997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999}
20000
20001/*
20002 * "visualmode()" function
20003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020006 typval_T *argvars UNUSED;
20007 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 char_u str[2];
20010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 str[0] = curbuf->b_visual_mode_eval;
20013 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020014 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015
20016 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020017 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019}
20020
20021/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020022 * "wildmenumode()" function
20023 */
20024 static void
20025f_wildmenumode(argvars, rettv)
20026 typval_T *argvars UNUSED;
20027 typval_T *rettv UNUSED;
20028{
20029#ifdef FEAT_WILDMENU
20030 if (wild_menu_showing)
20031 rettv->vval.v_number = 1;
20032#endif
20033}
20034
20035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 * "winbufnr(nr)" function
20037 */
20038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 typval_T *argvars;
20041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042{
20043 win_T *wp;
20044
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020045 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050}
20051
20052/*
20053 * "wincol()" function
20054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059{
20060 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062}
20063
20064/*
20065 * "winheight(nr)" function
20066 */
20067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020069 typval_T *argvars;
20070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071{
20072 win_T *wp;
20073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020074 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020078 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079}
20080
20081/*
20082 * "winline()" function
20083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088{
20089 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091}
20092
20093/*
20094 * "winnr()" function
20095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100{
20101 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020102
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020104 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107}
20108
20109/*
20110 * "winrestcmd()" function
20111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020113f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116{
20117#ifdef FEAT_WINDOWS
20118 win_T *wp;
20119 int winnr = 1;
20120 garray_T ga;
20121 char_u buf[50];
20122
20123 ga_init2(&ga, (int)sizeof(char), 70);
20124 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20125 {
20126 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20127 ga_concat(&ga, buf);
20128# ifdef FEAT_VERTSPLIT
20129 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20130 ga_concat(&ga, buf);
20131# endif
20132 ++winnr;
20133 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020134 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141}
20142
20143/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020144 * "winrestview()" function
20145 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020146 static void
20147f_winrestview(argvars, rettv)
20148 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020149 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020150{
20151 dict_T *dict;
20152
20153 if (argvars[0].v_type != VAR_DICT
20154 || (dict = argvars[0].vval.v_dict) == NULL)
20155 EMSG(_(e_invarg));
20156 else
20157 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020158 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20159 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20160 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20161 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020162#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020163 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20164 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020165#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020166 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20167 {
20168 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20169 curwin->w_set_curswant = FALSE;
20170 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020171
Bram Moolenaar82c25852014-05-28 16:47:16 +020020172 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20173 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020174#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020175 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20176 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020177#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020178 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20179 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20180 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20181 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182
20183 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020184 win_new_height(curwin, curwin->w_height);
20185# ifdef FEAT_VERTSPLIT
20186 win_new_width(curwin, W_WIDTH(curwin));
20187# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020188 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020189
Bram Moolenaarb851a962014-10-31 15:45:52 +010020190 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020191 curwin->w_topline = 1;
20192 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20193 curwin->w_topline = curbuf->b_ml.ml_line_count;
20194#ifdef FEAT_DIFF
20195 check_topfill(curwin, TRUE);
20196#endif
20197 }
20198}
20199
20200/*
20201 * "winsaveview()" function
20202 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020203 static void
20204f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020205 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020206 typval_T *rettv;
20207{
20208 dict_T *dict;
20209
Bram Moolenaara800b422010-06-27 01:15:55 +020020210 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020211 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020212 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020213
20214 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20215 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20216#ifdef FEAT_VIRTUALEDIT
20217 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20218#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020219 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020220 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20221
20222 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20223#ifdef FEAT_DIFF
20224 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20225#endif
20226 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20227 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20228}
20229
20230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 * "winwidth(nr)" function
20232 */
20233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 typval_T *argvars;
20236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237{
20238 win_T *wp;
20239
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020240 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 else
20244#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020245 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248#endif
20249}
20250
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020252 * "wordcount()" function
20253 */
20254 static void
20255f_wordcount(argvars, rettv)
20256 typval_T *argvars UNUSED;
20257 typval_T *rettv;
20258{
20259 if (rettv_dict_alloc(rettv) == FAIL)
20260 return;
20261 cursor_pos_info(rettv->vval.v_dict);
20262}
20263
20264/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020265 * Write list of strings to file
20266 */
20267 static int
20268write_list(fd, list, binary)
20269 FILE *fd;
20270 list_T *list;
20271 int binary;
20272{
20273 listitem_T *li;
20274 int c;
20275 int ret = OK;
20276 char_u *s;
20277
20278 for (li = list->lv_first; li != NULL; li = li->li_next)
20279 {
20280 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20281 {
20282 if (*s == '\n')
20283 c = putc(NUL, fd);
20284 else
20285 c = putc(*s, fd);
20286 if (c == EOF)
20287 {
20288 ret = FAIL;
20289 break;
20290 }
20291 }
20292 if (!binary || li->li_next != NULL)
20293 if (putc('\n', fd) == EOF)
20294 {
20295 ret = FAIL;
20296 break;
20297 }
20298 if (ret == FAIL)
20299 {
20300 EMSG(_(e_write));
20301 break;
20302 }
20303 }
20304 return ret;
20305}
20306
20307/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020308 * "writefile()" function
20309 */
20310 static void
20311f_writefile(argvars, rettv)
20312 typval_T *argvars;
20313 typval_T *rettv;
20314{
20315 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020316 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020317 char_u *fname;
20318 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020319 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020320
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020321 if (check_restricted() || check_secure())
20322 return;
20323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020324 if (argvars[0].v_type != VAR_LIST)
20325 {
20326 EMSG2(_(e_listarg), "writefile()");
20327 return;
20328 }
20329 if (argvars[0].vval.v_list == NULL)
20330 return;
20331
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020332 if (argvars[2].v_type != VAR_UNKNOWN)
20333 {
20334 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20335 binary = TRUE;
20336 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20337 append = TRUE;
20338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020339
20340 /* Always open the file in binary mode, library functions have a mind of
20341 * their own about CR-LF conversion. */
20342 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020343 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20344 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020345 {
20346 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20347 ret = -1;
20348 }
20349 else
20350 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020351 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20352 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020353 fclose(fd);
20354 }
20355
20356 rettv->vval.v_number = ret;
20357}
20358
20359/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020360 * "xor(expr, expr)" function
20361 */
20362 static void
20363f_xor(argvars, rettv)
20364 typval_T *argvars;
20365 typval_T *rettv;
20366{
20367 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20368 ^ get_tv_number_chk(&argvars[1], NULL);
20369}
20370
20371
20372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020374 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 */
20376 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020377var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020379 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020380 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020382 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020384 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385
Bram Moolenaara5525202006-03-02 22:52:09 +000020386 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020387 if (varp->v_type == VAR_LIST)
20388 {
20389 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020390 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020391 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020392 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020393
20394 l = varp->vval.v_list;
20395 if (l == NULL)
20396 return NULL;
20397
20398 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020399 pos.lnum = list_find_nr(l, 0L, &error);
20400 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020401 return NULL; /* invalid line number */
20402
20403 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020404 pos.col = list_find_nr(l, 1L, &error);
20405 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020406 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020407 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020408
20409 /* We accept "$" for the column number: last column. */
20410 li = list_find(l, 1L);
20411 if (li != NULL && li->li_tv.v_type == VAR_STRING
20412 && li->li_tv.vval.v_string != NULL
20413 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20414 pos.col = len + 1;
20415
Bram Moolenaara5525202006-03-02 22:52:09 +000020416 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020417 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020418 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020419 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020420
Bram Moolenaara5525202006-03-02 22:52:09 +000020421#ifdef FEAT_VIRTUALEDIT
20422 /* Get the virtual offset. Defaults to zero. */
20423 pos.coladd = list_find_nr(l, 2L, &error);
20424 if (error)
20425 pos.coladd = 0;
20426#endif
20427
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020428 return &pos;
20429 }
20430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020431 name = get_tv_string_chk(varp);
20432 if (name == NULL)
20433 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020434 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020436 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20437 {
20438 if (VIsual_active)
20439 return &VIsual;
20440 return &curwin->w_cursor;
20441 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020442 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020444 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20446 return NULL;
20447 return pp;
20448 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020449
20450#ifdef FEAT_VIRTUALEDIT
20451 pos.coladd = 0;
20452#endif
20453
Bram Moolenaar477933c2007-07-17 14:32:23 +000020454 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020455 {
20456 pos.col = 0;
20457 if (name[1] == '0') /* "w0": first visible line */
20458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020459 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020460 pos.lnum = curwin->w_topline;
20461 return &pos;
20462 }
20463 else if (name[1] == '$') /* "w$": last visible line */
20464 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020465 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020466 pos.lnum = curwin->w_botline - 1;
20467 return &pos;
20468 }
20469 }
20470 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020472 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 {
20474 pos.lnum = curbuf->b_ml.ml_line_count;
20475 pos.col = 0;
20476 }
20477 else
20478 {
20479 pos.lnum = curwin->w_cursor.lnum;
20480 pos.col = (colnr_T)STRLEN(ml_get_curline());
20481 }
20482 return &pos;
20483 }
20484 return NULL;
20485}
20486
20487/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020488 * Convert list in "arg" into a position and optional file number.
20489 * When "fnump" is NULL there is no file number, only 3 items.
20490 * Note that the column is passed on as-is, the caller may want to decrement
20491 * it to use 1 for the first column.
20492 * Return FAIL when conversion is not possible, doesn't check the position for
20493 * validity.
20494 */
20495 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020496list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020497 typval_T *arg;
20498 pos_T *posp;
20499 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020500 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020501{
20502 list_T *l = arg->vval.v_list;
20503 long i = 0;
20504 long n;
20505
Bram Moolenaar493c1782014-05-28 14:34:46 +020020506 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20507 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020508 if (arg->v_type != VAR_LIST
20509 || l == NULL
20510 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020511 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020512 return FAIL;
20513
20514 if (fnump != NULL)
20515 {
20516 n = list_find_nr(l, i++, NULL); /* fnum */
20517 if (n < 0)
20518 return FAIL;
20519 if (n == 0)
20520 n = curbuf->b_fnum; /* current buffer */
20521 *fnump = n;
20522 }
20523
20524 n = list_find_nr(l, i++, NULL); /* lnum */
20525 if (n < 0)
20526 return FAIL;
20527 posp->lnum = n;
20528
20529 n = list_find_nr(l, i++, NULL); /* col */
20530 if (n < 0)
20531 return FAIL;
20532 posp->col = n;
20533
20534#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020535 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020536 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020537 posp->coladd = 0;
20538 else
20539 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020540#endif
20541
Bram Moolenaar493c1782014-05-28 14:34:46 +020020542 if (curswantp != NULL)
20543 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20544
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020545 return OK;
20546}
20547
20548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 * Get the length of an environment variable name.
20550 * Advance "arg" to the first character after the name.
20551 * Return 0 for error.
20552 */
20553 static int
20554get_env_len(arg)
20555 char_u **arg;
20556{
20557 char_u *p;
20558 int len;
20559
20560 for (p = *arg; vim_isIDc(*p); ++p)
20561 ;
20562 if (p == *arg) /* no name found */
20563 return 0;
20564
20565 len = (int)(p - *arg);
20566 *arg = p;
20567 return len;
20568}
20569
20570/*
20571 * Get the length of the name of a function or internal variable.
20572 * "arg" is advanced to the first non-white character after the name.
20573 * Return 0 if something is wrong.
20574 */
20575 static int
20576get_id_len(arg)
20577 char_u **arg;
20578{
20579 char_u *p;
20580 int len;
20581
20582 /* Find the end of the name. */
20583 for (p = *arg; eval_isnamec(*p); ++p)
20584 ;
20585 if (p == *arg) /* no name found */
20586 return 0;
20587
20588 len = (int)(p - *arg);
20589 *arg = skipwhite(p);
20590
20591 return len;
20592}
20593
20594/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020595 * Get the length of the name of a variable or function.
20596 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020598 * Return -1 if curly braces expansion failed.
20599 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 * If the name contains 'magic' {}'s, expand them and return the
20601 * expanded name in an allocated string via 'alias' - caller must free.
20602 */
20603 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020604get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 char_u **arg;
20606 char_u **alias;
20607 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020608 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609{
20610 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 char_u *p;
20612 char_u *expr_start;
20613 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614
20615 *alias = NULL; /* default to no alias */
20616
20617 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20618 && (*arg)[2] == (int)KE_SNR)
20619 {
20620 /* hard coded <SNR>, already translated */
20621 *arg += 3;
20622 return get_id_len(arg) + 3;
20623 }
20624 len = eval_fname_script(*arg);
20625 if (len > 0)
20626 {
20627 /* literal "<SID>", "s:" or "<SNR>" */
20628 *arg += len;
20629 }
20630
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020634 p = find_name_end(*arg, &expr_start, &expr_end,
20635 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 if (expr_start != NULL)
20637 {
20638 char_u *temp_string;
20639
20640 if (!evaluate)
20641 {
20642 len += (int)(p - *arg);
20643 *arg = skipwhite(p);
20644 return len;
20645 }
20646
20647 /*
20648 * Include any <SID> etc in the expanded string:
20649 * Thus the -len here.
20650 */
20651 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20652 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020653 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 *alias = temp_string;
20655 *arg = skipwhite(p);
20656 return (int)STRLEN(temp_string);
20657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658
20659 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020660 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 EMSG2(_(e_invexpr2), *arg);
20662
20663 return len;
20664}
20665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020666/*
20667 * Find the end of a variable or function name, taking care of magic braces.
20668 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20669 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020670 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020671 * Return a pointer to just after the name. Equal to "arg" if there is no
20672 * valid name.
20673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020675find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 char_u *arg;
20677 char_u **expr_start;
20678 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020679 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020681 int mb_nest = 0;
20682 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 char_u *p;
20684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020685 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020687 *expr_start = NULL;
20688 *expr_end = NULL;
20689 }
20690
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020691 /* Quick check for valid starting character. */
20692 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20693 return arg;
20694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020695 for (p = arg; *p != NUL
20696 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020697 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020698 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020699 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020700 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020701 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020702 if (*p == '\'')
20703 {
20704 /* skip over 'string' to avoid counting [ and ] inside it. */
20705 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20706 ;
20707 if (*p == NUL)
20708 break;
20709 }
20710 else if (*p == '"')
20711 {
20712 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20713 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20714 if (*p == '\\' && p[1] != NUL)
20715 ++p;
20716 if (*p == NUL)
20717 break;
20718 }
20719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020720 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 if (*p == '[')
20723 ++br_nest;
20724 else if (*p == ']')
20725 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 if (*p == '{')
20731 {
20732 mb_nest++;
20733 if (expr_start != NULL && *expr_start == NULL)
20734 *expr_start = p;
20735 }
20736 else if (*p == '}')
20737 {
20738 mb_nest--;
20739 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20740 *expr_end = p;
20741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 }
20744
20745 return p;
20746}
20747
20748/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020749 * Expands out the 'magic' {}'s in a variable/function name.
20750 * Note that this can call itself recursively, to deal with
20751 * constructs like foo{bar}{baz}{bam}
20752 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20753 * "in_start" ^
20754 * "expr_start" ^
20755 * "expr_end" ^
20756 * "in_end" ^
20757 *
20758 * Returns a new allocated string, which the caller must free.
20759 * Returns NULL for failure.
20760 */
20761 static char_u *
20762make_expanded_name(in_start, expr_start, expr_end, in_end)
20763 char_u *in_start;
20764 char_u *expr_start;
20765 char_u *expr_end;
20766 char_u *in_end;
20767{
20768 char_u c1;
20769 char_u *retval = NULL;
20770 char_u *temp_result;
20771 char_u *nextcmd = NULL;
20772
20773 if (expr_end == NULL || in_end == NULL)
20774 return NULL;
20775 *expr_start = NUL;
20776 *expr_end = NUL;
20777 c1 = *in_end;
20778 *in_end = NUL;
20779
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020780 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020781 if (temp_result != NULL && nextcmd == NULL)
20782 {
20783 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20784 + (in_end - expr_end) + 1));
20785 if (retval != NULL)
20786 {
20787 STRCPY(retval, in_start);
20788 STRCAT(retval, temp_result);
20789 STRCAT(retval, expr_end + 1);
20790 }
20791 }
20792 vim_free(temp_result);
20793
20794 *in_end = c1; /* put char back for error messages */
20795 *expr_start = '{';
20796 *expr_end = '}';
20797
20798 if (retval != NULL)
20799 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020800 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801 if (expr_start != NULL)
20802 {
20803 /* Further expansion! */
20804 temp_result = make_expanded_name(retval, expr_start,
20805 expr_end, temp_result);
20806 vim_free(retval);
20807 retval = temp_result;
20808 }
20809 }
20810
20811 return retval;
20812}
20813
20814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020816 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 */
20818 static int
20819eval_isnamec(c)
20820 int c;
20821{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020822 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20823}
20824
20825/*
20826 * Return TRUE if character "c" can be used as the first character in a
20827 * variable or function name (excluding '{' and '}').
20828 */
20829 static int
20830eval_isnamec1(c)
20831 int c;
20832{
20833 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834}
20835
20836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 * Set number v: variable to "val".
20838 */
20839 void
20840set_vim_var_nr(idx, val)
20841 int idx;
20842 long val;
20843{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020844 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845}
20846
20847/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020848 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 */
20850 long
20851get_vim_var_nr(idx)
20852 int idx;
20853{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020854 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855}
20856
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020857/*
20858 * Get string v: variable value. Uses a static buffer, can only be used once.
20859 */
20860 char_u *
20861get_vim_var_str(idx)
20862 int idx;
20863{
20864 return get_tv_string(&vimvars[idx].vv_tv);
20865}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020866
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020868 * Get List v: variable value. Caller must take care of reference count when
20869 * needed.
20870 */
20871 list_T *
20872get_vim_var_list(idx)
20873 int idx;
20874{
20875 return vimvars[idx].vv_list;
20876}
20877
20878/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020879 * Set v:char to character "c".
20880 */
20881 void
20882set_vim_var_char(c)
20883 int c;
20884{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020885 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020886
20887#ifdef FEAT_MBYTE
20888 if (has_mbyte)
20889 buf[(*mb_char2bytes)(c, buf)] = NUL;
20890 else
20891#endif
20892 {
20893 buf[0] = c;
20894 buf[1] = NUL;
20895 }
20896 set_vim_var_string(VV_CHAR, buf, -1);
20897}
20898
20899/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020900 * Set v:count to "count" and v:count1 to "count1".
20901 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 */
20903 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020904set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 long count;
20906 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020907 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020909 if (set_prevcount)
20910 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020911 vimvars[VV_COUNT].vv_nr = count;
20912 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913}
20914
20915/*
20916 * Set string v: variable to a copy of "val".
20917 */
20918 void
20919set_vim_var_string(idx, val, len)
20920 int idx;
20921 char_u *val;
20922 int len; /* length of "val" to use or -1 (whole string) */
20923{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020924 /* Need to do this (at least) once, since we can't initialize a union.
20925 * Will always be invoked when "v:progname" is set. */
20926 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20927
Bram Moolenaare9a41262005-01-15 22:18:47 +000020928 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020930 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020932 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020934 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935}
20936
20937/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020938 * Set List v: variable to "val".
20939 */
20940 void
20941set_vim_var_list(idx, val)
20942 int idx;
20943 list_T *val;
20944{
20945 list_unref(vimvars[idx].vv_list);
20946 vimvars[idx].vv_list = val;
20947 if (val != NULL)
20948 ++val->lv_refcount;
20949}
20950
20951/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020952 * Set Dictionary v: variable to "val".
20953 */
20954 void
20955set_vim_var_dict(idx, val)
20956 int idx;
20957 dict_T *val;
20958{
20959 int todo;
20960 hashitem_T *hi;
20961
20962 dict_unref(vimvars[idx].vv_dict);
20963 vimvars[idx].vv_dict = val;
20964 if (val != NULL)
20965 {
20966 ++val->dv_refcount;
20967
20968 /* Set readonly */
20969 todo = (int)val->dv_hashtab.ht_used;
20970 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20971 {
20972 if (HASHITEM_EMPTY(hi))
20973 continue;
20974 --todo;
20975 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20976 }
20977 }
20978}
20979
20980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 * Set v:register if needed.
20982 */
20983 void
20984set_reg_var(c)
20985 int c;
20986{
20987 char_u regname;
20988
20989 if (c == 0 || c == ' ')
20990 regname = '"';
20991 else
20992 regname = c;
20993 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020994 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 set_vim_var_string(VV_REG, &regname, 1);
20996}
20997
20998/*
20999 * Get or set v:exception. If "oldval" == NULL, return the current value.
21000 * Otherwise, restore the value to "oldval" and return NULL.
21001 * Must always be called in pairs to save and restore v:exception! Does not
21002 * take care of memory allocations.
21003 */
21004 char_u *
21005v_exception(oldval)
21006 char_u *oldval;
21007{
21008 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021009 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010
Bram Moolenaare9a41262005-01-15 22:18:47 +000021011 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 return NULL;
21013}
21014
21015/*
21016 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21017 * Otherwise, restore the value to "oldval" and return NULL.
21018 * Must always be called in pairs to save and restore v:throwpoint! Does not
21019 * take care of memory allocations.
21020 */
21021 char_u *
21022v_throwpoint(oldval)
21023 char_u *oldval;
21024{
21025 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021026 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027
Bram Moolenaare9a41262005-01-15 22:18:47 +000021028 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 return NULL;
21030}
21031
21032#if defined(FEAT_AUTOCMD) || defined(PROTO)
21033/*
21034 * Set v:cmdarg.
21035 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21036 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21037 * Must always be called in pairs!
21038 */
21039 char_u *
21040set_cmdarg(eap, oldarg)
21041 exarg_T *eap;
21042 char_u *oldarg;
21043{
21044 char_u *oldval;
21045 char_u *newval;
21046 unsigned len;
21047
Bram Moolenaare9a41262005-01-15 22:18:47 +000021048 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021049 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021051 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021052 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021053 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 }
21055
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021056 if (eap->force_bin == FORCE_BIN)
21057 len = 6;
21058 else if (eap->force_bin == FORCE_NOBIN)
21059 len = 8;
21060 else
21061 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021062
21063 if (eap->read_edit)
21064 len += 7;
21065
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021066 if (eap->force_ff != 0)
21067 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21068# ifdef FEAT_MBYTE
21069 if (eap->force_enc != 0)
21070 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021071 if (eap->bad_char != 0)
21072 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021073# endif
21074
21075 newval = alloc(len + 1);
21076 if (newval == NULL)
21077 return NULL;
21078
21079 if (eap->force_bin == FORCE_BIN)
21080 sprintf((char *)newval, " ++bin");
21081 else if (eap->force_bin == FORCE_NOBIN)
21082 sprintf((char *)newval, " ++nobin");
21083 else
21084 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021085
21086 if (eap->read_edit)
21087 STRCAT(newval, " ++edit");
21088
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021089 if (eap->force_ff != 0)
21090 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21091 eap->cmd + eap->force_ff);
21092# ifdef FEAT_MBYTE
21093 if (eap->force_enc != 0)
21094 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21095 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021096 if (eap->bad_char == BAD_KEEP)
21097 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21098 else if (eap->bad_char == BAD_DROP)
21099 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21100 else if (eap->bad_char != 0)
21101 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021102# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021103 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021104 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105}
21106#endif
21107
21108/*
21109 * Get the value of internal variable "name".
21110 * Return OK or FAIL.
21111 */
21112 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021113get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 char_u *name;
21115 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021117 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021118 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021119 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120{
21121 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 typval_T *tv = NULL;
21123 typval_T atv;
21124 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126
21127 /* truncate the name, so that we can use strcmp() */
21128 cc = name[len];
21129 name[len] = NUL;
21130
21131 /*
21132 * Check for "b:changedtick".
21133 */
21134 if (STRCMP(name, "b:changedtick") == 0)
21135 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021136 atv.v_type = VAR_NUMBER;
21137 atv.vval.v_number = curbuf->b_changedtick;
21138 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 }
21140
21141 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 * Check for user-defined variables.
21143 */
21144 else
21145 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021146 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021149 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021150 if (dip != NULL)
21151 *dip = v;
21152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 }
21154
Bram Moolenaare9a41262005-01-15 22:18:47 +000021155 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021157 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 ret = FAIL;
21160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021161 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021162 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163
21164 name[len] = cc;
21165
21166 return ret;
21167}
21168
21169/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021170 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21171 * Also handle function call with Funcref variable: func(expr)
21172 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21173 */
21174 static int
21175handle_subscript(arg, rettv, evaluate, verbose)
21176 char_u **arg;
21177 typval_T *rettv;
21178 int evaluate; /* do more than finding the end */
21179 int verbose; /* give error messages */
21180{
21181 int ret = OK;
21182 dict_T *selfdict = NULL;
21183 char_u *s;
21184 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021185 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021186
21187 while (ret == OK
21188 && (**arg == '['
21189 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021190 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021191 && !vim_iswhite(*(*arg - 1)))
21192 {
21193 if (**arg == '(')
21194 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021195 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021196 if (evaluate)
21197 {
21198 functv = *rettv;
21199 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021200
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021201 /* Invoke the function. Recursive! */
21202 s = functv.vval.v_string;
21203 }
21204 else
21205 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021206 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021207 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21208 &len, evaluate, selfdict);
21209
21210 /* Clear the funcref afterwards, so that deleting it while
21211 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021212 if (evaluate)
21213 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021214
21215 /* Stop the expression evaluation when immediately aborting on
21216 * error, or when an interrupt occurred or an exception was thrown
21217 * but not caught. */
21218 if (aborting())
21219 {
21220 if (ret == OK)
21221 clear_tv(rettv);
21222 ret = FAIL;
21223 }
21224 dict_unref(selfdict);
21225 selfdict = NULL;
21226 }
21227 else /* **arg == '[' || **arg == '.' */
21228 {
21229 dict_unref(selfdict);
21230 if (rettv->v_type == VAR_DICT)
21231 {
21232 selfdict = rettv->vval.v_dict;
21233 if (selfdict != NULL)
21234 ++selfdict->dv_refcount;
21235 }
21236 else
21237 selfdict = NULL;
21238 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21239 {
21240 clear_tv(rettv);
21241 ret = FAIL;
21242 }
21243 }
21244 }
21245 dict_unref(selfdict);
21246 return ret;
21247}
21248
21249/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021250 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021251 * value).
21252 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021253 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021255{
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021257}
21258
21259/*
21260 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 * The string "s" must have been allocated, it is consumed.
21262 * Return NULL for out of memory, the variable otherwise.
21263 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021265alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 char_u *s;
21267{
Bram Moolenaar33570922005-01-25 22:26:29 +000021268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021270 rettv = alloc_tv();
21271 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021273 rettv->v_type = VAR_STRING;
21274 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 }
21276 else
21277 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279}
21280
21281/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021282 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021284 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287{
21288 if (varp != NULL)
21289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021290 switch (varp->v_type)
21291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021293 func_unref(varp->vval.v_string);
21294 /*FALLTHROUGH*/
21295 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296 vim_free(varp->vval.v_string);
21297 break;
21298 case VAR_LIST:
21299 list_unref(varp->vval.v_list);
21300 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021301 case VAR_DICT:
21302 dict_unref(varp->vval.v_dict);
21303 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021304 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021305#ifdef FEAT_FLOAT
21306 case VAR_FLOAT:
21307#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021308 case VAR_UNKNOWN:
21309 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021310 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021311 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021312 break;
21313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 vim_free(varp);
21315 }
21316}
21317
21318/*
21319 * Free the memory for a variable value and set the value to NULL or 0.
21320 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021321 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021322clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324{
21325 if (varp != NULL)
21326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021327 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021329 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 func_unref(varp->vval.v_string);
21331 /*FALLTHROUGH*/
21332 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 vim_free(varp->vval.v_string);
21334 varp->vval.v_string = NULL;
21335 break;
21336 case VAR_LIST:
21337 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021338 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021340 case VAR_DICT:
21341 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021342 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021343 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021344 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021345 varp->vval.v_number = 0;
21346 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021347#ifdef FEAT_FLOAT
21348 case VAR_FLOAT:
21349 varp->vval.v_float = 0.0;
21350 break;
21351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021352 case VAR_UNKNOWN:
21353 break;
21354 default:
21355 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021357 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 }
21359}
21360
21361/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 * Set the value of a variable to NULL without freeing items.
21363 */
21364 static void
21365init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367{
21368 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021370}
21371
21372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 * Get the number value of a variable.
21374 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021375 * For incompatible types, return 0.
21376 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21377 * caller of incompatible types: it sets *denote to TRUE if "denote"
21378 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 */
21380 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021382 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021384 int error = FALSE;
21385
21386 return get_tv_number_chk(varp, &error); /* return 0L on error */
21387}
21388
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021389 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021390get_tv_number_chk(varp, denote)
21391 typval_T *varp;
21392 int *denote;
21393{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021396 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021398 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021399 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021400#ifdef FEAT_FLOAT
21401 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021402 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021403 break;
21404#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021406 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021407 break;
21408 case VAR_STRING:
21409 if (varp->vval.v_string != NULL)
21410 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021411 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021412 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021413 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021414 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021415 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021416 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021417 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021418 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021419 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021423 if (denote == NULL) /* useful for values that must be unsigned */
21424 n = -1;
21425 else
21426 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428}
21429
21430/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021431 * Get the lnum from the first argument.
21432 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021433 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 */
21435 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021437 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438{
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 linenr_T lnum;
21441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021442 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (lnum == 0) /* no valid number, try using line() */
21444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445 rettv.v_type = VAR_NUMBER;
21446 f_line(argvars, &rettv);
21447 lnum = rettv.vval.v_number;
21448 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
21450 return lnum;
21451}
21452
21453/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021454 * Get the lnum from the first argument.
21455 * Also accepts "$", then "buf" is used.
21456 * Returns 0 on error.
21457 */
21458 static linenr_T
21459get_tv_lnum_buf(argvars, buf)
21460 typval_T *argvars;
21461 buf_T *buf;
21462{
21463 if (argvars[0].v_type == VAR_STRING
21464 && argvars[0].vval.v_string != NULL
21465 && argvars[0].vval.v_string[0] == '$'
21466 && buf != NULL)
21467 return buf->b_ml.ml_line_count;
21468 return get_tv_number_chk(&argvars[0], NULL);
21469}
21470
21471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 * Get the string value of a variable.
21473 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021474 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21475 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 * If the String variable has never been set, return an empty string.
21477 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021478 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21479 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 */
21481 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021482get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484{
21485 static char_u mybuf[NUMBUFLEN];
21486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021487 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488}
21489
21490 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021491get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021495 char_u *res = get_tv_string_buf_chk(varp, buf);
21496
21497 return res != NULL ? res : (char_u *)"";
21498}
21499
Bram Moolenaar7d647822014-04-05 21:28:56 +020021500/*
21501 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21502 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021503 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021504get_tv_string_chk(varp)
21505 typval_T *varp;
21506{
21507 static char_u mybuf[NUMBUFLEN];
21508
21509 return get_tv_string_buf_chk(varp, mybuf);
21510}
21511
21512 static char_u *
21513get_tv_string_buf_chk(varp, buf)
21514 typval_T *varp;
21515 char_u *buf;
21516{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021517 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021519 case VAR_NUMBER:
21520 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21521 return buf;
21522 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021523 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021524 break;
21525 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021526 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021527 break;
21528 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021529 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021530 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021531#ifdef FEAT_FLOAT
21532 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021533 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021534 break;
21535#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021536 case VAR_STRING:
21537 if (varp->vval.v_string != NULL)
21538 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021539 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021540 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545}
21546
21547/*
21548 * Find variable "name" in the list of variables.
21549 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021550 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021555find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021558 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021561 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562
Bram Moolenaara7043832005-01-21 11:56:39 +000021563 ht = find_var_ht(name, &varname);
21564 if (htp != NULL)
21565 *htp = ht;
21566 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021568 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569}
21570
21571/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021572 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021573 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021576find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021578 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021579 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021580 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021581{
Bram Moolenaar33570922005-01-25 22:26:29 +000021582 hashitem_T *hi;
21583
21584 if (*varname == NUL)
21585 {
21586 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021587 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021589 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 case 'g': return &globvars_var;
21591 case 'v': return &vimvars_var;
21592 case 'b': return &curbuf->b_bufvar;
21593 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021594#ifdef FEAT_WINDOWS
21595 case 't': return &curtab->tp_winvar;
21596#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021597 case 'l': return current_funccal == NULL
21598 ? NULL : &current_funccal->l_vars_var;
21599 case 'a': return current_funccal == NULL
21600 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021601 }
21602 return NULL;
21603 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021604
21605 hi = hash_find(ht, varname);
21606 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 {
21608 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021609 * worked find the variable again. Don't auto-load a script if it was
21610 * loaded already, otherwise it would be loaded every time when
21611 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021612 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021613 {
21614 /* Note: script_autoload() may make "hi" invalid. It must either
21615 * be obtained again or not used. */
21616 if (!script_autoload(varname, FALSE) || aborting())
21617 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021618 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021619 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 if (HASHITEM_EMPTY(hi))
21621 return NULL;
21622 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021623 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021624}
21625
21626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021627 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021628 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021629 * Set "varname" to the start of name without ':'.
21630 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021632find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 char_u *name;
21634 char_u **varname;
21635{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021636 hashitem_T *hi;
21637
Bram Moolenaar73627d02015-08-11 15:46:09 +020021638 if (name[0] == NUL)
21639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 if (name[1] != ':')
21641 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021642 /* The name must not start with a colon or #. */
21643 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 return NULL;
21645 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021646
21647 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021648 hi = hash_find(&compat_hashtab, name);
21649 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021650 return &compat_hashtab;
21651
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 return &globvarht; /* global variable */
21654 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 }
21656 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021657 if (*name == 'g') /* global variable */
21658 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021659 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21660 */
21661 if (vim_strchr(name + 2, ':') != NULL
21662 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021663 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021665 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021667 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021668#ifdef FEAT_WINDOWS
21669 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021670 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021671#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 if (*name == 'v') /* v: variable */
21673 return &vimvarht;
21674 if (*name == 'a' && current_funccal != NULL) /* function argument */
21675 return &current_funccal->l_avars.dv_hashtab;
21676 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21677 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 if (*name == 's' /* script variable */
21679 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21680 return &SCRIPT_VARS(current_SID);
21681 return NULL;
21682}
21683
21684/*
21685 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021686 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 * Returns NULL when it doesn't exist.
21688 */
21689 char_u *
21690get_var_value(name)
21691 char_u *name;
21692{
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021695 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 if (v == NULL)
21697 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021698 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699}
21700
21701/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021702 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 * sourcing this script and when executing functions defined in the script.
21704 */
21705 void
21706new_script_vars(id)
21707 scid_T id;
21708{
Bram Moolenaara7043832005-01-21 11:56:39 +000021709 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021710 hashtab_T *ht;
21711 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021712
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021715 /* Re-allocating ga_data means that an ht_array pointing to
21716 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021718 for (i = 1; i <= ga_scripts.ga_len; ++i)
21719 {
21720 ht = &SCRIPT_VARS(i);
21721 if (ht->ht_mask == HT_INIT_SIZE - 1)
21722 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021723 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021725 }
21726
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 while (ga_scripts.ga_len < id)
21728 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021729 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021730 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021731 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 }
21734 }
21735}
21736
21737/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21739 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 */
21741 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021742init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 dict_T *dict;
21744 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021745 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746{
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021748 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021749 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021750 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021751 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 dict_var->di_tv.vval.v_dict = dict;
21753 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021754 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21756 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757}
21758
21759/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021760 * Unreference a dictionary initialized by init_var_dict().
21761 */
21762 void
21763unref_var_dict(dict)
21764 dict_T *dict;
21765{
21766 /* Now the dict needs to be freed if no one else is using it, go back to
21767 * normal reference counting. */
21768 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21769 dict_unref(dict);
21770}
21771
21772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 * Frees all allocated variables and the value they contain.
21775 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 */
21777 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021778vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 hashtab_T *ht;
21780{
21781 vars_clear_ext(ht, TRUE);
21782}
21783
21784/*
21785 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21786 */
21787 static void
21788vars_clear_ext(ht, free_val)
21789 hashtab_T *ht;
21790 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791{
Bram Moolenaara7043832005-01-21 11:56:39 +000021792 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021793 hashitem_T *hi;
21794 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795
Bram Moolenaar33570922005-01-25 22:26:29 +000021796 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021797 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021798 for (hi = ht->ht_array; todo > 0; ++hi)
21799 {
21800 if (!HASHITEM_EMPTY(hi))
21801 {
21802 --todo;
21803
Bram Moolenaar33570922005-01-25 22:26:29 +000021804 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021805 * ht_array might change then. hash_clear() takes care of it
21806 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 v = HI2DI(hi);
21808 if (free_val)
21809 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021810 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021811 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021812 }
21813 }
21814 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021815 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816}
21817
Bram Moolenaara7043832005-01-21 11:56:39 +000021818/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021819 * Delete a variable from hashtab "ht" at item "hi".
21820 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021823delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 hashtab_T *ht;
21825 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826{
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021828
21829 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021830 clear_tv(&di->di_tv);
21831 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832}
21833
21834/*
21835 * List the value of one internal variable.
21836 */
21837 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021838list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021841 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021843 char_u *tofree;
21844 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021845 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021846
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021847 current_copyID += COPYID_INC;
21848 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021850 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021851 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852}
21853
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021855list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 char_u *prefix;
21857 char_u *name;
21858 int type;
21859 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021860 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861{
Bram Moolenaar31859182007-08-14 20:41:13 +000021862 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21863 msg_start();
21864 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 if (name != NULL) /* "a:" vars don't have a name stored */
21866 msg_puts(name);
21867 msg_putchar(' ');
21868 msg_advance(22);
21869 if (type == VAR_NUMBER)
21870 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021871 else if (type == VAR_FUNC)
21872 msg_putchar('*');
21873 else if (type == VAR_LIST)
21874 {
21875 msg_putchar('[');
21876 if (*string == '[')
21877 ++string;
21878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021879 else if (type == VAR_DICT)
21880 {
21881 msg_putchar('{');
21882 if (*string == '{')
21883 ++string;
21884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 else
21886 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021889
21890 if (type == VAR_FUNC)
21891 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021892 if (*first)
21893 {
21894 msg_clr_eos();
21895 *first = FALSE;
21896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897}
21898
21899/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021900 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 * If the variable already exists, the value is updated.
21902 * Otherwise the variable is created.
21903 */
21904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021908 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909{
Bram Moolenaar33570922005-01-25 22:26:29 +000021910 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021914 ht = find_var_ht(name, &varname);
21915 if (ht == NULL || *varname == NUL)
21916 {
21917 EMSG2(_(e_illvar), name);
21918 return;
21919 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021920 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021921
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021922 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21923 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021924
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021928 if (var_check_ro(v->di_flags, name, FALSE)
21929 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 return;
21931 if (v->di_tv.v_type != tv->v_type
21932 && !((v->di_tv.v_type == VAR_STRING
21933 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021935 || tv->v_type == VAR_NUMBER))
21936#ifdef FEAT_FLOAT
21937 && !((v->di_tv.v_type == VAR_NUMBER
21938 || v->di_tv.v_type == VAR_FLOAT)
21939 && (tv->v_type == VAR_NUMBER
21940 || tv->v_type == VAR_FLOAT))
21941#endif
21942 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021944 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 return;
21946 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021947
21948 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021949 * Handle setting internal v: variables separately where needed to
21950 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 */
21952 if (ht == &vimvarht)
21953 {
21954 if (v->di_tv.v_type == VAR_STRING)
21955 {
21956 vim_free(v->di_tv.vval.v_string);
21957 if (copy || tv->v_type != VAR_STRING)
21958 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21959 else
21960 {
21961 /* Take over the string to avoid an extra alloc/free. */
21962 v->di_tv.vval.v_string = tv->vval.v_string;
21963 tv->vval.v_string = NULL;
21964 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021965 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021967 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021968 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021970 if (STRCMP(varname, "searchforward") == 0)
21971 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021972#ifdef FEAT_SEARCH_EXTRA
21973 else if (STRCMP(varname, "hlsearch") == 0)
21974 {
21975 no_hlsearch = !v->di_tv.vval.v_number;
21976 redraw_all_later(SOME_VALID);
21977 }
21978#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021979 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021980 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021981 else if (v->di_tv.v_type != tv->v_type)
21982 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 }
21984
21985 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 }
21987 else /* add a new variable */
21988 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021989 /* Can't add "v:" variable. */
21990 if (ht == &vimvarht)
21991 {
21992 EMSG2(_(e_illvar), name);
21993 return;
21994 }
21995
Bram Moolenaar92124a32005-06-17 22:03:40 +000021996 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021997 if (!valid_varname(varname))
21998 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021999
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022000 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22001 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022002 if (v == NULL)
22003 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022007 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 return;
22009 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022010 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022013 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022015 else
22016 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022018 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022019 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021}
22022
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022024 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 * Also give an error message.
22026 */
22027 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022028var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022029 int flags;
22030 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022031 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022032{
22033 if (flags & DI_FLAGS_RO)
22034 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022035 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022036 return TRUE;
22037 }
22038 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22039 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022040 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 return TRUE;
22042 }
22043 return FALSE;
22044}
22045
22046/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022047 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22048 * Also give an error message.
22049 */
22050 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022051var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022052 int flags;
22053 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022054 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022055{
22056 if (flags & DI_FLAGS_FIX)
22057 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022058 EMSG2(_("E795: Cannot delete variable %s"),
22059 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022060 return TRUE;
22061 }
22062 return FALSE;
22063}
22064
22065/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022066 * Check if a funcref is assigned to a valid variable name.
22067 * Return TRUE and give an error if not.
22068 */
22069 static int
22070var_check_func_name(name, new_var)
22071 char_u *name; /* points to start of variable name */
22072 int new_var; /* TRUE when creating the variable */
22073{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022074 /* Allow for w: b: s: and t:. */
22075 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022076 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22077 ? name[2] : name[0]))
22078 {
22079 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22080 name);
22081 return TRUE;
22082 }
22083 /* Don't allow hiding a function. When "v" is not NULL we might be
22084 * assigning another function to the same var, the type is checked
22085 * below. */
22086 if (new_var && function_exists(name))
22087 {
22088 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22089 name);
22090 return TRUE;
22091 }
22092 return FALSE;
22093}
22094
22095/*
22096 * Check if a variable name is valid.
22097 * Return FALSE and give an error if not.
22098 */
22099 static int
22100valid_varname(varname)
22101 char_u *varname;
22102{
22103 char_u *p;
22104
22105 for (p = varname; *p != NUL; ++p)
22106 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22107 && *p != AUTOLOAD_CHAR)
22108 {
22109 EMSG2(_(e_illvar), varname);
22110 return FALSE;
22111 }
22112 return TRUE;
22113}
22114
22115/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022116 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022117 * Also give an error message, using "name" or _("name") when use_gettext is
22118 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022119 */
22120 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022121tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022122 int lock;
22123 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022124 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022125{
22126 if (lock & VAR_LOCKED)
22127 {
22128 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022129 name == NULL ? (char_u *)_("Unknown")
22130 : use_gettext ? (char_u *)_(name)
22131 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022132 return TRUE;
22133 }
22134 if (lock & VAR_FIXED)
22135 {
22136 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022137 name == NULL ? (char_u *)_("Unknown")
22138 : use_gettext ? (char_u *)_(name)
22139 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022140 return TRUE;
22141 }
22142 return FALSE;
22143}
22144
22145/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022146 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022147 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022148 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022149 * It is OK for "from" and "to" to point to the same item. This is used to
22150 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022152 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 typval_T *from;
22155 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022158 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022159 switch (from->v_type)
22160 {
22161 case VAR_NUMBER:
22162 to->vval.v_number = from->vval.v_number;
22163 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022164#ifdef FEAT_FLOAT
22165 case VAR_FLOAT:
22166 to->vval.v_float = from->vval.v_float;
22167 break;
22168#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022169 case VAR_STRING:
22170 case VAR_FUNC:
22171 if (from->vval.v_string == NULL)
22172 to->vval.v_string = NULL;
22173 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022175 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022176 if (from->v_type == VAR_FUNC)
22177 func_ref(to->vval.v_string);
22178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022179 break;
22180 case VAR_LIST:
22181 if (from->vval.v_list == NULL)
22182 to->vval.v_list = NULL;
22183 else
22184 {
22185 to->vval.v_list = from->vval.v_list;
22186 ++to->vval.v_list->lv_refcount;
22187 }
22188 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022189 case VAR_DICT:
22190 if (from->vval.v_dict == NULL)
22191 to->vval.v_dict = NULL;
22192 else
22193 {
22194 to->vval.v_dict = from->vval.v_dict;
22195 ++to->vval.v_dict->dv_refcount;
22196 }
22197 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022198 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022199 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022200 break;
22201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202}
22203
22204/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022205 * Make a copy of an item.
22206 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022207 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22208 * reference to an already copied list/dict can be used.
22209 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022210 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022211 static int
22212item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022213 typval_T *from;
22214 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022215 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022216 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022217{
22218 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022219 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022220
Bram Moolenaar33570922005-01-25 22:26:29 +000022221 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022222 {
22223 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022224 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022225 }
22226 ++recurse;
22227
22228 switch (from->v_type)
22229 {
22230 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022231#ifdef FEAT_FLOAT
22232 case VAR_FLOAT:
22233#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022234 case VAR_STRING:
22235 case VAR_FUNC:
22236 copy_tv(from, to);
22237 break;
22238 case VAR_LIST:
22239 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022240 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022241 if (from->vval.v_list == NULL)
22242 to->vval.v_list = NULL;
22243 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22244 {
22245 /* use the copy made earlier */
22246 to->vval.v_list = from->vval.v_list->lv_copylist;
22247 ++to->vval.v_list->lv_refcount;
22248 }
22249 else
22250 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22251 if (to->vval.v_list == NULL)
22252 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022253 break;
22254 case VAR_DICT:
22255 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022256 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022257 if (from->vval.v_dict == NULL)
22258 to->vval.v_dict = NULL;
22259 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22260 {
22261 /* use the copy made earlier */
22262 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22263 ++to->vval.v_dict->dv_refcount;
22264 }
22265 else
22266 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22267 if (to->vval.v_dict == NULL)
22268 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022269 break;
22270 default:
22271 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022272 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022273 }
22274 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022275 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022276}
22277
22278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 * ":echo expr1 ..." print each argument separated with a space, add a
22280 * newline at the end.
22281 * ":echon expr1 ..." print each argument plain.
22282 */
22283 void
22284ex_echo(eap)
22285 exarg_T *eap;
22286{
22287 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 char_u *p;
22291 int needclr = TRUE;
22292 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022293 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294
22295 if (eap->skip)
22296 ++emsg_skip;
22297 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22298 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022299 /* If eval1() causes an error message the text from the command may
22300 * still need to be cleared. E.g., "echo 22,44". */
22301 need_clr_eos = needclr;
22302
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022304 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 {
22306 /*
22307 * Report the invalid expression unless the expression evaluation
22308 * has been cancelled due to an aborting error, an interrupt, or an
22309 * exception.
22310 */
22311 if (!aborting())
22312 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022313 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 break;
22315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022316 need_clr_eos = FALSE;
22317
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 if (!eap->skip)
22319 {
22320 if (atstart)
22321 {
22322 atstart = FALSE;
22323 /* Call msg_start() after eval1(), evaluating the expression
22324 * may cause a message to appear. */
22325 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022326 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022327 /* Mark the saved text as finishing the line, so that what
22328 * follows is displayed on a new line when scrolling back
22329 * at the more prompt. */
22330 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 }
22334 else if (eap->cmdidx == CMD_echo)
22335 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022336 current_copyID += COPYID_INC;
22337 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022338 if (p != NULL)
22339 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022341 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022343 if (*p != TAB && needclr)
22344 {
22345 /* remove any text still there from the command */
22346 msg_clr_eos();
22347 needclr = FALSE;
22348 }
22349 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 }
22351 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022352 {
22353#ifdef FEAT_MBYTE
22354 if (has_mbyte)
22355 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022356 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022357
22358 (void)msg_outtrans_len_attr(p, i, echo_attr);
22359 p += i - 1;
22360 }
22361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022363 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022368 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 arg = skipwhite(arg);
22370 }
22371 eap->nextcmd = check_nextcmd(arg);
22372
22373 if (eap->skip)
22374 --emsg_skip;
22375 else
22376 {
22377 /* remove text that may still be there from the command */
22378 if (needclr)
22379 msg_clr_eos();
22380 if (eap->cmdidx == CMD_echo)
22381 msg_end();
22382 }
22383}
22384
22385/*
22386 * ":echohl {name}".
22387 */
22388 void
22389ex_echohl(eap)
22390 exarg_T *eap;
22391{
22392 int id;
22393
22394 id = syn_name2id(eap->arg);
22395 if (id == 0)
22396 echo_attr = 0;
22397 else
22398 echo_attr = syn_id2attr(id);
22399}
22400
22401/*
22402 * ":execute expr1 ..." execute the result of an expression.
22403 * ":echomsg expr1 ..." Print a message
22404 * ":echoerr expr1 ..." Print an error
22405 * Each gets spaces around each argument and a newline at the end for
22406 * echo commands
22407 */
22408 void
22409ex_execute(eap)
22410 exarg_T *eap;
22411{
22412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 int ret = OK;
22415 char_u *p;
22416 garray_T ga;
22417 int len;
22418 int save_did_emsg;
22419
22420 ga_init2(&ga, 1, 80);
22421
22422 if (eap->skip)
22423 ++emsg_skip;
22424 while (*arg != NUL && *arg != '|' && *arg != '\n')
22425 {
22426 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022427 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 {
22429 /*
22430 * Report the invalid expression unless the expression evaluation
22431 * has been cancelled due to an aborting error, an interrupt, or an
22432 * exception.
22433 */
22434 if (!aborting())
22435 EMSG2(_(e_invexpr2), p);
22436 ret = FAIL;
22437 break;
22438 }
22439
22440 if (!eap->skip)
22441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022442 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 len = (int)STRLEN(p);
22444 if (ga_grow(&ga, len + 2) == FAIL)
22445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022446 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 ret = FAIL;
22448 break;
22449 }
22450 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 ga.ga_len += len;
22454 }
22455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 arg = skipwhite(arg);
22458 }
22459
22460 if (ret != FAIL && ga.ga_data != NULL)
22461 {
22462 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022465 out_flush();
22466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 else if (eap->cmdidx == CMD_echoerr)
22468 {
22469 /* We don't want to abort following commands, restore did_emsg. */
22470 save_did_emsg = did_emsg;
22471 EMSG((char_u *)ga.ga_data);
22472 if (!force_abort)
22473 did_emsg = save_did_emsg;
22474 }
22475 else if (eap->cmdidx == CMD_execute)
22476 do_cmdline((char_u *)ga.ga_data,
22477 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22478 }
22479
22480 ga_clear(&ga);
22481
22482 if (eap->skip)
22483 --emsg_skip;
22484
22485 eap->nextcmd = check_nextcmd(arg);
22486}
22487
22488/*
22489 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22490 * "arg" points to the "&" or '+' when called, to "option" when returning.
22491 * Returns NULL when no option name found. Otherwise pointer to the char
22492 * after the option name.
22493 */
22494 static char_u *
22495find_option_end(arg, opt_flags)
22496 char_u **arg;
22497 int *opt_flags;
22498{
22499 char_u *p = *arg;
22500
22501 ++p;
22502 if (*p == 'g' && p[1] == ':')
22503 {
22504 *opt_flags = OPT_GLOBAL;
22505 p += 2;
22506 }
22507 else if (*p == 'l' && p[1] == ':')
22508 {
22509 *opt_flags = OPT_LOCAL;
22510 p += 2;
22511 }
22512 else
22513 *opt_flags = 0;
22514
22515 if (!ASCII_ISALPHA(*p))
22516 return NULL;
22517 *arg = p;
22518
22519 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22520 p += 4; /* termcap option */
22521 else
22522 while (ASCII_ISALPHA(*p))
22523 ++p;
22524 return p;
22525}
22526
22527/*
22528 * ":function"
22529 */
22530 void
22531ex_function(eap)
22532 exarg_T *eap;
22533{
22534 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022535 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 int j;
22537 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022539 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 char_u *name = NULL;
22541 char_u *p;
22542 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022543 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 garray_T newargs;
22545 garray_T newlines;
22546 int varargs = FALSE;
22547 int mustend = FALSE;
22548 int flags = 0;
22549 ufunc_T *fp;
22550 int indent;
22551 int nesting;
22552 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 dictitem_T *v;
22554 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022555 static int func_nr = 0; /* number for nameless function */
22556 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022557 hashtab_T *ht;
22558 int todo;
22559 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022560 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561
22562 /*
22563 * ":function" without argument: list functions.
22564 */
22565 if (ends_excmd(*eap->arg))
22566 {
22567 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022568 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022569 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022570 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022571 {
22572 if (!HASHITEM_EMPTY(hi))
22573 {
22574 --todo;
22575 fp = HI2UF(hi);
22576 if (!isdigit(*fp->uf_name))
22577 list_func_head(fp, FALSE);
22578 }
22579 }
22580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 eap->nextcmd = check_nextcmd(eap->arg);
22582 return;
22583 }
22584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022585 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022586 * ":function /pat": list functions matching pattern.
22587 */
22588 if (*eap->arg == '/')
22589 {
22590 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22591 if (!eap->skip)
22592 {
22593 regmatch_T regmatch;
22594
22595 c = *p;
22596 *p = NUL;
22597 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22598 *p = c;
22599 if (regmatch.regprog != NULL)
22600 {
22601 regmatch.rm_ic = p_ic;
22602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022603 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022604 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22605 {
22606 if (!HASHITEM_EMPTY(hi))
22607 {
22608 --todo;
22609 fp = HI2UF(hi);
22610 if (!isdigit(*fp->uf_name)
22611 && vim_regexec(&regmatch, fp->uf_name, 0))
22612 list_func_head(fp, FALSE);
22613 }
22614 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022615 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022616 }
22617 }
22618 if (*p == '/')
22619 ++p;
22620 eap->nextcmd = check_nextcmd(p);
22621 return;
22622 }
22623
22624 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022625 * Get the function name. There are these situations:
22626 * func normal function name
22627 * "name" == func, "fudi.fd_dict" == NULL
22628 * dict.func new dictionary entry
22629 * "name" == NULL, "fudi.fd_dict" set,
22630 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22631 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022632 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022633 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22634 * dict.func existing dict entry that's not a Funcref
22635 * "name" == NULL, "fudi.fd_dict" set,
22636 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022637 * s:func script-local function name
22638 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022641 name = trans_function_name(&p, eap->skip, 0, &fudi);
22642 paren = (vim_strchr(p, '(') != NULL);
22643 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 {
22645 /*
22646 * Return on an invalid expression in braces, unless the expression
22647 * evaluation has been cancelled due to an aborting error, an
22648 * interrupt, or an exception.
22649 */
22650 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022651 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022652 if (!eap->skip && fudi.fd_newkey != NULL)
22653 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022654 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 else
22658 eap->skip = TRUE;
22659 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022660
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 /* An error in a function call during evaluation of an expression in magic
22662 * braces should not cause the function not to be defined. */
22663 saved_did_emsg = did_emsg;
22664 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665
22666 /*
22667 * ":function func" with only function name: list function.
22668 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022669 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 {
22671 if (!ends_excmd(*skipwhite(p)))
22672 {
22673 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 }
22676 eap->nextcmd = check_nextcmd(p);
22677 if (eap->nextcmd != NULL)
22678 *p = NUL;
22679 if (!eap->skip && !got_int)
22680 {
22681 fp = find_func(name);
22682 if (fp != NULL)
22683 {
22684 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022685 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022687 if (FUNCLINE(fp, j) == NULL)
22688 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 msg_putchar('\n');
22690 msg_outnum((long)(j + 1));
22691 if (j < 9)
22692 msg_putchar(' ');
22693 if (j < 99)
22694 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022695 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 out_flush(); /* show a line at a time */
22697 ui_breakcheck();
22698 }
22699 if (!got_int)
22700 {
22701 msg_putchar('\n');
22702 msg_puts((char_u *)" endfunction");
22703 }
22704 }
22705 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022706 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022708 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710
22711 /*
22712 * ":function name(arg1, arg2)" Define function.
22713 */
22714 p = skipwhite(p);
22715 if (*p != '(')
22716 {
22717 if (!eap->skip)
22718 {
22719 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022720 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 }
22722 /* attempt to continue by skipping some text */
22723 if (vim_strchr(p, '(') != NULL)
22724 p = vim_strchr(p, '(');
22725 }
22726 p = skipwhite(p + 1);
22727
22728 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22729 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22730
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022731 if (!eap->skip)
22732 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022733 /* Check the name of the function. Unless it's a dictionary function
22734 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022735 if (name != NULL)
22736 arg = name;
22737 else
22738 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022739 if (arg != NULL && (fudi.fd_di == NULL
22740 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022741 {
22742 if (*arg == K_SPECIAL)
22743 j = 3;
22744 else
22745 j = 0;
22746 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22747 : eval_isnamec(arg[j])))
22748 ++j;
22749 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022750 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022751 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022752 /* Disallow using the g: dict. */
22753 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22754 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022755 }
22756
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 /*
22758 * Isolate the arguments: "arg1, arg2, ...)"
22759 */
22760 while (*p != ')')
22761 {
22762 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22763 {
22764 varargs = TRUE;
22765 p += 3;
22766 mustend = TRUE;
22767 }
22768 else
22769 {
22770 arg = p;
22771 while (ASCII_ISALNUM(*p) || *p == '_')
22772 ++p;
22773 if (arg == p || isdigit(*arg)
22774 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22775 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22776 {
22777 if (!eap->skip)
22778 EMSG2(_("E125: Illegal argument: %s"), arg);
22779 break;
22780 }
22781 if (ga_grow(&newargs, 1) == FAIL)
22782 goto erret;
22783 c = *p;
22784 *p = NUL;
22785 arg = vim_strsave(arg);
22786 if (arg == NULL)
22787 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022788
22789 /* Check for duplicate argument name. */
22790 for (i = 0; i < newargs.ga_len; ++i)
22791 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22792 {
22793 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022794 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022795 goto erret;
22796 }
22797
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22799 *p = c;
22800 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 if (*p == ',')
22802 ++p;
22803 else
22804 mustend = TRUE;
22805 }
22806 p = skipwhite(p);
22807 if (mustend && *p != ')')
22808 {
22809 if (!eap->skip)
22810 EMSG2(_(e_invarg2), eap->arg);
22811 break;
22812 }
22813 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022814 if (*p != ')')
22815 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 ++p; /* skip the ')' */
22817
Bram Moolenaare9a41262005-01-15 22:18:47 +000022818 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 for (;;)
22820 {
22821 p = skipwhite(p);
22822 if (STRNCMP(p, "range", 5) == 0)
22823 {
22824 flags |= FC_RANGE;
22825 p += 5;
22826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022827 else if (STRNCMP(p, "dict", 4) == 0)
22828 {
22829 flags |= FC_DICT;
22830 p += 4;
22831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 else if (STRNCMP(p, "abort", 5) == 0)
22833 {
22834 flags |= FC_ABORT;
22835 p += 5;
22836 }
22837 else
22838 break;
22839 }
22840
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022841 /* When there is a line break use what follows for the function body.
22842 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22843 if (*p == '\n')
22844 line_arg = p + 1;
22845 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 EMSG(_(e_trailing));
22847
22848 /*
22849 * Read the body of the function, until ":endfunction" is found.
22850 */
22851 if (KeyTyped)
22852 {
22853 /* Check if the function already exists, don't let the user type the
22854 * whole function before telling him it doesn't work! For a script we
22855 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022856 if (!eap->skip && !eap->forceit)
22857 {
22858 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22859 EMSG(_(e_funcdict));
22860 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022861 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022864 if (!eap->skip && did_emsg)
22865 goto erret;
22866
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 msg_putchar('\n'); /* don't overwrite the function name */
22868 cmdline_row = msg_row;
22869 }
22870
22871 indent = 2;
22872 nesting = 0;
22873 for (;;)
22874 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022875 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022876 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022877 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022878 saved_wait_return = FALSE;
22879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022881 sourcing_lnum_off = sourcing_lnum;
22882
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022883 if (line_arg != NULL)
22884 {
22885 /* Use eap->arg, split up in parts by line breaks. */
22886 theline = line_arg;
22887 p = vim_strchr(theline, '\n');
22888 if (p == NULL)
22889 line_arg += STRLEN(line_arg);
22890 else
22891 {
22892 *p = NUL;
22893 line_arg = p + 1;
22894 }
22895 }
22896 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 theline = getcmdline(':', 0L, indent);
22898 else
22899 theline = eap->getline(':', eap->cookie, indent);
22900 if (KeyTyped)
22901 lines_left = Rows - 1;
22902 if (theline == NULL)
22903 {
22904 EMSG(_("E126: Missing :endfunction"));
22905 goto erret;
22906 }
22907
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022908 /* Detect line continuation: sourcing_lnum increased more than one. */
22909 if (sourcing_lnum > sourcing_lnum_off + 1)
22910 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22911 else
22912 sourcing_lnum_off = 0;
22913
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 if (skip_until != NULL)
22915 {
22916 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22917 * don't check for ":endfunc". */
22918 if (STRCMP(theline, skip_until) == 0)
22919 {
22920 vim_free(skip_until);
22921 skip_until = NULL;
22922 }
22923 }
22924 else
22925 {
22926 /* skip ':' and blanks*/
22927 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22928 ;
22929
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022930 /* Check for "endfunction". */
22931 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022933 if (line_arg == NULL)
22934 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 break;
22936 }
22937
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022938 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 * at "end". */
22940 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22941 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022942 else if (STRNCMP(p, "if", 2) == 0
22943 || STRNCMP(p, "wh", 2) == 0
22944 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 || STRNCMP(p, "try", 3) == 0)
22946 indent += 2;
22947
22948 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022949 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022951 if (*p == '!')
22952 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022954 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22955 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022957 ++nesting;
22958 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 }
22960 }
22961
22962 /* Check for ":append" or ":insert". */
22963 p = skip_range(p, NULL);
22964 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22965 || (p[0] == 'i'
22966 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22967 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22968 skip_until = vim_strsave((char_u *)".");
22969
22970 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22971 arg = skipwhite(skiptowhite(p));
22972 if (arg[0] == '<' && arg[1] =='<'
22973 && ((p[0] == 'p' && p[1] == 'y'
22974 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22975 || (p[0] == 'p' && p[1] == 'e'
22976 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22977 || (p[0] == 't' && p[1] == 'c'
22978 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022979 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22980 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22982 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022983 || (p[0] == 'm' && p[1] == 'z'
22984 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 ))
22986 {
22987 /* ":python <<" continues until a dot, like ":append" */
22988 p = skipwhite(arg + 2);
22989 if (*p == NUL)
22990 skip_until = vim_strsave((char_u *)".");
22991 else
22992 skip_until = vim_strsave(p);
22993 }
22994 }
22995
22996 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022997 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022998 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022999 if (line_arg == NULL)
23000 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023002 }
23003
23004 /* Copy the line to newly allocated memory. get_one_sourceline()
23005 * allocates 250 bytes per line, this saves 80% on average. The cost
23006 * is an extra alloc/free. */
23007 p = vim_strsave(theline);
23008 if (p != NULL)
23009 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023010 if (line_arg == NULL)
23011 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023012 theline = p;
23013 }
23014
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023015 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23016
23017 /* Add NULL lines for continuation lines, so that the line count is
23018 * equal to the index in the growarray. */
23019 while (sourcing_lnum_off-- > 0)
23020 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023021
23022 /* Check for end of eap->arg. */
23023 if (line_arg != NULL && *line_arg == NUL)
23024 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 }
23026
23027 /* Don't define the function when skipping commands or when an error was
23028 * detected. */
23029 if (eap->skip || did_emsg)
23030 goto erret;
23031
23032 /*
23033 * If there are no errors, add the function
23034 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023035 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023036 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023037 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023038 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023039 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023040 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023041 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023042 goto erret;
23043 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023044
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023045 fp = find_func(name);
23046 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023048 if (!eap->forceit)
23049 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023050 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023051 goto erret;
23052 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023053 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023054 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023055 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023056 name);
23057 goto erret;
23058 }
23059 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023060 ga_clear_strings(&(fp->uf_args));
23061 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023062 vim_free(name);
23063 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 }
23066 else
23067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068 char numbuf[20];
23069
23070 fp = NULL;
23071 if (fudi.fd_newkey == NULL && !eap->forceit)
23072 {
23073 EMSG(_(e_funcdict));
23074 goto erret;
23075 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023076 if (fudi.fd_di == NULL)
23077 {
23078 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023079 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023080 goto erret;
23081 }
23082 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023083 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023084 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023085
23086 /* Give the function a sequential number. Can only be used with a
23087 * Funcref! */
23088 vim_free(name);
23089 sprintf(numbuf, "%d", ++func_nr);
23090 name = vim_strsave((char_u *)numbuf);
23091 if (name == NULL)
23092 goto erret;
23093 }
23094
23095 if (fp == NULL)
23096 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023097 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023098 {
23099 int slen, plen;
23100 char_u *scriptname;
23101
23102 /* Check that the autoload name matches the script name. */
23103 j = FAIL;
23104 if (sourcing_name != NULL)
23105 {
23106 scriptname = autoload_name(name);
23107 if (scriptname != NULL)
23108 {
23109 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023110 plen = (int)STRLEN(p);
23111 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023112 if (slen > plen && fnamecmp(p,
23113 sourcing_name + slen - plen) == 0)
23114 j = OK;
23115 vim_free(scriptname);
23116 }
23117 }
23118 if (j == FAIL)
23119 {
23120 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23121 goto erret;
23122 }
23123 }
23124
23125 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 if (fp == NULL)
23127 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128
23129 if (fudi.fd_dict != NULL)
23130 {
23131 if (fudi.fd_di == NULL)
23132 {
23133 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023134 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023135 if (fudi.fd_di == NULL)
23136 {
23137 vim_free(fp);
23138 goto erret;
23139 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023140 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23141 {
23142 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023143 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023144 goto erret;
23145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023146 }
23147 else
23148 /* overwrite existing dict entry */
23149 clear_tv(&fudi.fd_di->di_tv);
23150 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023151 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023152 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023153 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023154
23155 /* behave like "dict" was used */
23156 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 }
23158
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023160 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023161 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23162 {
23163 vim_free(fp);
23164 goto erret;
23165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023167 fp->uf_args = newargs;
23168 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023169#ifdef FEAT_PROFILE
23170 fp->uf_tml_count = NULL;
23171 fp->uf_tml_total = NULL;
23172 fp->uf_tml_self = NULL;
23173 fp->uf_profiling = FALSE;
23174 if (prof_def_func())
23175 func_do_profile(fp);
23176#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023177 fp->uf_varargs = varargs;
23178 fp->uf_flags = flags;
23179 fp->uf_calls = 0;
23180 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182
23183erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 ga_clear_strings(&newargs);
23185 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186ret_free:
23187 vim_free(skip_until);
23188 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023191 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192}
23193
23194/*
23195 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023196 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023198 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023199 * TFN_INT: internal function name OK
23200 * TFN_QUIET: be quiet
23201 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 * Advances "pp" to just after the function name (if no error).
23203 */
23204 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023205trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 char_u **pp;
23207 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023208 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023209 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023211 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 char_u *start;
23213 char_u *end;
23214 int lead;
23215 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023217 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218
23219 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023220 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023222
23223 /* Check for hard coded <SNR>: already translated function ID (from a user
23224 * command). */
23225 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23226 && (*pp)[2] == (int)KE_SNR)
23227 {
23228 *pp += 3;
23229 len = get_id_len(pp) + 3;
23230 return vim_strnsave(start, len);
23231 }
23232
23233 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23234 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023236 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023238
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023239 /* Note that TFN_ flags use the same values as GLV_ flags. */
23240 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023241 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023242 if (end == start)
23243 {
23244 if (!skip)
23245 EMSG(_("E129: Function name required"));
23246 goto theend;
23247 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023248 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023249 {
23250 /*
23251 * Report an invalid expression in braces, unless the expression
23252 * evaluation has been cancelled due to an aborting error, an
23253 * interrupt, or an exception.
23254 */
23255 if (!aborting())
23256 {
23257 if (end != NULL)
23258 EMSG2(_(e_invarg2), start);
23259 }
23260 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023261 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023262 goto theend;
23263 }
23264
23265 if (lv.ll_tv != NULL)
23266 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023267 if (fdp != NULL)
23268 {
23269 fdp->fd_dict = lv.ll_dict;
23270 fdp->fd_newkey = lv.ll_newkey;
23271 lv.ll_newkey = NULL;
23272 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023273 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023274 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23275 {
23276 name = vim_strsave(lv.ll_tv->vval.v_string);
23277 *pp = end;
23278 }
23279 else
23280 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023281 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23282 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023283 EMSG(_(e_funcref));
23284 else
23285 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023286 name = NULL;
23287 }
23288 goto theend;
23289 }
23290
23291 if (lv.ll_name == NULL)
23292 {
23293 /* Error found, but continue after the function name. */
23294 *pp = end;
23295 goto theend;
23296 }
23297
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023298 /* Check if the name is a Funcref. If so, use the value. */
23299 if (lv.ll_exp_name != NULL)
23300 {
23301 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023302 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023303 if (name == lv.ll_exp_name)
23304 name = NULL;
23305 }
23306 else
23307 {
23308 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023309 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023310 if (name == *pp)
23311 name = NULL;
23312 }
23313 if (name != NULL)
23314 {
23315 name = vim_strsave(name);
23316 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023317 if (STRNCMP(name, "<SNR>", 5) == 0)
23318 {
23319 /* Change "<SNR>" to the byte sequence. */
23320 name[0] = K_SPECIAL;
23321 name[1] = KS_EXTRA;
23322 name[2] = (int)KE_SNR;
23323 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23324 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023325 goto theend;
23326 }
23327
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023328 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023329 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023330 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023331 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23332 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23333 {
23334 /* When there was "s:" already or the name expanded to get a
23335 * leading "s:" then remove it. */
23336 lv.ll_name += 2;
23337 len -= 2;
23338 lead = 2;
23339 }
23340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023341 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023342 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023343 /* skip over "s:" and "g:" */
23344 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023345 lv.ll_name += 2;
23346 len = (int)(end - lv.ll_name);
23347 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023348
23349 /*
23350 * Copy the function name to allocated memory.
23351 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23352 * Accept <SNR>123_name() outside a script.
23353 */
23354 if (skip)
23355 lead = 0; /* do nothing */
23356 else if (lead > 0)
23357 {
23358 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023359 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23360 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023361 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023362 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023363 if (current_SID <= 0)
23364 {
23365 EMSG(_(e_usingsid));
23366 goto theend;
23367 }
23368 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23369 lead += (int)STRLEN(sid_buf);
23370 }
23371 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023372 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023373 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023374 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023375 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023376 goto theend;
23377 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023378 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023379 {
23380 char_u *cp = vim_strchr(lv.ll_name, ':');
23381
23382 if (cp != NULL && cp < end)
23383 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023384 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023385 goto theend;
23386 }
23387 }
23388
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023389 name = alloc((unsigned)(len + lead + 1));
23390 if (name != NULL)
23391 {
23392 if (lead > 0)
23393 {
23394 name[0] = K_SPECIAL;
23395 name[1] = KS_EXTRA;
23396 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023397 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023398 STRCPY(name + 3, sid_buf);
23399 }
23400 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023401 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023402 }
23403 *pp = end;
23404
23405theend:
23406 clear_lval(&lv);
23407 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408}
23409
23410/*
23411 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23412 * Return 2 if "p" starts with "s:".
23413 * Return 0 otherwise.
23414 */
23415 static int
23416eval_fname_script(p)
23417 char_u *p;
23418{
23419 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23420 || STRNICMP(p + 1, "SNR>", 4) == 0))
23421 return 5;
23422 if (p[0] == 's' && p[1] == ':')
23423 return 2;
23424 return 0;
23425}
23426
23427/*
23428 * Return TRUE if "p" starts with "<SID>" or "s:".
23429 * Only works if eval_fname_script() returned non-zero for "p"!
23430 */
23431 static int
23432eval_fname_sid(p)
23433 char_u *p;
23434{
23435 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23436}
23437
23438/*
23439 * List the head of the function: "name(arg1, arg2)".
23440 */
23441 static void
23442list_func_head(fp, indent)
23443 ufunc_T *fp;
23444 int indent;
23445{
23446 int j;
23447
23448 msg_start();
23449 if (indent)
23450 MSG_PUTS(" ");
23451 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 {
23454 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023455 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 }
23457 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023458 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023460 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 {
23462 if (j)
23463 MSG_PUTS(", ");
23464 msg_puts(FUNCARG(fp, j));
23465 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023466 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 {
23468 if (j)
23469 MSG_PUTS(", ");
23470 MSG_PUTS("...");
23471 }
23472 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023473 if (fp->uf_flags & FC_ABORT)
23474 MSG_PUTS(" abort");
23475 if (fp->uf_flags & FC_RANGE)
23476 MSG_PUTS(" range");
23477 if (fp->uf_flags & FC_DICT)
23478 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023479 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023480 if (p_verbose > 0)
23481 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482}
23483
23484/*
23485 * Find a function by name, return pointer to it in ufuncs.
23486 * Return NULL for unknown function.
23487 */
23488 static ufunc_T *
23489find_func(name)
23490 char_u *name;
23491{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023492 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494 hi = hash_find(&func_hashtab, name);
23495 if (!HASHITEM_EMPTY(hi))
23496 return HI2UF(hi);
23497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498}
23499
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023500#if defined(EXITFREE) || defined(PROTO)
23501 void
23502free_all_functions()
23503{
23504 hashitem_T *hi;
23505
23506 /* Need to start all over every time, because func_free() may change the
23507 * hash table. */
23508 while (func_hashtab.ht_used > 0)
23509 for (hi = func_hashtab.ht_array; ; ++hi)
23510 if (!HASHITEM_EMPTY(hi))
23511 {
23512 func_free(HI2UF(hi));
23513 break;
23514 }
23515}
23516#endif
23517
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023518 int
23519translated_function_exists(name)
23520 char_u *name;
23521{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023522 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023523 return find_internal_func(name) >= 0;
23524 return find_func(name) != NULL;
23525}
23526
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023527/*
23528 * Return TRUE if a function "name" exists.
23529 */
23530 static int
23531function_exists(name)
23532 char_u *name;
23533{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023534 char_u *nm = name;
23535 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023536 int n = FALSE;
23537
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023538 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23539 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023540 nm = skipwhite(nm);
23541
23542 /* Only accept "funcname", "funcname ", "funcname (..." and
23543 * "funcname(...", not "funcname!...". */
23544 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023545 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023546 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023547 return n;
23548}
23549
Bram Moolenaara1544c02013-05-30 12:35:52 +020023550 char_u *
23551get_expanded_name(name, check)
23552 char_u *name;
23553 int check;
23554{
23555 char_u *nm = name;
23556 char_u *p;
23557
23558 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23559
23560 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023561 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023562 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023563
Bram Moolenaara1544c02013-05-30 12:35:52 +020023564 vim_free(p);
23565 return NULL;
23566}
23567
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023568/*
23569 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023570 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23571 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023572 */
23573 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023574builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023575 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023576 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023577{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023578 char_u *p;
23579
23580 if (!ASCII_ISLOWER(name[0]))
23581 return FALSE;
23582 p = vim_strchr(name, AUTOLOAD_CHAR);
23583 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023584}
23585
Bram Moolenaar05159a02005-02-26 23:04:13 +000023586#if defined(FEAT_PROFILE) || defined(PROTO)
23587/*
23588 * Start profiling function "fp".
23589 */
23590 static void
23591func_do_profile(fp)
23592 ufunc_T *fp;
23593{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023594 int len = fp->uf_lines.ga_len;
23595
23596 if (len == 0)
23597 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023598 fp->uf_tm_count = 0;
23599 profile_zero(&fp->uf_tm_self);
23600 profile_zero(&fp->uf_tm_total);
23601 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023602 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603 if (fp->uf_tml_total == NULL)
23604 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023605 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606 if (fp->uf_tml_self == NULL)
23607 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023608 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023609 fp->uf_tml_idx = -1;
23610 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23611 || fp->uf_tml_self == NULL)
23612 return; /* out of memory */
23613
23614 fp->uf_profiling = TRUE;
23615}
23616
23617/*
23618 * Dump the profiling results for all functions in file "fd".
23619 */
23620 void
23621func_dump_profile(fd)
23622 FILE *fd;
23623{
23624 hashitem_T *hi;
23625 int todo;
23626 ufunc_T *fp;
23627 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023628 ufunc_T **sorttab;
23629 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023631 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023632 if (todo == 0)
23633 return; /* nothing to dump */
23634
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023635 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023636
Bram Moolenaar05159a02005-02-26 23:04:13 +000023637 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23638 {
23639 if (!HASHITEM_EMPTY(hi))
23640 {
23641 --todo;
23642 fp = HI2UF(hi);
23643 if (fp->uf_profiling)
23644 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023645 if (sorttab != NULL)
23646 sorttab[st_len++] = fp;
23647
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648 if (fp->uf_name[0] == K_SPECIAL)
23649 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23650 else
23651 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23652 if (fp->uf_tm_count == 1)
23653 fprintf(fd, "Called 1 time\n");
23654 else
23655 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23656 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23657 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23658 fprintf(fd, "\n");
23659 fprintf(fd, "count total (s) self (s)\n");
23660
23661 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23662 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023663 if (FUNCLINE(fp, i) == NULL)
23664 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023665 prof_func_line(fd, fp->uf_tml_count[i],
23666 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023667 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23668 }
23669 fprintf(fd, "\n");
23670 }
23671 }
23672 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023673
23674 if (sorttab != NULL && st_len > 0)
23675 {
23676 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23677 prof_total_cmp);
23678 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23679 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23680 prof_self_cmp);
23681 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23682 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023683
23684 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023685}
Bram Moolenaar73830342005-02-28 22:48:19 +000023686
23687 static void
23688prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23689 FILE *fd;
23690 ufunc_T **sorttab;
23691 int st_len;
23692 char *title;
23693 int prefer_self; /* when equal print only self time */
23694{
23695 int i;
23696 ufunc_T *fp;
23697
23698 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23699 fprintf(fd, "count total (s) self (s) function\n");
23700 for (i = 0; i < 20 && i < st_len; ++i)
23701 {
23702 fp = sorttab[i];
23703 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23704 prefer_self);
23705 if (fp->uf_name[0] == K_SPECIAL)
23706 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23707 else
23708 fprintf(fd, " %s()\n", fp->uf_name);
23709 }
23710 fprintf(fd, "\n");
23711}
23712
23713/*
23714 * Print the count and times for one function or function line.
23715 */
23716 static void
23717prof_func_line(fd, count, total, self, prefer_self)
23718 FILE *fd;
23719 int count;
23720 proftime_T *total;
23721 proftime_T *self;
23722 int prefer_self; /* when equal print only self time */
23723{
23724 if (count > 0)
23725 {
23726 fprintf(fd, "%5d ", count);
23727 if (prefer_self && profile_equal(total, self))
23728 fprintf(fd, " ");
23729 else
23730 fprintf(fd, "%s ", profile_msg(total));
23731 if (!prefer_self && profile_equal(total, self))
23732 fprintf(fd, " ");
23733 else
23734 fprintf(fd, "%s ", profile_msg(self));
23735 }
23736 else
23737 fprintf(fd, " ");
23738}
23739
23740/*
23741 * Compare function for total time sorting.
23742 */
23743 static int
23744#ifdef __BORLANDC__
23745_RTLENTRYF
23746#endif
23747prof_total_cmp(s1, s2)
23748 const void *s1;
23749 const void *s2;
23750{
23751 ufunc_T *p1, *p2;
23752
23753 p1 = *(ufunc_T **)s1;
23754 p2 = *(ufunc_T **)s2;
23755 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23756}
23757
23758/*
23759 * Compare function for self time sorting.
23760 */
23761 static int
23762#ifdef __BORLANDC__
23763_RTLENTRYF
23764#endif
23765prof_self_cmp(s1, s2)
23766 const void *s1;
23767 const void *s2;
23768{
23769 ufunc_T *p1, *p2;
23770
23771 p1 = *(ufunc_T **)s1;
23772 p2 = *(ufunc_T **)s2;
23773 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23774}
23775
Bram Moolenaar05159a02005-02-26 23:04:13 +000023776#endif
23777
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023778/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023779 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023780 * Return TRUE if a package was loaded.
23781 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023782 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023783script_autoload(name, reload)
23784 char_u *name;
23785 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023786{
23787 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023788 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023789 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023790 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023791
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023792 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023793 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023794 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023795 return FALSE;
23796
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023797 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023798
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023799 /* Find the name in the list of previously loaded package names. Skip
23800 * "autoload/", it's always the same. */
23801 for (i = 0; i < ga_loaded.ga_len; ++i)
23802 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23803 break;
23804 if (!reload && i < ga_loaded.ga_len)
23805 ret = FALSE; /* was loaded already */
23806 else
23807 {
23808 /* Remember the name if it wasn't loaded already. */
23809 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23810 {
23811 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23812 tofree = NULL;
23813 }
23814
23815 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023816 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023817 ret = TRUE;
23818 }
23819
23820 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023821 return ret;
23822}
23823
23824/*
23825 * Return the autoload script name for a function or variable name.
23826 * Returns NULL when out of memory.
23827 */
23828 static char_u *
23829autoload_name(name)
23830 char_u *name;
23831{
23832 char_u *p;
23833 char_u *scriptname;
23834
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023835 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023836 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23837 if (scriptname == NULL)
23838 return FALSE;
23839 STRCPY(scriptname, "autoload/");
23840 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023841 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023842 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023843 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023844 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023845 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023846}
23847
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23849
23850/*
23851 * Function given to ExpandGeneric() to obtain the list of user defined
23852 * function names.
23853 */
23854 char_u *
23855get_user_func_name(xp, idx)
23856 expand_T *xp;
23857 int idx;
23858{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023859 static long_u done;
23860 static hashitem_T *hi;
23861 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862
23863 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023865 done = 0;
23866 hi = func_hashtab.ht_array;
23867 }
23868 if (done < func_hashtab.ht_used)
23869 {
23870 if (done++ > 0)
23871 ++hi;
23872 while (HASHITEM_EMPTY(hi))
23873 ++hi;
23874 fp = HI2UF(hi);
23875
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023876 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023877 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023878
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023879 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23880 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881
23882 cat_func_name(IObuff, fp);
23883 if (xp->xp_context != EXPAND_USER_FUNC)
23884 {
23885 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023886 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 STRCAT(IObuff, ")");
23888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 return IObuff;
23890 }
23891 return NULL;
23892}
23893
23894#endif /* FEAT_CMDL_COMPL */
23895
23896/*
23897 * Copy the function name of "fp" to buffer "buf".
23898 * "buf" must be able to hold the function name plus three bytes.
23899 * Takes care of script-local function names.
23900 */
23901 static void
23902cat_func_name(buf, fp)
23903 char_u *buf;
23904 ufunc_T *fp;
23905{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023906 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 {
23908 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023909 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 }
23911 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913}
23914
23915/*
23916 * ":delfunction {name}"
23917 */
23918 void
23919ex_delfunction(eap)
23920 exarg_T *eap;
23921{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 char_u *p;
23924 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926
23927 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023928 name = trans_function_name(&p, eap->skip, 0, &fudi);
23929 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023931 {
23932 if (fudi.fd_dict != NULL && !eap->skip)
23933 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 if (!ends_excmd(*skipwhite(p)))
23937 {
23938 vim_free(name);
23939 EMSG(_(e_trailing));
23940 return;
23941 }
23942 eap->nextcmd = check_nextcmd(p);
23943 if (eap->nextcmd != NULL)
23944 *p = NUL;
23945
23946 if (!eap->skip)
23947 fp = find_func(name);
23948 vim_free(name);
23949
23950 if (!eap->skip)
23951 {
23952 if (fp == NULL)
23953 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023954 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 return;
23956 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 {
23959 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23960 return;
23961 }
23962
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023963 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965 /* Delete the dict item that refers to the function, it will
23966 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023967 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023969 else
23970 func_free(fp);
23971 }
23972}
23973
23974/*
23975 * Free a function and remove it from the list of functions.
23976 */
23977 static void
23978func_free(fp)
23979 ufunc_T *fp;
23980{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023981 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023982
23983 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023984 ga_clear_strings(&(fp->uf_args));
23985 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023986#ifdef FEAT_PROFILE
23987 vim_free(fp->uf_tml_count);
23988 vim_free(fp->uf_tml_total);
23989 vim_free(fp->uf_tml_self);
23990#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023991
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023992 /* remove the function from the function hashtable */
23993 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23994 if (HASHITEM_EMPTY(hi))
23995 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023996 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023997 hash_remove(&func_hashtab, hi);
23998
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 vim_free(fp);
24000}
24001
24002/*
24003 * Unreference a Function: decrement the reference count and free it when it
24004 * becomes zero. Only for numbered functions.
24005 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024006 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024007func_unref(name)
24008 char_u *name;
24009{
24010 ufunc_T *fp;
24011
24012 if (name != NULL && isdigit(*name))
24013 {
24014 fp = find_func(name);
24015 if (fp == NULL)
24016 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024017 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024018 {
24019 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024020 * when "uf_calls" becomes zero. */
24021 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024022 func_free(fp);
24023 }
24024 }
24025}
24026
24027/*
24028 * Count a reference to a Function.
24029 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024030 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024031func_ref(name)
24032 char_u *name;
24033{
24034 ufunc_T *fp;
24035
24036 if (name != NULL && isdigit(*name))
24037 {
24038 fp = find_func(name);
24039 if (fp == NULL)
24040 EMSG2(_(e_intern2), "func_ref()");
24041 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024042 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 }
24044}
24045
24046/*
24047 * Call a user function.
24048 */
24049 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024050call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 ufunc_T *fp; /* pointer to function */
24052 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024053 typval_T *argvars; /* arguments */
24054 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 linenr_T firstline; /* first line of range */
24056 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024057 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058{
Bram Moolenaar33570922005-01-25 22:26:29 +000024059 char_u *save_sourcing_name;
24060 linenr_T save_sourcing_lnum;
24061 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024062 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024063 int save_did_emsg;
24064 static int depth = 0;
24065 dictitem_T *v;
24066 int fixvar_idx = 0; /* index in fixvar[] */
24067 int i;
24068 int ai;
24069 char_u numbuf[NUMBUFLEN];
24070 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024071 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024072#ifdef FEAT_PROFILE
24073 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024074 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076
24077 /* If depth of calling is getting too high, don't execute the function */
24078 if (depth >= p_mfd)
24079 {
24080 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024081 rettv->v_type = VAR_NUMBER;
24082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 return;
24084 }
24085 ++depth;
24086
24087 line_breakcheck(); /* check for CTRL-C hit */
24088
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024089 fc = (funccall_T *)alloc(sizeof(funccall_T));
24090 fc->caller = current_funccal;
24091 current_funccal = fc;
24092 fc->func = fp;
24093 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024094 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024095 fc->linenr = 0;
24096 fc->returned = FALSE;
24097 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024099 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24100 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101
Bram Moolenaar33570922005-01-25 22:26:29 +000024102 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024103 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024104 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24105 * each argument variable and saves a lot of time.
24106 */
24107 /*
24108 * Init l: variables.
24109 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024110 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024111 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024112 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024113 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24114 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024115 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024116 name = v->di_key;
24117 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024118 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024119 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024120 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024121 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024122 v->di_tv.vval.v_dict = selfdict;
24123 ++selfdict->dv_refcount;
24124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024125
Bram Moolenaar33570922005-01-25 22:26:29 +000024126 /*
24127 * Init a: variables.
24128 * Set a:0 to "argcount".
24129 * Set a:000 to a list with room for the "..." arguments.
24130 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024131 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024132 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024133 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024134 /* Use "name" to avoid a warning from some compiler that checks the
24135 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024136 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024137 name = v->di_key;
24138 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024139 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024140 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024141 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024142 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024143 v->di_tv.vval.v_list = &fc->l_varlist;
24144 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24145 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24146 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024147
24148 /*
24149 * Set a:firstline to "firstline" and a:lastline to "lastline".
24150 * Set a:name to named arguments.
24151 * Set a:N to the "..." arguments.
24152 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024153 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024154 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024155 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024156 (varnumber_T)lastline);
24157 for (i = 0; i < argcount; ++i)
24158 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024160 if (ai < 0)
24161 /* named argument a:name */
24162 name = FUNCARG(fp, i);
24163 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024165 /* "..." argument a:1, a:2, etc. */
24166 sprintf((char *)numbuf, "%d", ai + 1);
24167 name = numbuf;
24168 }
24169 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24170 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024171 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24173 }
24174 else
24175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024176 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24177 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 if (v == NULL)
24179 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024180 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024181 }
24182 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024183 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024184
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024185 /* Note: the values are copied directly to avoid alloc/free.
24186 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024187 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024188 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024189
24190 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24191 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024192 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24193 fc->l_listitems[ai].li_tv = argvars[i];
24194 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024195 }
24196 }
24197
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 /* Don't redraw while executing the function. */
24199 ++RedrawingDisabled;
24200 save_sourcing_name = sourcing_name;
24201 save_sourcing_lnum = sourcing_lnum;
24202 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024203 /* need space for function name + ("function " + 3) or "[number]" */
24204 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24205 + STRLEN(fp->uf_name) + 20;
24206 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 if (sourcing_name != NULL)
24208 {
24209 if (save_sourcing_name != NULL
24210 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024211 sprintf((char *)sourcing_name, "%s[%d]..",
24212 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 else
24214 STRCPY(sourcing_name, "function ");
24215 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24216
24217 if (p_verbose >= 12)
24218 {
24219 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024220 verbose_enter_scroll();
24221
Bram Moolenaar555b2802005-05-19 21:08:39 +000024222 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 if (p_verbose >= 14)
24224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024226 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024227 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024228 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229
24230 msg_puts((char_u *)"(");
24231 for (i = 0; i < argcount; ++i)
24232 {
24233 if (i > 0)
24234 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024235 if (argvars[i].v_type == VAR_NUMBER)
24236 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 else
24238 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024239 /* Do not want errors such as E724 here. */
24240 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024241 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024242 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024243 if (s != NULL)
24244 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024245 if (vim_strsize(s) > MSG_BUF_CLEN)
24246 {
24247 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24248 s = buf;
24249 }
24250 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024251 vim_free(tofree);
24252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 }
24254 }
24255 msg_puts((char_u *)")");
24256 }
24257 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024258
24259 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 --no_wait_return;
24261 }
24262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024263#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024264 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024265 {
24266 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24267 func_do_profile(fp);
24268 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024269 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024270 {
24271 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024272 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024273 profile_zero(&fp->uf_tm_children);
24274 }
24275 script_prof_save(&wait_start);
24276 }
24277#endif
24278
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024280 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 save_did_emsg = did_emsg;
24282 did_emsg = FALSE;
24283
24284 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024285 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24287
24288 --RedrawingDisabled;
24289
24290 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024291 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024293 clear_tv(rettv);
24294 rettv->v_type = VAR_NUMBER;
24295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 }
24297
Bram Moolenaar05159a02005-02-26 23:04:13 +000024298#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024299 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024300 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024301 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024302 profile_end(&call_start);
24303 profile_sub_wait(&wait_start, &call_start);
24304 profile_add(&fp->uf_tm_total, &call_start);
24305 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024306 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024308 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24309 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024310 }
24311 }
24312#endif
24313
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 /* when being verbose, mention the return value */
24315 if (p_verbose >= 12)
24316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024318 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024321 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024322 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024323 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024324 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024327 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024328 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024329 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024330 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024331
Bram Moolenaar555b2802005-05-19 21:08:39 +000024332 /* The value may be very long. Skip the middle part, so that we
24333 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024334 * truncate it at the end. Don't want errors such as E724 here. */
24335 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024336 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024337 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024338 if (s != NULL)
24339 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024340 if (vim_strsize(s) > MSG_BUF_CLEN)
24341 {
24342 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24343 s = buf;
24344 }
24345 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024346 vim_free(tofree);
24347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 }
24349 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024350
24351 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 --no_wait_return;
24353 }
24354
24355 vim_free(sourcing_name);
24356 sourcing_name = save_sourcing_name;
24357 sourcing_lnum = save_sourcing_lnum;
24358 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024360 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024361 script_prof_restore(&wait_start);
24362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363
24364 if (p_verbose >= 12 && sourcing_name != NULL)
24365 {
24366 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024367 verbose_enter_scroll();
24368
Bram Moolenaar555b2802005-05-19 21:08:39 +000024369 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024371
24372 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 --no_wait_return;
24374 }
24375
24376 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024377 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024379
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024380 /* If the a:000 list and the l: and a: dicts are not referenced we can
24381 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024382 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24383 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24384 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24385 {
24386 free_funccal(fc, FALSE);
24387 }
24388 else
24389 {
24390 hashitem_T *hi;
24391 listitem_T *li;
24392 int todo;
24393
24394 /* "fc" is still in use. This can happen when returning "a:000" or
24395 * assigning "l:" to a global variable.
24396 * Link "fc" in the list for garbage collection later. */
24397 fc->caller = previous_funccal;
24398 previous_funccal = fc;
24399
24400 /* Make a copy of the a: variables, since we didn't do that above. */
24401 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24402 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24403 {
24404 if (!HASHITEM_EMPTY(hi))
24405 {
24406 --todo;
24407 v = HI2DI(hi);
24408 copy_tv(&v->di_tv, &v->di_tv);
24409 }
24410 }
24411
24412 /* Make a copy of the a:000 items, since we didn't do that above. */
24413 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24414 copy_tv(&li->li_tv, &li->li_tv);
24415 }
24416}
24417
24418/*
24419 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024420 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024421 */
24422 static int
24423can_free_funccal(fc, copyID)
24424 funccall_T *fc;
24425 int copyID;
24426{
24427 return (fc->l_varlist.lv_copyID != copyID
24428 && fc->l_vars.dv_copyID != copyID
24429 && fc->l_avars.dv_copyID != copyID);
24430}
24431
24432/*
24433 * Free "fc" and what it contains.
24434 */
24435 static void
24436free_funccal(fc, free_val)
24437 funccall_T *fc;
24438 int free_val; /* a: vars were allocated */
24439{
24440 listitem_T *li;
24441
24442 /* The a: variables typevals may not have been allocated, only free the
24443 * allocated variables. */
24444 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24445
24446 /* free all l: variables */
24447 vars_clear(&fc->l_vars.dv_hashtab);
24448
24449 /* Free the a:000 variables if they were allocated. */
24450 if (free_val)
24451 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24452 clear_tv(&li->li_tv);
24453
24454 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455}
24456
24457/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024458 * Add a number variable "name" to dict "dp" with value "nr".
24459 */
24460 static void
24461add_nr_var(dp, v, name, nr)
24462 dict_T *dp;
24463 dictitem_T *v;
24464 char *name;
24465 varnumber_T nr;
24466{
24467 STRCPY(v->di_key, name);
24468 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24469 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24470 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024471 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024472 v->di_tv.vval.v_number = nr;
24473}
24474
24475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 * ":return [expr]"
24477 */
24478 void
24479ex_return(eap)
24480 exarg_T *eap;
24481{
24482 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 int returning = FALSE;
24485
24486 if (current_funccal == NULL)
24487 {
24488 EMSG(_("E133: :return not inside a function"));
24489 return;
24490 }
24491
24492 if (eap->skip)
24493 ++emsg_skip;
24494
24495 eap->nextcmd = NULL;
24496 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024497 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 {
24499 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024500 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 }
24504 /* It's safer to return also on error. */
24505 else if (!eap->skip)
24506 {
24507 /*
24508 * Return unless the expression evaluation has been cancelled due to an
24509 * aborting error, an interrupt, or an exception.
24510 */
24511 if (!aborting())
24512 returning = do_return(eap, FALSE, TRUE, NULL);
24513 }
24514
24515 /* When skipping or the return gets pending, advance to the next command
24516 * in this line (!returning). Otherwise, ignore the rest of the line.
24517 * Following lines will be ignored by get_func_line(). */
24518 if (returning)
24519 eap->nextcmd = NULL;
24520 else if (eap->nextcmd == NULL) /* no argument */
24521 eap->nextcmd = check_nextcmd(arg);
24522
24523 if (eap->skip)
24524 --emsg_skip;
24525}
24526
24527/*
24528 * Return from a function. Possibly makes the return pending. Also called
24529 * for a pending return at the ":endtry" or after returning from an extra
24530 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024532 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 * FALSE when the return gets pending.
24534 */
24535 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024536do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 exarg_T *eap;
24538 int reanimate;
24539 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024540 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541{
24542 int idx;
24543 struct condstack *cstack = eap->cstack;
24544
24545 if (reanimate)
24546 /* Undo the return. */
24547 current_funccal->returned = FALSE;
24548
24549 /*
24550 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24551 * not in its finally clause (which then is to be executed next) is found.
24552 * In this case, make the ":return" pending for execution at the ":endtry".
24553 * Otherwise, return normally.
24554 */
24555 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24556 if (idx >= 0)
24557 {
24558 cstack->cs_pending[idx] = CSTP_RETURN;
24559
24560 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024561 /* A pending return again gets pending. "rettv" points to an
24562 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024564 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 else
24566 {
24567 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024568 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024570 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 {
24574 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024575 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024576 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577 else
24578 EMSG(_(e_outofmem));
24579 }
24580 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024581 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582
24583 if (reanimate)
24584 {
24585 /* The pending return value could be overwritten by a ":return"
24586 * without argument in a finally clause; reset the default
24587 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024588 current_funccal->rettv->v_type = VAR_NUMBER;
24589 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024590 }
24591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024592 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593 }
24594 else
24595 {
24596 current_funccal->returned = TRUE;
24597
24598 /* If the return is carried out now, store the return value. For
24599 * a return immediately after reanimation, the value is already
24600 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024601 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024603 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024604 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024606 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 }
24608 }
24609
24610 return idx < 0;
24611}
24612
24613/*
24614 * Free the variable with a pending return value.
24615 */
24616 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024617discard_pending_return(rettv)
24618 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619{
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621}
24622
24623/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024624 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 * is an allocated string. Used by report_pending() for verbose messages.
24626 */
24627 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024628get_return_cmd(rettv)
24629 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024631 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024633 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024635 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024636 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024637 if (s == NULL)
24638 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024639
24640 STRCPY(IObuff, ":return ");
24641 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24642 if (STRLEN(s) + 8 >= IOSIZE)
24643 STRCPY(IObuff + IOSIZE - 4, "...");
24644 vim_free(tofree);
24645 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646}
24647
24648/*
24649 * Get next function line.
24650 * Called by do_cmdline() to get the next line.
24651 * Returns allocated string, or NULL for end of function.
24652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 char_u *
24654get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024655 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024657 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658{
Bram Moolenaar33570922005-01-25 22:26:29 +000024659 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024660 ufunc_T *fp = fcp->func;
24661 char_u *retval;
24662 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663
24664 /* If breakpoints have been added/deleted need to check for it. */
24665 if (fcp->dbg_tick != debug_tick)
24666 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024667 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 sourcing_lnum);
24669 fcp->dbg_tick = debug_tick;
24670 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024671#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024672 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024673 func_line_end(cookie);
24674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675
Bram Moolenaar05159a02005-02-26 23:04:13 +000024676 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024677 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24678 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 retval = NULL;
24680 else
24681 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024682 /* Skip NULL lines (continuation lines). */
24683 while (fcp->linenr < gap->ga_len
24684 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24685 ++fcp->linenr;
24686 if (fcp->linenr >= gap->ga_len)
24687 retval = NULL;
24688 else
24689 {
24690 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24691 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024693 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024694 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 }
24698
24699 /* Did we encounter a breakpoint? */
24700 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24701 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024704 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 sourcing_lnum);
24706 fcp->dbg_tick = debug_tick;
24707 }
24708
24709 return retval;
24710}
24711
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712#if defined(FEAT_PROFILE) || defined(PROTO)
24713/*
24714 * Called when starting to read a function line.
24715 * "sourcing_lnum" must be correct!
24716 * When skipping lines it may not actually be executed, but we won't find out
24717 * until later and we need to store the time now.
24718 */
24719 void
24720func_line_start(cookie)
24721 void *cookie;
24722{
24723 funccall_T *fcp = (funccall_T *)cookie;
24724 ufunc_T *fp = fcp->func;
24725
24726 if (fp->uf_profiling && sourcing_lnum >= 1
24727 && sourcing_lnum <= fp->uf_lines.ga_len)
24728 {
24729 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024730 /* Skip continuation lines. */
24731 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24732 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024733 fp->uf_tml_execed = FALSE;
24734 profile_start(&fp->uf_tml_start);
24735 profile_zero(&fp->uf_tml_children);
24736 profile_get_wait(&fp->uf_tml_wait);
24737 }
24738}
24739
24740/*
24741 * Called when actually executing a function line.
24742 */
24743 void
24744func_line_exec(cookie)
24745 void *cookie;
24746{
24747 funccall_T *fcp = (funccall_T *)cookie;
24748 ufunc_T *fp = fcp->func;
24749
24750 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24751 fp->uf_tml_execed = TRUE;
24752}
24753
24754/*
24755 * Called when done with a function line.
24756 */
24757 void
24758func_line_end(cookie)
24759 void *cookie;
24760{
24761 funccall_T *fcp = (funccall_T *)cookie;
24762 ufunc_T *fp = fcp->func;
24763
24764 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24765 {
24766 if (fp->uf_tml_execed)
24767 {
24768 ++fp->uf_tml_count[fp->uf_tml_idx];
24769 profile_end(&fp->uf_tml_start);
24770 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024771 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024772 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24773 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774 }
24775 fp->uf_tml_idx = -1;
24776 }
24777}
24778#endif
24779
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780/*
24781 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024782 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 */
24784 int
24785func_has_ended(cookie)
24786 void *cookie;
24787{
Bram Moolenaar33570922005-01-25 22:26:29 +000024788 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789
24790 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24791 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024792 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 || fcp->returned);
24794}
24795
24796/*
24797 * return TRUE if cookie indicates a function which "abort"s on errors.
24798 */
24799 int
24800func_has_abort(cookie)
24801 void *cookie;
24802{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024803 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804}
24805
24806#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24807typedef enum
24808{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024809 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24810 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24811 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812} var_flavour_T;
24813
24814static var_flavour_T var_flavour __ARGS((char_u *varname));
24815
24816 static var_flavour_T
24817var_flavour(varname)
24818 char_u *varname;
24819{
24820 char_u *p = varname;
24821
24822 if (ASCII_ISUPPER(*p))
24823 {
24824 while (*(++p))
24825 if (ASCII_ISLOWER(*p))
24826 return VAR_FLAVOUR_SESSION;
24827 return VAR_FLAVOUR_VIMINFO;
24828 }
24829 else
24830 return VAR_FLAVOUR_DEFAULT;
24831}
24832#endif
24833
24834#if defined(FEAT_VIMINFO) || defined(PROTO)
24835/*
24836 * Restore global vars that start with a capital from the viminfo file
24837 */
24838 int
24839read_viminfo_varlist(virp, writing)
24840 vir_T *virp;
24841 int writing;
24842{
24843 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024844 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846
24847 if (!writing && (find_viminfo_parameter('!') != NULL))
24848 {
24849 tab = vim_strchr(virp->vir_line + 1, '\t');
24850 if (tab != NULL)
24851 {
24852 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024853 switch (*tab)
24854 {
24855 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024856#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024857 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024858#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024859 case 'D': type = VAR_DICT; break;
24860 case 'L': type = VAR_LIST; break;
24861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862
24863 tab = vim_strchr(tab, '\t');
24864 if (tab != NULL)
24865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024866 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024867 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024868 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024870#ifdef FEAT_FLOAT
24871 else if (type == VAR_FLOAT)
24872 (void)string2float(tab + 1, &tv.vval.v_float);
24873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024875 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024876 if (type == VAR_DICT || type == VAR_LIST)
24877 {
24878 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24879
24880 if (etv == NULL)
24881 /* Failed to parse back the dict or list, use it as a
24882 * string. */
24883 tv.v_type = VAR_STRING;
24884 else
24885 {
24886 vim_free(tv.vval.v_string);
24887 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024888 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024889 }
24890 }
24891
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024892 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024893
24894 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024895 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024896 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 }
24899 }
24900 }
24901
24902 return viminfo_readline(virp);
24903}
24904
24905/*
24906 * Write global vars that start with a capital to the viminfo file
24907 */
24908 void
24909write_viminfo_varlist(fp)
24910 FILE *fp;
24911{
Bram Moolenaar33570922005-01-25 22:26:29 +000024912 hashitem_T *hi;
24913 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024914 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024915 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024916 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024917 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024918 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919
24920 if (find_viminfo_parameter('!') == NULL)
24921 return;
24922
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024923 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024925 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024926 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024928 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024930 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024931 this_var = HI2DI(hi);
24932 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024933 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024935 {
24936 case VAR_STRING: s = "STR"; break;
24937 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024938#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024939 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024940#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024941 case VAR_DICT: s = "DIC"; break;
24942 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024943 default: continue;
24944 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024945 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024946 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024947 if (p != NULL)
24948 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024949 vim_free(tofree);
24950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 }
24952 }
24953}
24954#endif
24955
24956#if defined(FEAT_SESSION) || defined(PROTO)
24957 int
24958store_session_globals(fd)
24959 FILE *fd;
24960{
Bram Moolenaar33570922005-01-25 22:26:29 +000024961 hashitem_T *hi;
24962 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024963 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 char_u *p, *t;
24965
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024966 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024967 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024969 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024971 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 this_var = HI2DI(hi);
24973 if ((this_var->di_tv.v_type == VAR_NUMBER
24974 || this_var->di_tv.v_type == VAR_STRING)
24975 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024976 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024977 /* Escape special characters with a backslash. Turn a LF and
24978 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024979 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024980 (char_u *)"\\\"\n\r");
24981 if (p == NULL) /* out of memory */
24982 break;
24983 for (t = p; *t != NUL; ++t)
24984 if (*t == '\n')
24985 *t = 'n';
24986 else if (*t == '\r')
24987 *t = 'r';
24988 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024989 this_var->di_key,
24990 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24991 : ' ',
24992 p,
24993 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24994 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024995 || put_eol(fd) == FAIL)
24996 {
24997 vim_free(p);
24998 return FAIL;
24999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 vim_free(p);
25001 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025002#ifdef FEAT_FLOAT
25003 else if (this_var->di_tv.v_type == VAR_FLOAT
25004 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25005 {
25006 float_T f = this_var->di_tv.vval.v_float;
25007 int sign = ' ';
25008
25009 if (f < 0)
25010 {
25011 f = -f;
25012 sign = '-';
25013 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025014 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025015 this_var->di_key, sign, f) < 0)
25016 || put_eol(fd) == FAIL)
25017 return FAIL;
25018 }
25019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 }
25021 }
25022 return OK;
25023}
25024#endif
25025
Bram Moolenaar661b1822005-07-28 22:36:45 +000025026/*
25027 * Display script name where an item was last set.
25028 * Should only be invoked when 'verbose' is non-zero.
25029 */
25030 void
25031last_set_msg(scriptID)
25032 scid_T scriptID;
25033{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025034 char_u *p;
25035
Bram Moolenaar661b1822005-07-28 22:36:45 +000025036 if (scriptID != 0)
25037 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025038 p = home_replace_save(NULL, get_scriptname(scriptID));
25039 if (p != NULL)
25040 {
25041 verbose_enter();
25042 MSG_PUTS(_("\n\tLast set from "));
25043 MSG_PUTS(p);
25044 vim_free(p);
25045 verbose_leave();
25046 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025047 }
25048}
25049
Bram Moolenaard812df62008-11-09 12:46:09 +000025050/*
25051 * List v:oldfiles in a nice way.
25052 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025053 void
25054ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025055 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025056{
25057 list_T *l = vimvars[VV_OLDFILES].vv_list;
25058 listitem_T *li;
25059 int nr = 0;
25060
25061 if (l == NULL)
25062 msg((char_u *)_("No old files"));
25063 else
25064 {
25065 msg_start();
25066 msg_scroll = TRUE;
25067 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25068 {
25069 msg_outnum((long)++nr);
25070 MSG_PUTS(": ");
25071 msg_outtrans(get_tv_string(&li->li_tv));
25072 msg_putchar('\n');
25073 out_flush(); /* output one line at a time */
25074 ui_breakcheck();
25075 }
25076 /* Assume "got_int" was set to truncate the listing. */
25077 got_int = FALSE;
25078
25079#ifdef FEAT_BROWSE_CMD
25080 if (cmdmod.browse)
25081 {
25082 quit_more = FALSE;
25083 nr = prompt_for_number(FALSE);
25084 msg_starthere();
25085 if (nr > 0)
25086 {
25087 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25088 (long)nr);
25089
25090 if (p != NULL)
25091 {
25092 p = expand_env_save(p);
25093 eap->arg = p;
25094 eap->cmdidx = CMD_edit;
25095 cmdmod.browse = FALSE;
25096 do_exedit(eap, NULL);
25097 vim_free(p);
25098 }
25099 }
25100 }
25101#endif
25102 }
25103}
25104
Bram Moolenaar53744302015-07-17 17:38:22 +020025105/* reset v:option_new, v:option_old and v:option_type */
25106 void
25107reset_v_option_vars()
25108{
25109 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25110 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25111 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25112}
25113
25114
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115#endif /* FEAT_EVAL */
25116
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025118#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119
25120#ifdef WIN3264
25121/*
25122 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25123 */
25124static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25125static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25126static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25127
25128/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025129 * Get the short path (8.3) for the filename in "fnamep".
25130 * Only works for a valid file name.
25131 * When the path gets longer "fnamep" is changed and the allocated buffer
25132 * is put in "bufp".
25133 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25134 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 */
25136 static int
25137get_short_pathname(fnamep, bufp, fnamelen)
25138 char_u **fnamep;
25139 char_u **bufp;
25140 int *fnamelen;
25141{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025142 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 char_u *newbuf;
25144
25145 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 l = GetShortPathName(*fnamep, *fnamep, len);
25147 if (l > len - 1)
25148 {
25149 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025150 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 newbuf = vim_strnsave(*fnamep, l);
25152 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025154
25155 vim_free(*bufp);
25156 *fnamep = *bufp = newbuf;
25157
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025158 /* Really should always succeed, as the buffer is big enough. */
25159 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 }
25161
25162 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025163 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164}
25165
25166/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025167 * Get the short path (8.3) for the filename in "fname". The converted
25168 * path is returned in "bufp".
25169 *
25170 * Some of the directories specified in "fname" may not exist. This function
25171 * will shorten the existing directories at the beginning of the path and then
25172 * append the remaining non-existing path.
25173 *
25174 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025175 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025176 * bufp - Pointer to an allocated buffer for the filename.
25177 * fnamelen - Length of the filename pointed to by fname
25178 *
25179 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 */
25181 static int
25182shortpath_for_invalid_fname(fname, bufp, fnamelen)
25183 char_u **fname;
25184 char_u **bufp;
25185 int *fnamelen;
25186{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025187 char_u *short_fname, *save_fname, *pbuf_unused;
25188 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025190 int old_len, len;
25191 int new_len, sfx_len;
25192 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193
25194 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025195 old_len = *fnamelen;
25196 save_fname = vim_strnsave(*fname, old_len);
25197 pbuf_unused = NULL;
25198 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025200 endp = save_fname + old_len - 1; /* Find the end of the copy */
25201 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025203 /*
25204 * Try shortening the supplied path till it succeeds by removing one
25205 * directory at a time from the tail of the path.
25206 */
25207 len = 0;
25208 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025210 /* go back one path-separator */
25211 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25212 --endp;
25213 if (endp <= save_fname)
25214 break; /* processed the complete path */
25215
25216 /*
25217 * Replace the path separator with a NUL and try to shorten the
25218 * resulting path.
25219 */
25220 ch = *endp;
25221 *endp = 0;
25222 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025223 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025224 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25225 {
25226 retval = FAIL;
25227 goto theend;
25228 }
25229 *endp = ch; /* preserve the string */
25230
25231 if (len > 0)
25232 break; /* successfully shortened the path */
25233
25234 /* failed to shorten the path. Skip the path separator */
25235 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 }
25237
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025238 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 /*
25241 * Succeeded in shortening the path. Now concatenate the shortened
25242 * path with the remaining path at the tail.
25243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025245 /* Compute the length of the new path. */
25246 sfx_len = (int)(save_endp - endp) + 1;
25247 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025249 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025251 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025253 /* There is not enough space in the currently allocated string,
25254 * copy it to a buffer big enough. */
25255 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025257 {
25258 retval = FAIL;
25259 goto theend;
25260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 }
25262 else
25263 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025264 /* Transfer short_fname to the main buffer (it's big enough),
25265 * unless get_short_pathname() did its work in-place. */
25266 *fname = *bufp = save_fname;
25267 if (short_fname != save_fname)
25268 vim_strncpy(save_fname, short_fname, len);
25269 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025271
25272 /* concat the not-shortened part of the path */
25273 vim_strncpy(*fname + len, endp, sfx_len);
25274 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025276
25277theend:
25278 vim_free(pbuf_unused);
25279 vim_free(save_fname);
25280
25281 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282}
25283
25284/*
25285 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025286 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 */
25288 static int
25289shortpath_for_partial(fnamep, bufp, fnamelen)
25290 char_u **fnamep;
25291 char_u **bufp;
25292 int *fnamelen;
25293{
25294 int sepcount, len, tflen;
25295 char_u *p;
25296 char_u *pbuf, *tfname;
25297 int hasTilde;
25298
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025299 /* Count up the path separators from the RHS.. so we know which part
25300 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025302 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 if (vim_ispathsep(*p))
25304 ++sepcount;
25305
25306 /* Need full path first (use expand_env() to remove a "~/") */
25307 hasTilde = (**fnamep == '~');
25308 if (hasTilde)
25309 pbuf = tfname = expand_env_save(*fnamep);
25310 else
25311 pbuf = tfname = FullName_save(*fnamep, FALSE);
25312
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025313 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025315 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317
25318 if (len == 0)
25319 {
25320 /* Don't have a valid filename, so shorten the rest of the
25321 * path if we can. This CAN give us invalid 8.3 filenames, but
25322 * there's not a lot of point in guessing what it might be.
25323 */
25324 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025325 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 }
25328
25329 /* Count the paths backward to find the beginning of the desired string. */
25330 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025331 {
25332#ifdef FEAT_MBYTE
25333 if (has_mbyte)
25334 p -= mb_head_off(tfname, p);
25335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 if (vim_ispathsep(*p))
25337 {
25338 if (sepcount == 0 || (hasTilde && sepcount == 1))
25339 break;
25340 else
25341 sepcount --;
25342 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 if (hasTilde)
25345 {
25346 --p;
25347 if (p >= tfname)
25348 *p = '~';
25349 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351 }
25352 else
25353 ++p;
25354
25355 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25356 vim_free(*bufp);
25357 *fnamelen = (int)STRLEN(p);
25358 *bufp = pbuf;
25359 *fnamep = p;
25360
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362}
25363#endif /* WIN3264 */
25364
25365/*
25366 * Adjust a filename, according to a string of modifiers.
25367 * *fnamep must be NUL terminated when called. When returning, the length is
25368 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025369 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 * When there is an error, *fnamep is set to NULL.
25371 */
25372 int
25373modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25374 char_u *src; /* string with modifiers */
25375 int *usedlen; /* characters after src that are used */
25376 char_u **fnamep; /* file name so far */
25377 char_u **bufp; /* buffer for allocated file name or NULL */
25378 int *fnamelen; /* length of fnamep */
25379{
25380 int valid = 0;
25381 char_u *tail;
25382 char_u *s, *p, *pbuf;
25383 char_u dirname[MAXPATHL];
25384 int c;
25385 int has_fullname = 0;
25386#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025387 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 int has_shortname = 0;
25389#endif
25390
25391repeat:
25392 /* ":p" - full path/file_name */
25393 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25394 {
25395 has_fullname = 1;
25396
25397 valid |= VALID_PATH;
25398 *usedlen += 2;
25399
25400 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25401 if ((*fnamep)[0] == '~'
25402#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25403 && ((*fnamep)[1] == '/'
25404# ifdef BACKSLASH_IN_FILENAME
25405 || (*fnamep)[1] == '\\'
25406# endif
25407 || (*fnamep)[1] == NUL)
25408
25409#endif
25410 )
25411 {
25412 *fnamep = expand_env_save(*fnamep);
25413 vim_free(*bufp); /* free any allocated file name */
25414 *bufp = *fnamep;
25415 if (*fnamep == NULL)
25416 return -1;
25417 }
25418
25419 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025420 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 {
25422 if (vim_ispathsep(*p)
25423 && p[1] == '.'
25424 && (p[2] == NUL
25425 || vim_ispathsep(p[2])
25426 || (p[2] == '.'
25427 && (p[3] == NUL || vim_ispathsep(p[3])))))
25428 break;
25429 }
25430
25431 /* FullName_save() is slow, don't use it when not needed. */
25432 if (*p != NUL || !vim_isAbsName(*fnamep))
25433 {
25434 *fnamep = FullName_save(*fnamep, *p != NUL);
25435 vim_free(*bufp); /* free any allocated file name */
25436 *bufp = *fnamep;
25437 if (*fnamep == NULL)
25438 return -1;
25439 }
25440
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025441#ifdef WIN3264
25442# if _WIN32_WINNT >= 0x0500
25443 if (vim_strchr(*fnamep, '~') != NULL)
25444 {
25445 /* Expand 8.3 filename to full path. Needed to make sure the same
25446 * file does not have two different names.
25447 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25448 p = alloc(_MAX_PATH + 1);
25449 if (p != NULL)
25450 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025451 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025452 {
25453 vim_free(*bufp);
25454 *bufp = *fnamep = p;
25455 }
25456 else
25457 vim_free(p);
25458 }
25459 }
25460# endif
25461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 /* Append a path separator to a directory. */
25463 if (mch_isdir(*fnamep))
25464 {
25465 /* Make room for one or two extra characters. */
25466 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25467 vim_free(*bufp); /* free any allocated file name */
25468 *bufp = *fnamep;
25469 if (*fnamep == NULL)
25470 return -1;
25471 add_pathsep(*fnamep);
25472 }
25473 }
25474
25475 /* ":." - path relative to the current directory */
25476 /* ":~" - path relative to the home directory */
25477 /* ":8" - shortname path - postponed till after */
25478 while (src[*usedlen] == ':'
25479 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25480 {
25481 *usedlen += 2;
25482 if (c == '8')
25483 {
25484#ifdef WIN3264
25485 has_shortname = 1; /* Postpone this. */
25486#endif
25487 continue;
25488 }
25489 pbuf = NULL;
25490 /* Need full path first (use expand_env() to remove a "~/") */
25491 if (!has_fullname)
25492 {
25493 if (c == '.' && **fnamep == '~')
25494 p = pbuf = expand_env_save(*fnamep);
25495 else
25496 p = pbuf = FullName_save(*fnamep, FALSE);
25497 }
25498 else
25499 p = *fnamep;
25500
25501 has_fullname = 0;
25502
25503 if (p != NULL)
25504 {
25505 if (c == '.')
25506 {
25507 mch_dirname(dirname, MAXPATHL);
25508 s = shorten_fname(p, dirname);
25509 if (s != NULL)
25510 {
25511 *fnamep = s;
25512 if (pbuf != NULL)
25513 {
25514 vim_free(*bufp); /* free any allocated file name */
25515 *bufp = pbuf;
25516 pbuf = NULL;
25517 }
25518 }
25519 }
25520 else
25521 {
25522 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25523 /* Only replace it when it starts with '~' */
25524 if (*dirname == '~')
25525 {
25526 s = vim_strsave(dirname);
25527 if (s != NULL)
25528 {
25529 *fnamep = s;
25530 vim_free(*bufp);
25531 *bufp = s;
25532 }
25533 }
25534 }
25535 vim_free(pbuf);
25536 }
25537 }
25538
25539 tail = gettail(*fnamep);
25540 *fnamelen = (int)STRLEN(*fnamep);
25541
25542 /* ":h" - head, remove "/file_name", can be repeated */
25543 /* Don't remove the first "/" or "c:\" */
25544 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25545 {
25546 valid |= VALID_HEAD;
25547 *usedlen += 2;
25548 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025549 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025550 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 *fnamelen = (int)(tail - *fnamep);
25552#ifdef VMS
25553 if (*fnamelen > 0)
25554 *fnamelen += 1; /* the path separator is part of the path */
25555#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025556 if (*fnamelen == 0)
25557 {
25558 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25559 p = vim_strsave((char_u *)".");
25560 if (p == NULL)
25561 return -1;
25562 vim_free(*bufp);
25563 *bufp = *fnamep = tail = p;
25564 *fnamelen = 1;
25565 }
25566 else
25567 {
25568 while (tail > s && !after_pathsep(s, tail))
25569 mb_ptr_back(*fnamep, tail);
25570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025571 }
25572
25573 /* ":8" - shortname */
25574 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25575 {
25576 *usedlen += 2;
25577#ifdef WIN3264
25578 has_shortname = 1;
25579#endif
25580 }
25581
25582#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025583 /*
25584 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 */
25586 if (has_shortname)
25587 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025588 /* Copy the string if it is shortened by :h and when it wasn't copied
25589 * yet, because we are going to change it in place. Avoids changing
25590 * the buffer name for "%:8". */
25591 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 {
25593 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025594 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595 return -1;
25596 vim_free(*bufp);
25597 *bufp = *fnamep = p;
25598 }
25599
25600 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025601 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 if (!has_fullname && !vim_isAbsName(*fnamep))
25603 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025604 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605 return -1;
25606 }
25607 else
25608 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025609 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610
Bram Moolenaardc935552011-08-17 15:23:23 +020025611 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025613 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 return -1;
25615
25616 if (l == 0)
25617 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025618 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025620 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 return -1;
25622 }
25623 *fnamelen = l;
25624 }
25625 }
25626#endif /* WIN3264 */
25627
25628 /* ":t" - tail, just the basename */
25629 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25630 {
25631 *usedlen += 2;
25632 *fnamelen -= (int)(tail - *fnamep);
25633 *fnamep = tail;
25634 }
25635
25636 /* ":e" - extension, can be repeated */
25637 /* ":r" - root, without extension, can be repeated */
25638 while (src[*usedlen] == ':'
25639 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25640 {
25641 /* find a '.' in the tail:
25642 * - for second :e: before the current fname
25643 * - otherwise: The last '.'
25644 */
25645 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25646 s = *fnamep - 2;
25647 else
25648 s = *fnamep + *fnamelen - 1;
25649 for ( ; s > tail; --s)
25650 if (s[0] == '.')
25651 break;
25652 if (src[*usedlen + 1] == 'e') /* :e */
25653 {
25654 if (s > tail)
25655 {
25656 *fnamelen += (int)(*fnamep - (s + 1));
25657 *fnamep = s + 1;
25658#ifdef VMS
25659 /* cut version from the extension */
25660 s = *fnamep + *fnamelen - 1;
25661 for ( ; s > *fnamep; --s)
25662 if (s[0] == ';')
25663 break;
25664 if (s > *fnamep)
25665 *fnamelen = s - *fnamep;
25666#endif
25667 }
25668 else if (*fnamep <= tail)
25669 *fnamelen = 0;
25670 }
25671 else /* :r */
25672 {
25673 if (s > tail) /* remove one extension */
25674 *fnamelen = (int)(s - *fnamep);
25675 }
25676 *usedlen += 2;
25677 }
25678
25679 /* ":s?pat?foo?" - substitute */
25680 /* ":gs?pat?foo?" - global substitute */
25681 if (src[*usedlen] == ':'
25682 && (src[*usedlen + 1] == 's'
25683 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25684 {
25685 char_u *str;
25686 char_u *pat;
25687 char_u *sub;
25688 int sep;
25689 char_u *flags;
25690 int didit = FALSE;
25691
25692 flags = (char_u *)"";
25693 s = src + *usedlen + 2;
25694 if (src[*usedlen + 1] == 'g')
25695 {
25696 flags = (char_u *)"g";
25697 ++s;
25698 }
25699
25700 sep = *s++;
25701 if (sep)
25702 {
25703 /* find end of pattern */
25704 p = vim_strchr(s, sep);
25705 if (p != NULL)
25706 {
25707 pat = vim_strnsave(s, (int)(p - s));
25708 if (pat != NULL)
25709 {
25710 s = p + 1;
25711 /* find end of substitution */
25712 p = vim_strchr(s, sep);
25713 if (p != NULL)
25714 {
25715 sub = vim_strnsave(s, (int)(p - s));
25716 str = vim_strnsave(*fnamep, *fnamelen);
25717 if (sub != NULL && str != NULL)
25718 {
25719 *usedlen = (int)(p + 1 - src);
25720 s = do_string_sub(str, pat, sub, flags);
25721 if (s != NULL)
25722 {
25723 *fnamep = s;
25724 *fnamelen = (int)STRLEN(s);
25725 vim_free(*bufp);
25726 *bufp = s;
25727 didit = TRUE;
25728 }
25729 }
25730 vim_free(sub);
25731 vim_free(str);
25732 }
25733 vim_free(pat);
25734 }
25735 }
25736 /* after using ":s", repeat all the modifiers */
25737 if (didit)
25738 goto repeat;
25739 }
25740 }
25741
Bram Moolenaar26df0922014-02-23 23:39:13 +010025742 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25743 {
25744 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25745 if (p == NULL)
25746 return -1;
25747 vim_free(*bufp);
25748 *bufp = *fnamep = p;
25749 *fnamelen = (int)STRLEN(p);
25750 *usedlen += 2;
25751 }
25752
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 return valid;
25754}
25755
25756/*
25757 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25758 * "flags" can be "g" to do a global substitute.
25759 * Returns an allocated string, NULL for error.
25760 */
25761 char_u *
25762do_string_sub(str, pat, sub, flags)
25763 char_u *str;
25764 char_u *pat;
25765 char_u *sub;
25766 char_u *flags;
25767{
25768 int sublen;
25769 regmatch_T regmatch;
25770 int i;
25771 int do_all;
25772 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025773 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 garray_T ga;
25775 char_u *ret;
25776 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025777 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778
25779 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25780 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025781 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782
25783 ga_init2(&ga, 1, 200);
25784
25785 do_all = (flags[0] == 'g');
25786
25787 regmatch.rm_ic = p_ic;
25788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25789 if (regmatch.regprog != NULL)
25790 {
25791 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025792 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25794 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025795 /* Skip empty match except for first match. */
25796 if (regmatch.startp[0] == regmatch.endp[0])
25797 {
25798 if (zero_width == regmatch.startp[0])
25799 {
25800 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025801 i = MB_PTR2LEN(tail);
25802 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25803 (size_t)i);
25804 ga.ga_len += i;
25805 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025806 continue;
25807 }
25808 zero_width = regmatch.startp[0];
25809 }
25810
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811 /*
25812 * Get some space for a temporary buffer to do the substitution
25813 * into. It will contain:
25814 * - The text up to where the match is.
25815 * - The substituted text.
25816 * - The text after the match.
25817 */
25818 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025819 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25821 {
25822 ga_clear(&ga);
25823 break;
25824 }
25825
25826 /* copy the text up to where the match is */
25827 i = (int)(regmatch.startp[0] - tail);
25828 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25829 /* add the substituted text */
25830 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25831 + ga.ga_len + i, TRUE, TRUE, FALSE);
25832 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025833 tail = regmatch.endp[0];
25834 if (*tail == NUL)
25835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 if (!do_all)
25837 break;
25838 }
25839
25840 if (ga.ga_data != NULL)
25841 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25842
Bram Moolenaar473de612013-06-08 18:19:48 +020025843 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 }
25845
25846 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25847 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025848 if (p_cpo == empty_option)
25849 p_cpo = save_cpo;
25850 else
25851 /* Darn, evaluating {sub} expression changed the value. */
25852 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853
25854 return ret;
25855}
25856
25857#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */