blob: a2d2939ba236f615e232c2fd954871a7bed8c9bc [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 Moolenaard6e256c2011-12-14 15:32:50 +0100470static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200474static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100476static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100493static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200529static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200562static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200566static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000574static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000575static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200576static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100588static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000591static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000605static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100610static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200628#ifdef FEAT_LUA
629static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200636static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
645static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
649static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100653static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000654static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000659static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200661#ifdef FEAT_PYTHON3
662static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
663#endif
664#ifdef FEAT_PYTHON
665static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000668static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000669static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#ifdef FEAT_FLOAT
682static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200684static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100686static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000689static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000691static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200696static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000699static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000700static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000701static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000702static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200704static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000705static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100707#ifdef FEAT_CRYPT
708static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
709#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000710static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200711static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200715static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
723static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000726static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728#ifdef HAVE_STRFTIME
729static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
731static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200737static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200738static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000744static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200745static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200747static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000748static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000749static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000751static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000752static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000754static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200755#ifdef FEAT_FLOAT
756static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
763static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200766static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200767static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100768static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100772static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000779static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100783static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784
Bram Moolenaar493c1782014-05-28 14:34:46 +0200785static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000786static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static int get_env_len __ARGS((char_u **arg));
788static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000789static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000790static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
791#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
792#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
793 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000794static 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 +0000795static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000796static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200797static 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 +0000798static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static typval_T *alloc_tv __ARGS((void));
800static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static void init_tv __ARGS((typval_T *varp));
802static long get_tv_number __ARGS((typval_T *varp));
803static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000804static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static char_u *get_tv_string __ARGS((typval_T *varp));
806static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000807static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100808static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
809static 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 +0000810static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
811static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
812static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000813static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
814static 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 +0000815static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
817static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200818static int var_check_func_name __ARGS((char_u *name, int new_var));
819static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200820static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000821static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
823static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
824static int eval_fname_script __ARGS((char_u *p));
825static int eval_fname_sid __ARGS((char_u *p));
826static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static ufunc_T *find_func __ARGS((char_u *name));
828static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200829static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830#ifdef FEAT_PROFILE
831static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000832static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
833static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
834static int
835# ifdef __BORLANDC__
836 _RTLENTRYF
837# endif
838 prof_total_cmp __ARGS((const void *s1, const void *s2));
839static int
840# ifdef __BORLANDC__
841 _RTLENTRYF
842# endif
843 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200845static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000847static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849static 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 +0000850static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
851static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
854static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000855static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000856static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000857static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200858static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200859static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200861
862#ifdef EBCDIC
863static int compare_func_name __ARGS((const void *s1, const void *s2));
864static void sortFunctions __ARGS(());
865#endif
866
Bram Moolenaar33570922005-01-25 22:26:29 +0000867/*
868 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000869 */
870 void
871eval_init()
872{
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 int i;
874 struct vimvar *p;
875
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200876 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
877 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200878 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881
882 for (i = 0; i < VV_LEN; ++i)
883 {
884 p = &vimvars[i];
885 STRCPY(p->vv_di.di_key, p->vv_name);
886 if (p->vv_flags & VV_RO)
887 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
888 else if (p->vv_flags & VV_RO_SBX)
889 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
890 else
891 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000892
893 /* add to v: scope dict, unless the value is not always available */
894 if (p->vv_type != VAR_UNKNOWN)
895 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000896 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000897 /* add to compat scope dict */
898 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000900 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100901 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200902 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100903 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200904 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
907 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100908 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909 */
910 sortFunctions();
911#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000912}
913
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914#if defined(EXITFREE) || defined(PROTO)
915 void
916eval_clear()
917{
918 int i;
919 struct vimvar *p;
920
921 for (i = 0; i < VV_LEN; ++i)
922 {
923 p = &vimvars[i];
924 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000925 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000926 vim_free(p->vv_str);
927 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000928 }
929 else if (p->vv_di.di_tv.v_type == VAR_LIST)
930 {
931 list_unref(p->vv_list);
932 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000934 }
935 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000936 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937 hash_clear(&compat_hashtab);
938
Bram Moolenaard9fba312005-06-26 22:34:35 +0000939 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100940# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200941 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100942# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000943
944 /* global variables */
945 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000946
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000947 /* autoloaded script names */
948 ga_clear_strings(&ga_loaded);
949
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 /* Script-local variables. First clear all the variables and in a second
951 * loop free the scriptvar_T, because a variable in one script might hold
952 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200953 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200954 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200955 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 ga_clear(&ga_scripts);
958
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000959 /* unreferenced lists and dicts */
960 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000961
962 /* functions */
963 free_all_functions();
964 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965}
966#endif
967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * Return the name of the executed function.
970 */
971 char_u *
972func_name(cookie)
973 void *cookie;
974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000975 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the address holding the next breakpoint line for a funccall cookie.
980 */
981 linenr_T *
982func_breakpoint(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/*
989 * Return the address holding the debug tick for a funccall cookie.
990 */
991 int *
992func_dbg_tick(cookie)
993 void *cookie;
994{
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
999 * Return the nesting level for a funccall cookie.
1000 */
1001 int
1002func_level(cookie)
1003 void *cookie;
1004{
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
1008/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001009funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001011/* pointer to list of previously used funccal, still around because some
1012 * item in it is still being used. */
1013funccall_T *previous_funccal = NULL;
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015/*
1016 * Return TRUE when a function was ended by a ":return" command.
1017 */
1018 int
1019current_func_returned()
1020{
1021 return current_funccal->returned;
1022}
1023
1024
1025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Set an internal variable to a string value. Creates the variable if it does
1027 * not already exist.
1028 */
1029 void
1030set_internal_string_var(name, value)
1031 char_u *name;
1032 char_u *value;
1033{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001034 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 val = vim_strsave(value);
1038 if (val != NULL)
1039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001040 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001041 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001043 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046 }
1047}
1048
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051static char_u *redir_endp = NULL;
1052static char_u *redir_varname = NULL;
1053
1054/*
1055 * Start recording command output to a variable
1056 * Returns OK if successfully completed the setup. FAIL otherwise.
1057 */
1058 int
1059var_redir_start(name, append)
1060 char_u *name;
1061 int append; /* append to an existing variable */
1062{
1063 int save_emsg;
1064 int err;
1065 typval_T tv;
1066
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001068 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 {
1070 EMSG(_(e_invarg));
1071 return FAIL;
1072 }
1073
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 redir_varname = vim_strsave(name);
1076 if (redir_varname == NULL)
1077 return FAIL;
1078
1079 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1080 if (redir_lval == NULL)
1081 {
1082 var_redir_stop();
1083 return FAIL;
1084 }
1085
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086 /* The output is stored in growarray "redir_ga" until redirection ends. */
1087 ga_init2(&redir_ga, (int)sizeof(char), 500);
1088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001090 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001091 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1093 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001094 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (redir_endp != NULL && *redir_endp != NUL)
1096 /* Trailing characters are present after the variable name */
1097 EMSG(_(e_trailing));
1098 else
1099 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
1104
1105 /* check if we can write to the variable: set it to or append an empty
1106 * string */
1107 save_emsg = did_emsg;
1108 did_emsg = FALSE;
1109 tv.v_type = VAR_STRING;
1110 tv.vval.v_string = (char_u *)"";
1111 if (append)
1112 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1113 else
1114 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001115 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001117 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 if (err)
1119 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 var_redir_stop();
1122 return FAIL;
1123 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124
1125 return OK;
1126}
1127
1128/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 * Append "value[value_len]" to the variable set by var_redir_start().
1130 * The actual appending is postponed until redirection ends, because the value
1131 * appended may in fact be the string we write to, changing it may cause freed
1132 * memory to be used:
1133 * :redir => foo
1134 * :let foo
1135 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 */
1137 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143
1144 if (redir_lval == NULL)
1145 return;
1146
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001152 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153 {
1154 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 }
1157 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159}
1160
1161/*
1162 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 */
1165 void
1166var_redir_stop()
1167{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168 typval_T tv;
1169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 if (redir_lval != NULL)
1171 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 /* If there was no error: assign the text to the variable. */
1173 if (redir_endp != NULL)
1174 {
1175 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1176 tv.v_type = VAR_STRING;
1177 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001178 /* Call get_lval() again, if it's inside a Dict or List it may
1179 * have changed. */
1180 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001181 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001182 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1183 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1184 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001185 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 /* free the collected output */
1188 vim_free(redir_ga.ga_data);
1189 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 vim_free(redir_lval);
1192 redir_lval = NULL;
1193 }
1194 vim_free(redir_varname);
1195 redir_varname = NULL;
1196}
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# if defined(FEAT_MBYTE) || defined(PROTO)
1199 int
1200eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1201 char_u *enc_from;
1202 char_u *enc_to;
1203 char_u *fname_from;
1204 char_u *fname_to;
1205{
1206 int err = FALSE;
1207
1208 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1209 set_vim_var_string(VV_CC_TO, enc_to, -1);
1210 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1211 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1212 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_CC_FROM, NULL, -1);
1215 set_vim_var_string(VV_CC_TO, NULL, -1);
1216 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1217 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1218
1219 if (err)
1220 return FAIL;
1221 return OK;
1222}
1223# endif
1224
1225# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1226 int
1227eval_printexpr(fname, args)
1228 char_u *fname;
1229 char_u *args;
1230{
1231 int err = FALSE;
1232
1233 set_vim_var_string(VV_FNAME_IN, fname, -1);
1234 set_vim_var_string(VV_CMDARG, args, -1);
1235 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1236 err = TRUE;
1237 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1238 set_vim_var_string(VV_CMDARG, NULL, -1);
1239
1240 if (err)
1241 {
1242 mch_remove(fname);
1243 return FAIL;
1244 }
1245 return OK;
1246}
1247# endif
1248
1249# if defined(FEAT_DIFF) || defined(PROTO)
1250 void
1251eval_diff(origfile, newfile, outfile)
1252 char_u *origfile;
1253 char_u *newfile;
1254 char_u *outfile;
1255{
1256 int err = FALSE;
1257
1258 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1259 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1260 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1261 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1262 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1263 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1264 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1265}
1266
1267 void
1268eval_patch(origfile, difffile, outfile)
1269 char_u *origfile;
1270 char_u *difffile;
1271 char_u *outfile;
1272{
1273 int err;
1274
1275 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1276 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1277 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1278 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1281 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1282}
1283# endif
1284
1285/*
1286 * Top level evaluation function, returning a boolean.
1287 * Sets "error" to TRUE if there was an error.
1288 * Return TRUE or FALSE.
1289 */
1290 int
1291eval_to_bool(arg, error, nextcmd, skip)
1292 char_u *arg;
1293 int *error;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int retval = FALSE;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 else
1305 {
1306 *error = FALSE;
1307 if (!skip)
1308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001309 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
1320 * Top level evaluation function, returning a string. If "skip" is TRUE,
1321 * only parsing to "nextcmd" is done, without reporting errors. Return
1322 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1323 */
1324 char_u *
1325eval_to_string_skip(arg, nextcmd, skip)
1326 char_u *arg;
1327 char_u **nextcmd;
1328 int skip; /* only parse, don't execute */
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *retval;
1332
1333 if (skip)
1334 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 retval = NULL;
1337 else
1338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 retval = vim_strsave(get_tv_string(&tv));
1340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342 if (skip)
1343 --emsg_skip;
1344
1345 return retval;
1346}
1347
1348/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001349 * Skip over an expression at "*pp".
1350 * Return FAIL for an error, OK otherwise.
1351 */
1352 int
1353skip_expr(pp)
1354 char_u **pp;
1355{
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001357
1358 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360}
1361
1362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364 * When "convert" is TRUE convert a List into a sequence of lines and convert
1365 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Return pointer to allocated memory, or NULL for failure.
1367 */
1368 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001369eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *arg;
1371 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001377#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 retval = NULL;
1383 else
1384 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001385 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001386 {
1387 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001388 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001389 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001390 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001391 if (tv.vval.v_list->lv_len > 0)
1392 ga_append(&ga, NL);
1393 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 ga_append(&ga, NUL);
1395 retval = (char_u *)ga.ga_data;
1396 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001397#ifdef FEAT_FLOAT
1398 else if (convert && tv.v_type == VAR_FLOAT)
1399 {
1400 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1401 retval = vim_strsave(numbuf);
1402 }
1403#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001404 else
1405 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408
1409 return retval;
1410}
1411
1412/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 * Call eval_to_string() without using current local variables and using
1414 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 */
1416 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char_u *arg;
1419 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 char_u *retval;
1423 void *save_funccalp;
1424
1425 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 if (use_sandbox)
1427 ++sandbox;
1428 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001430 if (use_sandbox)
1431 --sandbox;
1432 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 restore_funccal(save_funccalp);
1434 return retval;
1435}
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437/*
1438 * Top level evaluation function, returning a number.
1439 * Evaluates "expr" silently.
1440 * Returns -1 for an error.
1441 */
1442 int
1443eval_to_number(expr)
1444 char_u *expr;
1445{
Bram Moolenaar33570922005-01-25 22:26:29 +00001446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001448 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 ++emsg_off;
1451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 retval = -1;
1454 else
1455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001456 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 --emsg_off;
1460
1461 return retval;
1462}
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464/*
1465 * Prepare v: variable "idx" to be used.
1466 * Save the current typeval in "save_tv".
1467 * When not used yet add the variable to the v: hashtable.
1468 */
1469 static void
1470prepare_vimvar(idx, save_tv)
1471 int idx;
1472 typval_T *save_tv;
1473{
1474 *save_tv = vimvars[idx].vv_tv;
1475 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1476 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1477}
1478
1479/*
1480 * Restore v: variable "idx" to typeval "save_tv".
1481 * When no longer defined, remove the variable from the v: hashtable.
1482 */
1483 static void
1484restore_vimvar(idx, save_tv)
1485 int idx;
1486 typval_T *save_tv;
1487{
1488 hashitem_T *hi;
1489
Bram Moolenaara40058a2005-07-11 22:42:07 +00001490 vimvars[idx].vv_tv = *save_tv;
1491 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1492 {
1493 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1494 if (HASHITEM_EMPTY(hi))
1495 EMSG2(_(e_intern2), "restore_vimvar()");
1496 else
1497 hash_remove(&vimvarht, hi);
1498 }
1499}
1500
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001501#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502/*
1503 * Evaluate an expression to a list with suggestions.
1504 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001505 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506 */
1507 list_T *
1508eval_spell_expr(badword, expr)
1509 char_u *badword;
1510 char_u *expr;
1511{
1512 typval_T save_val;
1513 typval_T rettv;
1514 list_T *list = NULL;
1515 char_u *p = skipwhite(expr);
1516
1517 /* Set "v:val" to the bad word. */
1518 prepare_vimvar(VV_VAL, &save_val);
1519 vimvars[VV_VAL].vv_type = VAR_STRING;
1520 vimvars[VV_VAL].vv_str = badword;
1521 if (p_verbose == 0)
1522 ++emsg_off;
1523
1524 if (eval1(&p, &rettv, TRUE) == OK)
1525 {
1526 if (rettv.v_type != VAR_LIST)
1527 clear_tv(&rettv);
1528 else
1529 list = rettv.vval.v_list;
1530 }
1531
1532 if (p_verbose == 0)
1533 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534 restore_vimvar(VV_VAL, &save_val);
1535
1536 return list;
1537}
1538
1539/*
1540 * "list" is supposed to contain two items: a word and a number. Return the
1541 * word in "pp" and the number as the return value.
1542 * Return -1 if anything isn't right.
1543 * Used to get the good word and score from the eval_spell_expr() result.
1544 */
1545 int
1546get_spellword(list, pp)
1547 list_T *list;
1548 char_u **pp;
1549{
1550 listitem_T *li;
1551
1552 li = list->lv_first;
1553 if (li == NULL)
1554 return -1;
1555 *pp = get_tv_string(&li->li_tv);
1556
1557 li = li->li_next;
1558 if (li == NULL)
1559 return -1;
1560 return get_tv_number(&li->li_tv);
1561}
1562#endif
1563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001564/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001565 * Top level evaluation function.
1566 * Returns an allocated typval_T with the result.
1567 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 */
1569 typval_T *
1570eval_expr(arg, nextcmd)
1571 char_u *arg;
1572 char_u **nextcmd;
1573{
1574 typval_T *tv;
1575
1576 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001577 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001578 {
1579 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 }
1582
1583 return tv;
1584}
1585
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 * Uses argv[argc] for the function arguments. Only Number and String
1590 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001593 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 char_u *func;
1596 int argc;
1597 char_u **argv;
1598 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001599 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 long n;
1604 int len;
1605 int i;
1606 int doesrange;
1607 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001610 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 for (i = 0; i < argc; i++)
1615 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 /* Pass a NULL or empty argument as an empty string */
1617 if (argv[i] == NULL || *argv[i] == NUL)
1618 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001619 argvars[i].v_type = VAR_STRING;
1620 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001621 continue;
1622 }
1623
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001624 if (str_arg_only)
1625 len = 0;
1626 else
1627 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001628 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (len != 0 && len == (int)STRLEN(argv[i]))
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_NUMBER;
1632 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 else
1635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001636 argvars[i].v_type = VAR_STRING;
1637 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
1639 }
1640
1641 if (safe)
1642 {
1643 save_funccalp = save_funccal();
1644 ++sandbox;
1645 }
1646
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1648 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (safe)
1652 {
1653 --sandbox;
1654 restore_funccal(save_funccalp);
1655 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 vim_free(argvars);
1657
1658 if (ret == FAIL)
1659 clear_tv(rettv);
1660
1661 return ret;
1662}
1663
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001664/*
1665 * Call vimL function "func" and return the result as a number.
1666 * Returns -1 when calling the function fails.
1667 * Uses argv[argc] for the function arguments.
1668 */
1669 long
1670call_func_retnr(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677 long retval;
1678
1679 /* All arguments are passed as strings, no conversion to number. */
1680 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1681 return -1;
1682
1683 retval = get_tv_number_chk(&rettv, NULL);
1684 clear_tv(&rettv);
1685 return retval;
1686}
1687
1688#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1689 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1690
Bram Moolenaar4f688582007-07-24 12:34:30 +00001691# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001693 * Call vimL function "func" and return the result as a string.
1694 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 * Uses argv[argc] for the function arguments.
1696 */
1697 void *
1698call_func_retstr(func, argc, argv, safe)
1699 char_u *func;
1700 int argc;
1701 char_u **argv;
1702 int safe; /* use the sandbox */
1703{
1704 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001705 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001707 /* All arguments are passed as strings, no conversion to number. */
1708 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709 return NULL;
1710
1711 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return retval;
1714}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001718 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 */
1722 void *
1723call_func_retlist(func, argc, argv, safe)
1724 char_u *func;
1725 int argc;
1726 char_u **argv;
1727 int safe; /* use the sandbox */
1728{
1729 typval_T rettv;
1730
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 return NULL;
1734
1735 if (rettv.v_type != VAR_LIST)
1736 {
1737 clear_tv(&rettv);
1738 return NULL;
1739 }
1740
1741 return rettv.vval.v_list;
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744
1745/*
1746 * Save the current function call pointer, and set it to NULL.
1747 * Used when executing autocommands and for ":source".
1748 */
1749 void *
1750save_funccal()
1751{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 current_funccal = NULL;
1755 return (void *)fc;
1756}
1757
1758 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001759restore_funccal(vfc)
1760 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762 funccall_T *fc = (funccall_T *)vfc;
1763
1764 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765}
1766
Bram Moolenaar05159a02005-02-26 23:04:13 +00001767#if defined(FEAT_PROFILE) || defined(PROTO)
1768/*
1769 * Prepare profiling for entering a child or something else that is not
1770 * counted for the script/function itself.
1771 * Should always be called in pair with prof_child_exit().
1772 */
1773 void
1774prof_child_enter(tm)
1775 proftime_T *tm; /* place to store waittime */
1776{
1777 funccall_T *fc = current_funccal;
1778
1779 if (fc != NULL && fc->func->uf_profiling)
1780 profile_start(&fc->prof_child);
1781 script_prof_save(tm);
1782}
1783
1784/*
1785 * Take care of time spent in a child.
1786 * Should always be called after prof_child_enter().
1787 */
1788 void
1789prof_child_exit(tm)
1790 proftime_T *tm; /* where waittime was stored */
1791{
1792 funccall_T *fc = current_funccal;
1793
1794 if (fc != NULL && fc->func->uf_profiling)
1795 {
1796 profile_end(&fc->prof_child);
1797 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1798 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1799 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1800 }
1801 script_prof_restore(tm);
1802}
1803#endif
1804
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_FOLDING
1807/*
1808 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1809 * it in "*cp". Doesn't give error messages.
1810 */
1811 int
1812eval_foldexpr(arg, cp)
1813 char_u *arg;
1814 int *cp;
1815{
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 int retval;
1818 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001819 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1820 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001823 if (use_sandbox)
1824 ++sandbox;
1825 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 retval = 0;
1829 else
1830 {
1831 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 if (tv.v_type == VAR_NUMBER)
1833 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001834 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 retval = 0;
1836 else
1837 {
1838 /* If the result is a string, check if there is a non-digit before
1839 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (!VIM_ISDIGIT(*s) && *s != '-')
1842 *cp = *s++;
1843 retval = atol((char *)s);
1844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001848 if (use_sandbox)
1849 --sandbox;
1850 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 return retval;
1853}
1854#endif
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 * ":let" list all variable values
1858 * ":let var1 var2" list variable values
1859 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 * ":let var += expr" assignment command.
1861 * ":let var -= expr" assignment command.
1862 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 */
1865 void
1866ex_let(eap)
1867 exarg_T *eap;
1868{
1869 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 int var_count = 0;
1874 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001877 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 argend = skip_var_list(arg, &var_count, &semicolon);
1880 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001882 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1883 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001884 expr = skipwhite(argend);
1885 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1886 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001888 /*
1889 * ":let" without "=": list variables
1890 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 if (*arg == '[')
1892 EMSG(_(e_invarg));
1893 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 list_glob_vars(&first);
1900 list_buf_vars(&first);
1901 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001904#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_script_vars(&first);
1906 list_func_vars(&first);
1907 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 eap->nextcmd = check_nextcmd(arg);
1910 }
1911 else
1912 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op[0] = '=';
1914 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1918 op[0] = *expr; /* +=, -= or .= */
1919 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001921 else
1922 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (eap->skip)
1925 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 {
1929 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 --emsg_skip;
1932 }
1933 else if (i != FAIL)
1934 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
1940}
1941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942/*
1943 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1944 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 * When "nextchars" is not NULL it points to a string with characters that
1946 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1947 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 * Returns OK or FAIL;
1949 */
1950 static int
1951ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1952 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 int copy; /* copy values from "tv", don't move */
1955 int semicolon; /* from skip_var_list() */
1956 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958{
1959 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 listitem_T *item;
1963 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964
1965 if (*arg != '[')
1966 {
1967 /*
1968 * ":let var = expr" or ":for var in list"
1969 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 return FAIL;
1972 return OK;
1973 }
1974
1975 /*
1976 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1977 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001978 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 {
1980 EMSG(_(e_listreq));
1981 return FAIL;
1982 }
1983
1984 i = list_len(l);
1985 if (semicolon == 0 && var_count < i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990 if (var_count - semicolon > i)
1991 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001992 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 return FAIL;
1994 }
1995
1996 item = l->lv_first;
1997 while (*arg != ']')
1998 {
1999 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 item = item->li_next;
2002 if (arg == NULL)
2003 return FAIL;
2004
2005 arg = skipwhite(arg);
2006 if (*arg == ';')
2007 {
2008 /* Put the rest of the list (may be empty) in the var after ';'.
2009 * Create a new list for this. */
2010 l = list_alloc();
2011 if (l == NULL)
2012 return FAIL;
2013 while (item != NULL)
2014 {
2015 list_append_tv(l, &item->li_tv);
2016 item = item->li_next;
2017 }
2018
2019 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002020 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 ltv.vval.v_list = l;
2022 l->lv_refcount = 1;
2023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2025 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 clear_tv(&ltv);
2027 if (arg == NULL)
2028 return FAIL;
2029 break;
2030 }
2031 else if (*arg != ',' && *arg != ']')
2032 {
2033 EMSG2(_(e_intern2), "ex_let_vars()");
2034 return FAIL;
2035 }
2036 }
2037
2038 return OK;
2039}
2040
2041/*
2042 * Skip over assignable variable "var" or list of variables "[var, var]".
2043 * Used for ":let varvar = expr" and ":for varvar in expr".
2044 * For "[var, var]" increment "*var_count" for each variable.
2045 * for "[var, var; var]" set "semicolon".
2046 * Return NULL for an error.
2047 */
2048 static char_u *
2049skip_var_list(arg, var_count, semicolon)
2050 char_u *arg;
2051 int *var_count;
2052 int *semicolon;
2053{
2054 char_u *p, *s;
2055
2056 if (*arg == '[')
2057 {
2058 /* "[var, var]": find the matching ']'. */
2059 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002060 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 {
2062 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2063 s = skip_var_one(p);
2064 if (s == p)
2065 {
2066 EMSG2(_(e_invarg2), p);
2067 return NULL;
2068 }
2069 ++*var_count;
2070
2071 p = skipwhite(s);
2072 if (*p == ']')
2073 break;
2074 else if (*p == ';')
2075 {
2076 if (*semicolon == 1)
2077 {
2078 EMSG(_("Double ; in list of variables"));
2079 return NULL;
2080 }
2081 *semicolon = 1;
2082 }
2083 else if (*p != ',')
2084 {
2085 EMSG2(_(e_invarg2), p);
2086 return NULL;
2087 }
2088 }
2089 return p + 1;
2090 }
2091 else
2092 return skip_var_one(arg);
2093}
2094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002096 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002097 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 static char_u *
2100skip_var_one(arg)
2101 char_u *arg;
2102{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 if (*arg == '@' && arg[1] != NUL)
2104 return arg + 2;
2105 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2106 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107}
2108
Bram Moolenaara7043832005-01-21 11:56:39 +00002109/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 * List variables for hashtab "ht" with prefix "prefix".
2111 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002116 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 hashitem_T *hi;
2121 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 int todo;
2123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2126 {
2127 if (!HASHITEM_EMPTY(hi))
2128 {
2129 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 di = HI2DI(hi);
2131 if (empty || di->di_tv.v_type != VAR_STRING
2132 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 }
2135 }
2136}
2137
2138/*
2139 * List global variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_glob_vars(first)
2143 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002146}
2147
2148/*
2149 * List buffer variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_buf_vars(first)
2153 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002154{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155 char_u numbuf[NUMBUFLEN];
2156
Bram Moolenaar429fa852013-04-15 12:27:36 +02002157 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159
2160 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2162 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002163}
2164
2165/*
2166 * List window variables.
2167 */
2168 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169list_win_vars(first)
2170 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002172 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174}
2175
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176#ifdef FEAT_WINDOWS
2177/*
2178 * List tab page variables.
2179 */
2180 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181list_tab_vars(first)
2182 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002184 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186}
2187#endif
2188
Bram Moolenaara7043832005-01-21 11:56:39 +00002189/*
2190 * List Vim variables.
2191 */
2192 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193list_vim_vars(first)
2194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197}
2198
2199/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200 * List script-local variables, if there is a script.
2201 */
2202 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203list_script_vars(first)
2204 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205{
2206 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2208 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209}
2210
2211/*
2212 * List function variables, if there is a function.
2213 */
2214 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215list_func_vars(first)
2216 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217{
2218 if (current_funccal != NULL)
2219 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221}
2222
2223/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 * List variables in "arg".
2225 */
2226 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002227list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 exarg_T *eap;
2229 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231{
2232 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 char_u *name_start;
2236 char_u *arg_subsc;
2237 char_u *tofree;
2238 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239
2240 while (!ends_excmd(*arg) && !got_int)
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002244 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2246 {
2247 emsg_severe = TRUE;
2248 EMSG(_(e_trailing));
2249 break;
2250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* get_name_len() takes care of expanding curly braces */
2255 name_start = name = arg;
2256 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2257 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 /* This is mainly to keep test 49 working: when expanding
2260 * curly braces fails overrule the exception error message. */
2261 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 emsg_severe = TRUE;
2264 EMSG2(_(e_invarg2), arg);
2265 break;
2266 }
2267 error = TRUE;
2268 }
2269 else
2270 {
2271 if (tofree != NULL)
2272 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002273 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 else
2276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* handle d.key, l[idx], f(expr) */
2278 arg_subsc = arg;
2279 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002287 case 'g': list_glob_vars(first); break;
2288 case 'b': list_buf_vars(first); break;
2289 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002290#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 'v': list_vim_vars(first); break;
2294 case 's': list_script_vars(first); break;
2295 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 default:
2297 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
2300 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 {
2302 char_u numbuf[NUMBUFLEN];
2303 char_u *tf;
2304 int c;
2305 char_u *s;
2306
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002307 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 c = *arg;
2309 *arg = NUL;
2310 list_one_var_a((char_u *)"",
2311 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 tv.v_type,
2313 s == NULL ? (char_u *)"" : s,
2314 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 *arg = c;
2316 vim_free(tf);
2317 }
2318 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322
2323 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328
2329 return arg;
2330}
2331
2332/*
2333 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2334 * Returns a pointer to the char just after the var name.
2335 * Returns NULL if there is an error.
2336 */
2337 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002340 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344{
2345 int c1;
2346 char_u *name;
2347 char_u *p;
2348 char_u *arg_end = NULL;
2349 int len;
2350 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352
2353 /*
2354 * ":let $VAR = expr": Set environment variable.
2355 */
2356 if (*arg == '$')
2357 {
2358 /* Find the end of the name. */
2359 ++arg;
2360 name = arg;
2361 len = get_env_len(&arg);
2362 if (len == 0)
2363 EMSG2(_(e_invarg2), name - 1);
2364 else
2365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 if (op != NULL && (*op == '+' || *op == '-'))
2367 EMSG2(_(e_letwrong), op);
2368 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002371 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
2373 c1 = name[len];
2374 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 p = get_tv_string_chk(tv);
2376 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 {
2378 int mustfree = FALSE;
2379 char_u *s = vim_getenv(name, &mustfree);
2380
2381 if (s != NULL)
2382 {
2383 p = tofree = concat_str(s, p);
2384 if (mustfree)
2385 vim_free(s);
2386 }
2387 }
2388 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 if (STRICMP(name, "HOME") == 0)
2392 init_homedir();
2393 else if (didset_vim && STRICMP(name, "VIM") == 0)
2394 didset_vim = FALSE;
2395 else if (didset_vimruntime
2396 && STRICMP(name, "VIMRUNTIME") == 0)
2397 didset_vimruntime = FALSE;
2398 arg_end = arg;
2399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
2403 }
2404 }
2405
2406 /*
2407 * ":let &option = expr": Set option value.
2408 * ":let &l:option = expr": Set local option value.
2409 * ":let &g:option = expr": Set global option value.
2410 */
2411 else if (*arg == '&')
2412 {
2413 /* Find the end of the name. */
2414 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002415 if (p == NULL || (endchars != NULL
2416 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 EMSG(_(e_letunexp));
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 long n;
2421 int opt_type;
2422 long numval;
2423 char_u *stringval = NULL;
2424 char_u *s;
2425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 c1 = *p;
2427 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428
2429 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 s = get_tv_string_chk(tv); /* != NULL if number or string */
2431 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 {
2433 opt_type = get_option_value(arg, &numval,
2434 &stringval, opt_flags);
2435 if ((opt_type == 1 && *op == '.')
2436 || (opt_type == 0 && *op != '.'))
2437 EMSG2(_(e_letwrong), op);
2438 else
2439 {
2440 if (opt_type == 1) /* number */
2441 {
2442 if (*op == '+')
2443 n = numval + n;
2444 else
2445 n = numval - n;
2446 }
2447 else if (opt_type == 0 && stringval != NULL) /* string */
2448 {
2449 s = concat_str(stringval, s);
2450 vim_free(stringval);
2451 stringval = s;
2452 }
2453 }
2454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 if (s != NULL)
2456 {
2457 set_option_value(arg, n, s, opt_flags);
2458 arg_end = p;
2459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let @r = expr": Set register contents.
2467 */
2468 else if (*arg == '@')
2469 {
2470 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (op != NULL && (*op == '+' || *op == '-'))
2472 EMSG2(_(e_letwrong), op);
2473 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002474 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002478 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 char_u *s;
2480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 p = get_tv_string_chk(tv);
2482 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002484 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (s != NULL)
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 vim_free(s);
2489 }
2490 }
2491 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 arg_end = arg + 1;
2495 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498 }
2499
2500 /*
2501 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002504 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002508 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2512 EMSG(_(e_letunexp));
2513 else
2514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 arg_end = p;
2517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
2521
2522 else
2523 EMSG2(_(e_invarg2), arg);
2524
2525 return arg_end;
2526}
2527
2528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2530 */
2531 static int
2532check_changedtick(arg)
2533 char_u *arg;
2534{
2535 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2536 {
2537 EMSG2(_(e_readonlyvar), arg);
2538 return TRUE;
2539 }
2540 return FALSE;
2541}
2542
2543/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Get an lval: variable, Dict item or List item that can be assigned a value
2545 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2546 * "name.key", "name.key[expr]" etc.
2547 * Indexing only works if "name" is an existing List or Dictionary.
2548 * "name" points to the start of the name.
2549 * If "rettv" is not NULL it points to the value to be assigned.
2550 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2551 * wrong; must end in space or cmd separator.
2552 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 * flags:
2554 * GLV_QUIET: do not give error messages
2555 * GLV_NO_AUTOLOAD: do not use script autoloading
2556 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002558 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 */
2561 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 typval_T *rettv;
2565 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 int unlet;
2567 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002569 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 char_u *expr_start, *expr_end;
2573 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 dictitem_T *v;
2575 typval_T var1;
2576 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002582 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586
2587 if (skip)
2588 {
2589 /* When skipping just find the end of the name. */
2590 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002591 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 }
2593
2594 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002595 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (expr_start != NULL)
2597 {
2598 /* Don't expand the name when we already know there is an error. */
2599 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2600 && *p != '[' && *p != '.')
2601 {
2602 EMSG(_(e_trailing));
2603 return NULL;
2604 }
2605
2606 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2607 if (lp->ll_exp_name == NULL)
2608 {
2609 /* Report an invalid expression in braces, unless the
2610 * expression evaluation has been cancelled due to an
2611 * aborting error, an interrupt, or an exception. */
2612 if (!aborting() && !quiet)
2613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002614 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 EMSG2(_(e_invarg2), name);
2616 return NULL;
2617 }
2618 }
2619 lp->ll_name = lp->ll_exp_name;
2620 }
2621 else
2622 lp->ll_name = name;
2623
2624 /* Without [idx] or .key we are done. */
2625 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2626 return p;
2627
2628 cc = *p;
2629 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (v == NULL && !quiet)
2632 EMSG2(_(e_undefvar), lp->ll_name);
2633 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 if (v == NULL)
2635 return NULL;
2636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /*
2638 * Loop until no more [idx] or .key is following.
2639 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2644 && !(lp->ll_tv->v_type == VAR_DICT
2645 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!quiet)
2648 EMSG(_("E689: Can only index a List or Dictionary"));
2649 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E708: [:] must come last"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 len = -1;
2659 if (*p == '.')
2660 {
2661 key = p + 1;
2662 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2663 ;
2664 if (len == 0)
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_(e_emptykey));
2668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = key + len;
2671 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 else
2673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (*p == ':')
2677 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 empty1 = FALSE;
2681 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var1) == NULL)
2684 {
2685 /* not a number or string */
2686 clear_tv(&var1);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
2691 /* Optionally get the second index [ :expr]. */
2692 if (*p == ':')
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (rettv != NULL && (rettv->v_type != VAR_LIST
2703 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (!empty1)
2708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711 p = skipwhite(p + 1);
2712 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002723 if (get_tv_string_chk(&var2) == NULL)
2724 {
2725 /* not a number or string */
2726 if (!empty1)
2727 clear_tv(&var1);
2728 clear_tv(&var2);
2729 return NULL;
2730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (!quiet)
2740 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (!empty1)
2742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
2748 /* Skip to past ']'. */
2749 ++p;
2750 }
2751
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
2754 if (len == -1)
2755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (*key == NUL)
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (!quiet)
2761 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002766 lp->ll_list = NULL;
2767 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002768 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 /* When assigning to a scope dictionary check that a function and
2771 * variable name is valid (only variable name unless it is l: or
2772 * g: dictionary). Disallow overwriting a builtin function. */
2773 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 int prevval;
2776 int wrong;
2777
2778 if (len != -1)
2779 {
2780 prevval = key[len];
2781 key[len] = NUL;
2782 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002783 else
2784 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2786 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 || !valid_varname(key);
2789 if (len != -1)
2790 key[len] = prevval;
2791 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 return NULL;
2793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 /* Can't add "v:" variable. */
2798 if (lp->ll_dict == &vimvardict)
2799 {
2800 EMSG2(_(e_illvar), name);
2801 return NULL;
2802 }
2803
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002804 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002808 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 if (len == -1)
2810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 }
2813 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 p = NULL;
2821 break;
2822 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002824 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 return NULL;
2826
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
2831 else
2832 {
2833 /*
2834 * Get the number and item for the only or first index of the List.
2835 */
2836 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 else
2839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002840 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 clear_tv(&var1);
2842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_list = lp->ll_tv->vval.v_list;
2845 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2846 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002848 if (lp->ll_n1 < 0)
2849 {
2850 lp->ll_n1 = 0;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 }
2853 }
2854 if (lp->ll_li == NULL)
2855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862
2863 /*
2864 * May need to find the item or absolute index for the second
2865 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 * When no index given: "lp->ll_empty2" is TRUE.
2867 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 {
2878 if (!quiet)
2879 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2886 if (lp->ll_n1 < 0)
2887 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2888 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 {
2890 if (!quiet)
2891 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return p;
2901}
2902
2903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
2907clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909{
2910 vim_free(lp->ll_exp_name);
2911 vim_free(lp->ll_newkey);
2912}
2913
2914/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926{
2927 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 listitem_T *ri;
2929 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930
2931 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 cc = *endp;
2936 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002941 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002942 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002943 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002946 if ((di == NULL
2947 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2948 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2949 FALSE)))
2950 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 set_var(lp->ll_name, &tv, FALSE);
2952 clear_tv(&tv);
2953 }
2954 }
2955 else
2956 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 else if (tv_check_lock(lp->ll_newkey == NULL
2961 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002962 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 else if (lp->ll_range)
2965 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966 listitem_T *ll_li = lp->ll_li;
2967 int ll_n1 = lp->ll_n1;
2968
2969 /*
2970 * Check whether any of the list items is locked
2971 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002972 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 return;
2976 ri = ri->li_next;
2977 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2978 break;
2979 ll_li = ll_li->li_next;
2980 ++ll_n1;
2981 }
2982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 /*
2984 * Assign the List values to the list items.
2985 */
2986 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 if (op != NULL && *op != '=')
2989 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2990 else
2991 {
2992 clear_tv(&lp->ll_li->li_tv);
2993 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 ri = ri->li_next;
2996 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2997 break;
2998 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003001 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 ri = NULL;
3004 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 lp->ll_li = lp->ll_li->li_next;
3008 ++lp->ll_n1;
3009 }
3010 if (ri != NULL)
3011 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 else if (lp->ll_empty2
3013 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 : lp->ll_n1 != lp->ll_n2)
3015 EMSG(_("E711: List value has not enough items"));
3016 }
3017 else
3018 {
3019 /*
3020 * Assign to a List or Dictionary item.
3021 */
3022 if (lp->ll_newkey != NULL)
3023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (op != NULL && *op != '=')
3025 {
3026 EMSG2(_(e_letwrong), op);
3027 return;
3028 }
3029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003031 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 if (di == NULL)
3033 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3035 {
3036 vim_free(di);
3037 return;
3038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003040 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 else if (op != NULL && *op != '=')
3042 {
3043 tv_op(lp->ll_tv, rettv, op);
3044 return;
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 /*
3050 * Assign the value to the variable or list item.
3051 */
3052 if (copy)
3053 copy_tv(rettv, lp->ll_tv);
3054 else
3055 {
3056 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003057 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 }
3060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061}
3062
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3065 * Returns OK or FAIL.
3066 */
3067 static int
3068tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 typval_T *tv1;
3070 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 char_u *op;
3072{
3073 long n;
3074 char_u numbuf[NUMBUFLEN];
3075 char_u *s;
3076
3077 /* Can't do anything with a Funcref or a Dict on the right. */
3078 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3079 {
3080 switch (tv1->v_type)
3081 {
3082 case VAR_DICT:
3083 case VAR_FUNC:
3084 break;
3085
3086 case VAR_LIST:
3087 if (*op != '+' || tv2->v_type != VAR_LIST)
3088 break;
3089 /* List += List */
3090 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3091 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3092 return OK;
3093
3094 case VAR_NUMBER:
3095 case VAR_STRING:
3096 if (tv2->v_type == VAR_LIST)
3097 break;
3098 if (*op == '+' || *op == '-')
3099 {
3100 /* nr += nr or nr -= nr*/
3101 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#ifdef FEAT_FLOAT
3103 if (tv2->v_type == VAR_FLOAT)
3104 {
3105 float_T f = n;
3106
3107 if (*op == '+')
3108 f += tv2->vval.v_float;
3109 else
3110 f -= tv2->vval.v_float;
3111 clear_tv(tv1);
3112 tv1->v_type = VAR_FLOAT;
3113 tv1->vval.v_float = f;
3114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#endif
3117 {
3118 if (*op == '+')
3119 n += get_tv_number(tv2);
3120 else
3121 n -= get_tv_number(tv2);
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_NUMBER;
3124 tv1->vval.v_number = n;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 }
3127 else
3128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 if (tv2->v_type == VAR_FLOAT)
3130 break;
3131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 /* str .= str */
3133 s = get_tv_string(tv1);
3134 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_STRING;
3137 tv1->vval.v_string = s;
3138 }
3139 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140
3141#ifdef FEAT_FLOAT
3142 case VAR_FLOAT:
3143 {
3144 float_T f;
3145
3146 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3147 && tv2->v_type != VAR_NUMBER
3148 && tv2->v_type != VAR_STRING))
3149 break;
3150 if (tv2->v_type == VAR_FLOAT)
3151 f = tv2->vval.v_float;
3152 else
3153 f = get_tv_number(tv2);
3154 if (*op == '+')
3155 tv1->vval.v_float += f;
3156 else
3157 tv1->vval.v_float -= f;
3158 }
3159 return OK;
3160#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003161 }
3162 }
3163
3164 EMSG2(_(e_letwrong), op);
3165 return FAIL;
3166}
3167
3168/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * Add a watcher to a list.
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 list_T *l;
3174 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175{
3176 lw->lw_next = l->lv_watch;
3177 l->lv_watch = lw;
3178}
3179
3180/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003181 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 * No warning when it isn't found...
3183 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003184 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 list_T *l;
3187 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190
3191 lwp = &l->lv_watch;
3192 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3193 {
3194 if (lw == lwrem)
3195 {
3196 *lwp = lw->lw_next;
3197 break;
3198 }
3199 lwp = &lw->lw_next;
3200 }
3201}
3202
3203/*
3204 * Just before removing an item from a list: advance watchers to the next
3205 * item.
3206 */
3207 static void
3208list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003209 list_T *l;
3210 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213
3214 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3215 if (lw->lw_item == item)
3216 lw->lw_item = item->li_next;
3217}
3218
3219/*
3220 * Evaluate the expression used in a ":for var in expr" command.
3221 * "arg" points to "var".
3222 * Set "*errp" to TRUE for an error, FALSE otherwise;
3223 * Return a pointer that holds the info. Null when there is an error.
3224 */
3225 void *
3226eval_for_line(arg, errp, nextcmdp, skip)
3227 char_u *arg;
3228 int *errp;
3229 char_u **nextcmdp;
3230 int skip;
3231{
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 typval_T tv;
3235 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
3237 *errp = TRUE; /* default: there is an error */
3238
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240 if (fi == NULL)
3241 return NULL;
3242
3243 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3244 if (expr == NULL)
3245 return fi;
3246
3247 expr = skipwhite(expr);
3248 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3249 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003250 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 return fi;
3252 }
3253
3254 if (skip)
3255 ++emsg_skip;
3256 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3257 {
3258 *errp = FALSE;
3259 if (!skip)
3260 {
3261 l = tv.vval.v_list;
3262 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003263 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 clear_tv(&tv);
3266 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 else
3268 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003269 /* No need to increment the refcount, it's already set for the
3270 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 fi->fi_list = l;
3272 list_add_watch(l, &fi->fi_lw);
3273 fi->fi_lw.lw_item = l->lv_first;
3274 }
3275 }
3276 }
3277 if (skip)
3278 --emsg_skip;
3279
3280 return fi;
3281}
3282
3283/*
3284 * Use the first item in a ":for" list. Advance to the next.
3285 * Assign the values to the variable (list). "arg" points to the first one.
3286 * Return TRUE when a valid item was found, FALSE when at end of list or
3287 * something wrong.
3288 */
3289 int
3290next_for_item(fi_void, arg)
3291 void *fi_void;
3292 char_u *arg;
3293{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003294 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 item = fi->fi_lw.lw_item;
3299 if (item == NULL)
3300 result = FALSE;
3301 else
3302 {
3303 fi->fi_lw.lw_item = item->li_next;
3304 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3305 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3306 }
3307 return result;
3308}
3309
3310/*
3311 * Free the structure used to store info used by ":for".
3312 */
3313 void
3314free_for_info(fi_void)
3315 void *fi_void;
3316{
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003319 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 list_unref(fi->fi_list);
3323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 vim_free(fi);
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328
3329 void
3330set_context_for_expression(xp, arg, cmdidx)
3331 expand_T *xp;
3332 char_u *arg;
3333 cmdidx_T cmdidx;
3334{
3335 int got_eq = FALSE;
3336 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (cmdidx == CMD_let)
3340 {
3341 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003342 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 {
3344 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (vim_iswhite(*p))
3350 break;
3351 }
3352 return;
3353 }
3354 }
3355 else
3356 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3357 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 while ((xp->xp_pattern = vim_strpbrk(arg,
3359 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3360 {
3361 c = *xp->xp_pattern;
3362 if (c == '&')
3363 {
3364 c = xp->xp_pattern[1];
3365 if (c == '&')
3366 {
3367 ++xp->xp_pattern;
3368 xp->xp_context = cmdidx != CMD_let || got_eq
3369 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3370 }
3371 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3375 xp->xp_pattern += 2;
3376
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else if (c == '$')
3380 {
3381 /* environment variable */
3382 xp->xp_context = EXPAND_ENV_VARS;
3383 }
3384 else if (c == '=')
3385 {
3386 got_eq = TRUE;
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && xp->xp_context == EXPAND_FUNCTIONS
3391 && vim_strchr(xp->xp_pattern, '(') == NULL)
3392 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395 }
3396 else if (cmdidx != CMD_let || got_eq)
3397 {
3398 if (c == '"') /* string */
3399 {
3400 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3401 if (c == '\\' && xp->xp_pattern[1] != NUL)
3402 ++xp->xp_pattern;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '\'') /* literal string */
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3409 /* skip */ ;
3410 xp->xp_context = EXPAND_NOTHING;
3411 }
3412 else if (c == '|')
3413 {
3414 if (xp->xp_pattern[1] == '|')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
3420 xp->xp_context = EXPAND_COMMANDS;
3421 }
3422 else
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003426 /* Doesn't look like something valid, expand as an expression
3427 * anyway. */
3428 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 arg = xp->xp_pattern;
3430 if (*arg != NUL)
3431 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3432 /* skip */ ;
3433 }
3434 xp->xp_pattern = arg;
3435}
3436
3437#endif /* FEAT_CMDL_COMPL */
3438
3439/*
3440 * ":1,25call func(arg1, arg2)" function call.
3441 */
3442 void
3443ex_call(eap)
3444 exarg_T *eap;
3445{
3446 char_u *arg = eap->arg;
3447 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003449 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 linenr_T lnum;
3453 int doesrange;
3454 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003457 if (eap->skip)
3458 {
3459 /* trans_function_name() doesn't work well when skipping, use eval0()
3460 * instead to skip to any following command, e.g. for:
3461 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003462 ++emsg_skip;
3463 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3464 clear_tv(&rettv);
3465 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003466 return;
3467 }
3468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003469 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003470 if (fudi.fd_newkey != NULL)
3471 {
3472 /* Still need to give an error message for missing key. */
3473 EMSG2(_(e_dictkey), fudi.fd_newkey);
3474 vim_free(fudi.fd_newkey);
3475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003476 if (tofree == NULL)
3477 return;
3478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 /* Increase refcount on dictionary, it could get deleted when evaluating
3480 * the arguments. */
3481 if (fudi.fd_dict != NULL)
3482 ++fudi.fd_dict->dv_refcount;
3483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003484 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003485 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003486 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar532c7802005-01-27 14:44:31 +00003488 /* Skip white space to allow ":call func ()". Not good, but required for
3489 * backward compatibility. */
3490 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 if (*startarg != '(')
3494 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003495 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 goto end;
3497 }
3498
3499 /*
3500 * When skipping, evaluate the function once, to find the end of the
3501 * arguments.
3502 * When the function takes a range, this is discovered after the first
3503 * call, and the loop is broken.
3504 */
3505 if (eap->skip)
3506 {
3507 ++emsg_skip;
3508 lnum = eap->line2; /* do it once, also with an invalid range */
3509 }
3510 else
3511 lnum = eap->line1;
3512 for ( ; lnum <= eap->line2; ++lnum)
3513 {
3514 if (!eap->skip && eap->addr_count > 0)
3515 {
3516 curwin->w_cursor.lnum = lnum;
3517 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003518#ifdef FEAT_VIRTUALEDIT
3519 curwin->w_cursor.coladd = 0;
3520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003524 eap->line1, eap->line2, &doesrange,
3525 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 failed = TRUE;
3528 break;
3529 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003530
3531 /* Handle a function returning a Funcref, Dictionary or List. */
3532 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3533 {
3534 failed = TRUE;
3535 break;
3536 }
3537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (doesrange || eap->skip)
3540 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003543 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (aborting())
3547 break;
3548 }
3549 if (eap->skip)
3550 --emsg_skip;
3551
3552 if (!failed)
3553 {
3554 /* Check for trailing illegal characters and a following command. */
3555 if (!ends_excmd(*arg))
3556 {
3557 emsg_severe = TRUE;
3558 EMSG(_(e_trailing));
3559 }
3560 else
3561 eap->nextcmd = check_nextcmd(arg);
3562 }
3563
3564end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003565 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003566 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567}
3568
3569/*
3570 * ":unlet[!] var1 ... " command.
3571 */
3572 void
3573ex_unlet(eap)
3574 exarg_T *eap;
3575{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 ex_unletlock(eap, eap->arg, 0);
3577}
3578
3579/*
3580 * ":lockvar" and ":unlockvar" commands
3581 */
3582 void
3583ex_lockvar(eap)
3584 exarg_T *eap;
3585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 int deep = 2;
3588
3589 if (eap->forceit)
3590 deep = -1;
3591 else if (vim_isdigit(*arg))
3592 {
3593 deep = getdigits(&arg);
3594 arg = skipwhite(arg);
3595 }
3596
3597 ex_unletlock(eap, arg, deep);
3598}
3599
3600/*
3601 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3602 */
3603 static void
3604ex_unletlock(eap, argstart, deep)
3605 exarg_T *eap;
3606 char_u *argstart;
3607 int deep;
3608{
3609 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 do
3615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003617 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003618 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lv.ll_name == NULL)
3620 error = TRUE; /* error but continue parsing */
3621 if (name_end == NULL || (!vim_iswhite(*name_end)
3622 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (name_end != NULL)
3625 {
3626 emsg_severe = TRUE;
3627 EMSG(_(e_trailing));
3628 }
3629 if (!(eap->skip || error))
3630 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
3632 }
3633
3634 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 {
3636 if (eap->cmdidx == CMD_unlet)
3637 {
3638 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3639 error = TRUE;
3640 }
3641 else
3642 {
3643 if (do_lock_var(&lv, name_end, deep,
3644 eap->cmdidx == CMD_lockvar) == FAIL)
3645 error = TRUE;
3646 }
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 if (!eap->skip)
3650 clear_lval(&lv);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 arg = skipwhite(name_end);
3653 } while (!ends_excmd(*arg));
3654
3655 eap->nextcmd = check_nextcmd(arg);
3656}
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 int forceit;
3663{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 int ret = OK;
3665 int cc;
3666
3667 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 cc = *name_end;
3670 *name_end = NUL;
3671
3672 /* Normal name or expanded name. */
3673 if (check_changedtick(lp->ll_name))
3674 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003679 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 else if (lp->ll_range)
3685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 listitem_T *ll_li = lp->ll_li;
3688 int ll_n1 = lp->ll_n1;
3689
3690 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3691 {
3692 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003694 return FAIL;
3695 ll_li = li;
3696 ++ll_n1;
3697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698
3699 /* Delete a range of List items. */
3700 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 li = lp->ll_li->li_next;
3703 listitem_remove(lp->ll_list, lp->ll_li);
3704 lp->ll_li = li;
3705 ++lp->ll_n1;
3706 }
3707 }
3708 else
3709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a List item. */
3712 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003715 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 }
3717
3718 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719}
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721/*
3722 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else
3745 {
3746 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3747 d = di->di_tv.vval.v_dict;
3748 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 hi = hash_find(ht, varname);
3750 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003753 if (var_check_fixed(di->di_flags, name, FALSE)
3754 || var_check_ro(di->di_flags, name, FALSE)
3755 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 return FAIL;
3757 delete_var(ht, hi);
3758 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761 if (forceit)
3762 return OK;
3763 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return FAIL;
3765}
3766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767/*
3768 * Lock or unlock variable indicated by "lp".
3769 * "deep" is the levels to go (-1 for unlimited);
3770 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3771 */
3772 static int
3773do_lock_var(lp, name_end, deep, lock)
3774 lval_T *lp;
3775 char_u *name_end;
3776 int deep;
3777 int lock;
3778{
3779 int ret = OK;
3780 int cc;
3781 dictitem_T *di;
3782
3783 if (deep == 0) /* nothing to do */
3784 return OK;
3785
3786 if (lp->ll_tv == NULL)
3787 {
3788 cc = *name_end;
3789 *name_end = NUL;
3790
3791 /* Normal name or expanded name. */
3792 if (check_changedtick(lp->ll_name))
3793 ret = FAIL;
3794 else
3795 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003796 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 if (di == NULL)
3798 ret = FAIL;
3799 else
3800 {
3801 if (lock)
3802 di->di_flags |= DI_FLAGS_LOCK;
3803 else
3804 di->di_flags &= ~DI_FLAGS_LOCK;
3805 item_lock(&di->di_tv, deep, lock);
3806 }
3807 }
3808 *name_end = cc;
3809 }
3810 else if (lp->ll_range)
3811 {
3812 listitem_T *li = lp->ll_li;
3813
3814 /* (un)lock a range of List items. */
3815 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3816 {
3817 item_lock(&li->li_tv, deep, lock);
3818 li = li->li_next;
3819 ++lp->ll_n1;
3820 }
3821 }
3822 else if (lp->ll_list != NULL)
3823 /* (un)lock a List item. */
3824 item_lock(&lp->ll_li->li_tv, deep, lock);
3825 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003826 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827 item_lock(&lp->ll_di->di_tv, deep, lock);
3828
3829 return ret;
3830}
3831
3832/*
3833 * Lock or unlock an item. "deep" is nr of levels to go.
3834 */
3835 static void
3836item_lock(tv, deep, lock)
3837 typval_T *tv;
3838 int deep;
3839 int lock;
3840{
3841 static int recurse = 0;
3842 list_T *l;
3843 listitem_T *li;
3844 dict_T *d;
3845 hashitem_T *hi;
3846 int todo;
3847
3848 if (recurse >= DICT_MAXNEST)
3849 {
3850 EMSG(_("E743: variable nested too deep for (un)lock"));
3851 return;
3852 }
3853 if (deep == 0)
3854 return;
3855 ++recurse;
3856
3857 /* lock/unlock the item itself */
3858 if (lock)
3859 tv->v_lock |= VAR_LOCKED;
3860 else
3861 tv->v_lock &= ~VAR_LOCKED;
3862
3863 switch (tv->v_type)
3864 {
3865 case VAR_LIST:
3866 if ((l = tv->vval.v_list) != NULL)
3867 {
3868 if (lock)
3869 l->lv_lock |= VAR_LOCKED;
3870 else
3871 l->lv_lock &= ~VAR_LOCKED;
3872 if (deep < 0 || deep > 1)
3873 /* recursive: lock/unlock the items the List contains */
3874 for (li = l->lv_first; li != NULL; li = li->li_next)
3875 item_lock(&li->li_tv, deep - 1, lock);
3876 }
3877 break;
3878 case VAR_DICT:
3879 if ((d = tv->vval.v_dict) != NULL)
3880 {
3881 if (lock)
3882 d->dv_lock |= VAR_LOCKED;
3883 else
3884 d->dv_lock &= ~VAR_LOCKED;
3885 if (deep < 0 || deep > 1)
3886 {
3887 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003888 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3890 {
3891 if (!HASHITEM_EMPTY(hi))
3892 {
3893 --todo;
3894 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3895 }
3896 }
3897 }
3898 }
3899 }
3900 --recurse;
3901}
3902
Bram Moolenaara40058a2005-07-11 22:42:07 +00003903/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003904 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3905 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003906 */
3907 static int
3908tv_islocked(tv)
3909 typval_T *tv;
3910{
3911 return (tv->v_lock & VAR_LOCKED)
3912 || (tv->v_type == VAR_LIST
3913 && tv->vval.v_list != NULL
3914 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3915 || (tv->v_type == VAR_DICT
3916 && tv->vval.v_dict != NULL
3917 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3921/*
3922 * Delete all "menutrans_" variables.
3923 */
3924 void
3925del_menutrans_vars()
3926{
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 {
3934 if (!HASHITEM_EMPTY(hi))
3935 {
3936 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3938 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 }
3940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942}
3943#endif
3944
3945#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3946
3947/*
3948 * Local string buffer for the next two functions to store a variable name
3949 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3950 * get_user_var_name().
3951 */
3952
3953static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3954
3955static char_u *varnamebuf = NULL;
3956static int varnamebuflen = 0;
3957
3958/*
3959 * Function to concatenate a prefix and a variable name.
3960 */
3961 static char_u *
3962cat_prefix_varname(prefix, name)
3963 int prefix;
3964 char_u *name;
3965{
3966 int len;
3967
3968 len = (int)STRLEN(name) + 3;
3969 if (len > varnamebuflen)
3970 {
3971 vim_free(varnamebuf);
3972 len += 10; /* some additional space */
3973 varnamebuf = alloc(len);
3974 if (varnamebuf == NULL)
3975 {
3976 varnamebuflen = 0;
3977 return NULL;
3978 }
3979 varnamebuflen = len;
3980 }
3981 *varnamebuf = prefix;
3982 varnamebuf[1] = ':';
3983 STRCPY(varnamebuf + 2, name);
3984 return varnamebuf;
3985}
3986
3987/*
3988 * Function given to ExpandGeneric() to obtain the list of user defined
3989 * (global/buffer/window/built-in) variable names.
3990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u *
3992get_user_var_name(xp, idx)
3993 expand_T *xp;
3994 int idx;
3995{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003996 static long_u gdone;
3997 static long_u bdone;
3998 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003999#ifdef FEAT_WINDOWS
4000 static long_u tdone;
4001#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004002 static int vidx;
4003 static hashitem_T *hi;
4004 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 tdone = 0;
4011#endif
4012 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004013
4014 /* Global variables */
4015 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004017 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004019 else
4020 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 while (HASHITEM_EMPTY(hi))
4022 ++hi;
4023 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4024 return cat_prefix_varname('g', hi->hi_key);
4025 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027
4028 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004029 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004034 else
4035 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 while (HASHITEM_EMPTY(hi))
4037 ++hi;
4038 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return (char_u *)"b:changedtick";
4044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045
4046 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004047 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004050 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004052 else
4053 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 while (HASHITEM_EMPTY(hi))
4055 ++hi;
4056 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004058
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004061 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062 if (tdone < ht->ht_used)
4063 {
4064 if (tdone++ == 0)
4065 hi = ht->ht_array;
4066 else
4067 ++hi;
4068 while (HASHITEM_EMPTY(hi))
4069 ++hi;
4070 return cat_prefix_varname('t', hi->hi_key);
4071 }
4072#endif
4073
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 /* v: variables */
4075 if (vidx < VV_LEN)
4076 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 vim_free(varnamebuf);
4079 varnamebuf = NULL;
4080 varnamebuflen = 0;
4081 return NULL;
4082}
4083
4084#endif /* FEAT_CMDL_COMPL */
4085
4086/*
4087 * types for expressions.
4088 */
4089typedef enum
4090{
4091 TYPE_UNKNOWN = 0
4092 , TYPE_EQUAL /* == */
4093 , TYPE_NEQUAL /* != */
4094 , TYPE_GREATER /* > */
4095 , TYPE_GEQUAL /* >= */
4096 , TYPE_SMALLER /* < */
4097 , TYPE_SEQUAL /* <= */
4098 , TYPE_MATCH /* =~ */
4099 , TYPE_NOMATCH /* !~ */
4100} exptype_T;
4101
4102/*
4103 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4106 */
4107
4108/*
4109 * Handle zero level expression.
4110 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004112 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 char_u **nextcmd;
4120 int evaluate;
4121{
4122 int ret;
4123 char_u *p;
4124
4125 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (ret == FAIL || !ends_excmd(*p))
4128 {
4129 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /*
4132 * Report the invalid expression unless the expression evaluation has
4133 * been cancelled due to an aborting error, an interrupt, or an
4134 * exception.
4135 */
4136 if (!aborting())
4137 EMSG2(_(e_invexpr2), arg);
4138 ret = FAIL;
4139 }
4140 if (nextcmd != NULL)
4141 *nextcmd = check_nextcmd(p);
4142
4143 return ret;
4144}
4145
4146/*
4147 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004148 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 *
4150 * "arg" must point to the first non-white of the expression.
4151 * "arg" is advanced to the next non-white after the recognized expression.
4152 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004153 * Note: "rettv.v_lock" is not set.
4154 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Return OK or FAIL.
4156 */
4157 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int evaluate;
4162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 long result;
4238 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * Get the first variable.
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
4246
4247 /*
4248 * Repeat until there is no following "||".
4249 */
4250 first = TRUE;
4251 result = FALSE;
4252 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4253 {
4254 if (evaluate && first)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 first = FALSE;
4262 }
4263
4264 /*
4265 * Get the second variable.
4266 */
4267 *arg = skipwhite(*arg + 2);
4268 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4269 return FAIL;
4270
4271 /*
4272 * Compute the result.
4273 */
4274 if (evaluate && !result)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (evaluate)
4283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle second level expression:
4294 * expr3 && expr3 && expr3 logical AND
4295 *
4296 * "arg" must point to the first non-white of the expression.
4297 * "arg" is advanced to the next non-white after the recognized expression.
4298 *
4299 * Return OK or FAIL.
4300 */
4301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 int evaluate;
4306{
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 long result;
4309 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 /*
4319 * Repeat until there is no following "&&".
4320 */
4321 first = TRUE;
4322 result = TRUE;
4323 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4324 {
4325 if (evaluate && first)
4326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (error)
4331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 first = FALSE;
4333 }
4334
4335 /*
4336 * Get the second variable.
4337 */
4338 *arg = skipwhite(*arg + 2);
4339 if (eval4(arg, &var2, evaluate && result) == FAIL)
4340 return FAIL;
4341
4342 /*
4343 * Compute the result.
4344 */
4345 if (evaluate && result)
4346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (error)
4351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 if (evaluate)
4354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 rettv->v_type = VAR_NUMBER;
4356 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358 }
4359
4360 return OK;
4361}
4362
4363/*
4364 * Handle third level expression:
4365 * var1 == var2
4366 * var1 =~ var2
4367 * var1 != var2
4368 * var1 !~ var2
4369 * var1 > var2
4370 * var1 >= var2
4371 * var1 < var2
4372 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 * var1 is var2
4374 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int evaluate;
4386{
Bram Moolenaar33570922005-01-25 22:26:29 +00004387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 char_u *p;
4389 int i;
4390 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int len = 2;
4393 long n1, n2;
4394 char_u *s1, *s2;
4395 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4396 regmatch_T regmatch;
4397 int ic;
4398 char_u *save_cpo;
4399
4400 /*
4401 * Get the first variable.
4402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FAIL;
4405
4406 p = *arg;
4407 switch (p[0])
4408 {
4409 case '=': if (p[1] == '=')
4410 type = TYPE_EQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_MATCH;
4413 break;
4414 case '!': if (p[1] == '=')
4415 type = TYPE_NEQUAL;
4416 else if (p[1] == '~')
4417 type = TYPE_NOMATCH;
4418 break;
4419 case '>': if (p[1] != '=')
4420 {
4421 type = TYPE_GREATER;
4422 len = 1;
4423 }
4424 else
4425 type = TYPE_GEQUAL;
4426 break;
4427 case '<': if (p[1] != '=')
4428 {
4429 type = TYPE_SMALLER;
4430 len = 1;
4431 }
4432 else
4433 type = TYPE_SEQUAL;
4434 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 case 'i': if (p[1] == 's')
4436 {
4437 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4438 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004439 i = p[len];
4440 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 {
4442 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4443 type_is = TRUE;
4444 }
4445 }
4446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
4449 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004450 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
4452 if (type != TYPE_UNKNOWN)
4453 {
4454 /* extra question mark appended: ignore case */
4455 if (p[len] == '?')
4456 {
4457 ic = TRUE;
4458 ++len;
4459 }
4460 /* extra '#' appended: match case */
4461 else if (p[len] == '#')
4462 {
4463 ic = FALSE;
4464 ++len;
4465 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
4468 ic = p_ic;
4469
4470 /*
4471 * Get the second variable.
4472 */
4473 *arg = skipwhite(p + len);
4474 if (eval5(arg, &var2, evaluate) == FAIL)
4475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FAIL;
4478 }
4479
4480 if (evaluate)
4481 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004482 if (type_is && rettv->v_type != var2.v_type)
4483 {
4484 /* For "is" a different type always means FALSE, for "notis"
4485 * it means TRUE. */
4486 n1 = (type == TYPE_NEQUAL);
4487 }
4488 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4489 {
4490 if (type_is)
4491 {
4492 n1 = (rettv->v_type == var2.v_type
4493 && rettv->vval.v_list == var2.vval.v_list);
4494 if (type == TYPE_NEQUAL)
4495 n1 = !n1;
4496 }
4497 else if (rettv->v_type != var2.v_type
4498 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4499 {
4500 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004501 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004503 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 clear_tv(rettv);
4505 clear_tv(&var2);
4506 return FAIL;
4507 }
4508 else
4509 {
4510 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004511 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4512 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 }
4517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4519 {
4520 if (type_is)
4521 {
4522 n1 = (rettv->v_type == var2.v_type
4523 && rettv->vval.v_dict == var2.vval.v_dict);
4524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 else if (rettv->v_type != var2.v_type
4528 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4529 {
4530 if (rettv->v_type != var2.v_type)
4531 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4532 else
4533 EMSG(_("E736: Invalid operation for Dictionary"));
4534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 else
4539 {
4540 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004541 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4542 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004543 if (type == TYPE_NEQUAL)
4544 n1 = !n1;
4545 }
4546 }
4547
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4549 {
4550 if (rettv->v_type != var2.v_type
4551 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4552 {
4553 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004556 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 clear_tv(rettv);
4558 clear_tv(&var2);
4559 return FAIL;
4560 }
4561 else
4562 {
4563 /* Compare two Funcrefs for being equal or unequal. */
4564 if (rettv->vval.v_string == NULL
4565 || var2.vval.v_string == NULL)
4566 n1 = FALSE;
4567 else
4568 n1 = STRCMP(rettv->vval.v_string,
4569 var2.vval.v_string) == 0;
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 }
4574
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004575#ifdef FEAT_FLOAT
4576 /*
4577 * If one of the two variables is a float, compare as a float.
4578 * When using "=~" or "!~", always compare as string.
4579 */
4580 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4581 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4582 {
4583 float_T f1, f2;
4584
4585 if (rettv->v_type == VAR_FLOAT)
4586 f1 = rettv->vval.v_float;
4587 else
4588 f1 = get_tv_number(rettv);
4589 if (var2.v_type == VAR_FLOAT)
4590 f2 = var2.vval.v_float;
4591 else
4592 f2 = get_tv_number(&var2);
4593 n1 = FALSE;
4594 switch (type)
4595 {
4596 case TYPE_EQUAL: n1 = (f1 == f2); break;
4597 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4598 case TYPE_GREATER: n1 = (f1 > f2); break;
4599 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4600 case TYPE_SMALLER: n1 = (f1 < f2); break;
4601 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4602 case TYPE_UNKNOWN:
4603 case TYPE_MATCH:
4604 case TYPE_NOMATCH: break; /* avoid gcc warning */
4605 }
4606 }
4607#endif
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 /*
4610 * If one of the two variables is a number, compare as a number.
4611 * When using "=~" or "!~", always compare as string.
4612 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004613 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 n1 = get_tv_number(rettv);
4617 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 switch (type)
4619 {
4620 case TYPE_EQUAL: n1 = (n1 == n2); break;
4621 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4622 case TYPE_GREATER: n1 = (n1 > n2); break;
4623 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4624 case TYPE_SMALLER: n1 = (n1 < n2); break;
4625 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4626 case TYPE_UNKNOWN:
4627 case TYPE_MATCH:
4628 case TYPE_NOMATCH: break; /* avoid gcc warning */
4629 }
4630 }
4631 else
4632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 s1 = get_tv_string_buf(rettv, buf1);
4634 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4636 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4637 else
4638 i = 0;
4639 n1 = FALSE;
4640 switch (type)
4641 {
4642 case TYPE_EQUAL: n1 = (i == 0); break;
4643 case TYPE_NEQUAL: n1 = (i != 0); break;
4644 case TYPE_GREATER: n1 = (i > 0); break;
4645 case TYPE_GEQUAL: n1 = (i >= 0); break;
4646 case TYPE_SMALLER: n1 = (i < 0); break;
4647 case TYPE_SEQUAL: n1 = (i <= 0); break;
4648
4649 case TYPE_MATCH:
4650 case TYPE_NOMATCH:
4651 /* avoid 'l' flag in 'cpoptions' */
4652 save_cpo = p_cpo;
4653 p_cpo = (char_u *)"";
4654 regmatch.regprog = vim_regcomp(s2,
4655 RE_MAGIC + RE_STRING);
4656 regmatch.rm_ic = ic;
4657 if (regmatch.regprog != NULL)
4658 {
4659 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004660 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (type == TYPE_NOMATCH)
4662 n1 = !n1;
4663 }
4664 p_cpo = save_cpo;
4665 break;
4666
4667 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4668 }
4669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
4671 clear_tv(&var2);
4672 rettv->v_type = VAR_NUMBER;
4673 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 }
4676
4677 return OK;
4678}
4679
4680/*
4681 * Handle fourth level expression:
4682 * + number addition
4683 * - number subtraction
4684 * . string concatenation
4685 *
4686 * "arg" must point to the first non-white of the expression.
4687 * "arg" is advanced to the next non-white after the recognized expression.
4688 *
4689 * Return OK or FAIL.
4690 */
4691 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int evaluate;
4696{
Bram Moolenaar33570922005-01-25 22:26:29 +00004697 typval_T var2;
4698 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 int op;
4700 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 float_T f1 = 0, f2 = 0;
4703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u *s1, *s2;
4705 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4706 char_u *p;
4707
4708 /*
4709 * Get the first variable.
4710 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FAIL;
4713
4714 /*
4715 * Repeat computing, until no '+', '-' or '.' is following.
4716 */
4717 for (;;)
4718 {
4719 op = **arg;
4720 if (op != '+' && op != '-' && op != '.')
4721 break;
4722
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723 if ((op != '+' || rettv->v_type != VAR_LIST)
4724#ifdef FEAT_FLOAT
4725 && (op == '.' || rettv->v_type != VAR_FLOAT)
4726#endif
4727 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004728 {
4729 /* For "list + ...", an illegal use of the first operand as
4730 * a number cannot be determined before evaluating the 2nd
4731 * operand: if this is also a list, all is ok.
4732 * For "something . ...", "something - ..." or "non-list + ...",
4733 * we know that the first operand needs to be a string or number
4734 * without evaluating the 2nd operand. So check before to avoid
4735 * side effects after an error. */
4736 if (evaluate && get_tv_string_chk(rettv) == NULL)
4737 {
4738 clear_tv(rettv);
4739 return FAIL;
4740 }
4741 }
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /*
4744 * Get the second variable.
4745 */
4746 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004747 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return FAIL;
4751 }
4752
4753 if (evaluate)
4754 {
4755 /*
4756 * Compute the result.
4757 */
4758 if (op == '.')
4759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4761 s2 = get_tv_string_buf_chk(&var2, buf2);
4762 if (s2 == NULL) /* type error ? */
4763 {
4764 clear_tv(rettv);
4765 clear_tv(&var2);
4766 return FAIL;
4767 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004768 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
4770 rettv->v_type = VAR_STRING;
4771 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004773 else if (op == '+' && rettv->v_type == VAR_LIST
4774 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004775 {
4776 /* concatenate Lists */
4777 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4778 &var3) == FAIL)
4779 {
4780 clear_tv(rettv);
4781 clear_tv(&var2);
4782 return FAIL;
4783 }
4784 clear_tv(rettv);
4785 *rettv = var3;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 else
4788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 int error = FALSE;
4790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791#ifdef FEAT_FLOAT
4792 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 f1 = rettv->vval.v_float;
4795 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797 else
4798#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 n1 = get_tv_number_chk(rettv, &error);
4801 if (error)
4802 {
4803 /* This can only happen for "list + non-list". For
4804 * "non-list + ..." or "something - ...", we returned
4805 * before evaluating the 2nd operand. */
4806 clear_tv(rettv);
4807 return FAIL;
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 f1 = n1;
4812#endif
4813 }
4814#ifdef FEAT_FLOAT
4815 if (var2.v_type == VAR_FLOAT)
4816 {
4817 f2 = var2.vval.v_float;
4818 n2 = 0;
4819 }
4820 else
4821#endif
4822 {
4823 n2 = get_tv_number_chk(&var2, &error);
4824 if (error)
4825 {
4826 clear_tv(rettv);
4827 clear_tv(&var2);
4828 return FAIL;
4829 }
4830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 f2 = n2;
4833#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836
4837#ifdef FEAT_FLOAT
4838 /* If there is a float on either side the result is a float. */
4839 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4840 {
4841 if (op == '+')
4842 f1 = f1 + f2;
4843 else
4844 f1 = f1 - f2;
4845 rettv->v_type = VAR_FLOAT;
4846 rettv->vval.v_float = f1;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004849#endif
4850 {
4851 if (op == '+')
4852 n1 = n1 + n2;
4853 else
4854 n1 = n1 - n2;
4855 rettv->v_type = VAR_NUMBER;
4856 rettv->vval.v_number = n1;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 }
4862 return OK;
4863}
4864
4865/*
4866 * Handle fifth level expression:
4867 * * number multiplication
4868 * / number division
4869 * % number modulo
4870 *
4871 * "arg" must point to the first non-white of the expression.
4872 * "arg" is advanced to the next non-white after the recognized expression.
4873 *
4874 * Return OK or FAIL.
4875 */
4876 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004877eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int op;
4885 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886#ifdef FEAT_FLOAT
4887 int use_float = FALSE;
4888 float_T f1 = 0, f2;
4889#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
4892 /*
4893 * Get the first variable.
4894 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004895 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return FAIL;
4897
4898 /*
4899 * Repeat computing, until no '*', '/' or '%' is following.
4900 */
4901 for (;;)
4902 {
4903 op = **arg;
4904 if (op != '*' && op != '/' && op != '%')
4905 break;
4906
4907 if (evaluate)
4908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#ifdef FEAT_FLOAT
4910 if (rettv->v_type == VAR_FLOAT)
4911 {
4912 f1 = rettv->vval.v_float;
4913 use_float = TRUE;
4914 n1 = 0;
4915 }
4916 else
4917#endif
4918 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920 if (error)
4921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else
4924 n1 = 0;
4925
4926 /*
4927 * Get the second variable.
4928 */
4929 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004930 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return FAIL;
4932
4933 if (evaluate)
4934 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004935#ifdef FEAT_FLOAT
4936 if (var2.v_type == VAR_FLOAT)
4937 {
4938 if (!use_float)
4939 {
4940 f1 = n1;
4941 use_float = TRUE;
4942 }
4943 f2 = var2.vval.v_float;
4944 n2 = 0;
4945 }
4946 else
4947#endif
4948 {
4949 n2 = get_tv_number_chk(&var2, &error);
4950 clear_tv(&var2);
4951 if (error)
4952 return FAIL;
4953#ifdef FEAT_FLOAT
4954 if (use_float)
4955 f2 = n2;
4956#endif
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
4959 /*
4960 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963#ifdef FEAT_FLOAT
4964 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 if (op == '*')
4967 f1 = f1 * f2;
4968 else if (op == '/')
4969 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970# ifdef VMS
4971 /* VMS crashes on divide by zero, work around it */
4972 if (f2 == 0.0)
4973 {
4974 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004975 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004977 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004978 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004979 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980 }
4981 else
4982 f1 = f1 / f2;
4983# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 /* We rely on the floating point library to handle divide
4985 * by zero to result in "inf" and not a crash. */
4986 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004991 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 return FAIL;
4993 }
4994 rettv->v_type = VAR_FLOAT;
4995 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 if (op == '*')
5001 n1 = n1 * n2;
5002 else if (op == '/')
5003 {
5004 if (n2 == 0) /* give an error message? */
5005 {
5006 if (n1 == 0)
5007 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5008 else if (n1 < 0)
5009 n1 = -0x7fffffffL;
5010 else
5011 n1 = 0x7fffffffL;
5012 }
5013 else
5014 n1 = n1 / n2;
5015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 {
5018 if (n2 == 0) /* give an error message? */
5019 n1 = 0;
5020 else
5021 n1 = n1 % n2;
5022 }
5023 rettv->v_type = VAR_NUMBER;
5024 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 }
5028
5029 return OK;
5030}
5031
5032/*
5033 * Handle sixth level expression:
5034 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005035 * "string" string constant
5036 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 * &option-name option value
5038 * @r register contents
5039 * identifier variable value
5040 * function() function call
5041 * $VAR environment variable
5042 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005043 * [expr, expr] List
5044 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *
5046 * Also handle:
5047 * ! in front logical NOT
5048 * - in front unary minus
5049 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 * trailing [] subscript in String or List
5051 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 *
5053 * "arg" must point to the first non-white of the expression.
5054 * "arg" is advanced to the next non-white after the recognized expression.
5055 *
5056 * Return OK or FAIL.
5057 */
5058 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005059eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005063 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 long n;
5066 int len;
5067 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 char_u *start_leader, *end_leader;
5069 int ret = OK;
5070 char_u *alias;
5071
5072 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 /*
5079 * Skip '!' and '-' characters. They are handled later.
5080 */
5081 start_leader = *arg;
5082 while (**arg == '!' || **arg == '-' || **arg == '+')
5083 *arg = skipwhite(*arg + 1);
5084 end_leader = *arg;
5085
5086 switch (**arg)
5087 {
5088 /*
5089 * Number constant.
5090 */
5091 case '0':
5092 case '1':
5093 case '2':
5094 case '3':
5095 case '4':
5096 case '5':
5097 case '6':
5098 case '7':
5099 case '8':
5100 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 {
5102#ifdef FEAT_FLOAT
5103 char_u *p = skipdigits(*arg + 1);
5104 int get_float = FALSE;
5105
5106 /* We accept a float when the format matches
5107 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005108 * strict to avoid backwards compatibility problems.
5109 * Don't look for a float after the "." operator, so that
5110 * ":let vers = 1.2.3" doesn't fail. */
5111 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005113 get_float = TRUE;
5114 p = skipdigits(p + 2);
5115 if (*p == 'e' || *p == 'E')
5116 {
5117 ++p;
5118 if (*p == '-' || *p == '+')
5119 ++p;
5120 if (!vim_isdigit(*p))
5121 get_float = FALSE;
5122 else
5123 p = skipdigits(p + 1);
5124 }
5125 if (ASCII_ISALPHA(*p) || *p == '.')
5126 get_float = FALSE;
5127 }
5128 if (get_float)
5129 {
5130 float_T f;
5131
5132 *arg += string2float(*arg, &f);
5133 if (evaluate)
5134 {
5135 rettv->v_type = VAR_FLOAT;
5136 rettv->vval.v_float = f;
5137 }
5138 }
5139 else
5140#endif
5141 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005142 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 *arg += len;
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_NUMBER;
5147 rettv->vval.v_number = n;
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
5153 /*
5154 * String constant: "string".
5155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 break;
5164
5165 /*
5166 * List: [expr, expr]
5167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 * Dictionary: {key: val, key: val}
5173 */
5174 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5175 break;
5176
5177 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005178 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005180 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Environment variable: $VAR.
5185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188
5189 /*
5190 * Register contents: @r.
5191 */
5192 case '@': ++*arg;
5193 if (evaluate)
5194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005196 rettv->vval.v_string = get_reg_contents(**arg,
5197 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 if (**arg != NUL)
5200 ++*arg;
5201 break;
5202
5203 /*
5204 * nested expression: (expression).
5205 */
5206 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (**arg == ')')
5209 ++*arg;
5210 else if (ret == OK)
5211 {
5212 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 ret = FAIL;
5215 }
5216 break;
5217
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221
5222 if (ret == NOTDONE)
5223 {
5224 /*
5225 * Must be a variable or function name.
5226 * Can also be a curly-braces kind of name: {expr}.
5227 */
5228 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 if (alias != NULL)
5231 s = alias;
5232
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 ret = FAIL;
5235 else
5236 {
5237 if (**arg == '(') /* recursive! */
5238 {
5239 /* If "s" is the name of a variable of type VAR_FUNC
5240 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005241 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242
5243 /* Invoke the function. */
5244 ret = get_func_tv(s, len, rettv, arg,
5245 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005246 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005247
5248 /* If evaluate is FALSE rettv->v_type was not set in
5249 * get_func_tv, but it's needed in handle_subscript() to parse
5250 * what follows. So set it here. */
5251 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5252 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005253 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005254 rettv->v_type = VAR_FUNC;
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 /* Stop the expression evaluation when immediately
5258 * aborting on error, or when an interrupt occurred or
5259 * an exception was thrown but not caught. */
5260 if (aborting())
5261 {
5262 if (ret == OK)
5263 clear_tv(rettv);
5264 ret = FAIL;
5265 }
5266 }
5267 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005268 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005269 else
5270 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005272 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 }
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 *arg = skipwhite(*arg);
5276
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5278 * expr(expr). */
5279 if (ret == OK)
5280 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 /*
5283 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5284 */
5285 if (ret == OK && evaluate && end_leader > start_leader)
5286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 int val = 0;
5289#ifdef FEAT_FLOAT
5290 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292 if (rettv->v_type == VAR_FLOAT)
5293 f = rettv->vval.v_float;
5294 else
5295#endif
5296 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 clear_tv(rettv);
5300 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else
5303 {
5304 while (end_leader > start_leader)
5305 {
5306 --end_leader;
5307 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005308 {
5309#ifdef FEAT_FLOAT
5310 if (rettv->v_type == VAR_FLOAT)
5311 f = !f;
5312 else
5313#endif
5314 val = !val;
5315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005317 {
5318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 f = -f;
5321 else
5322#endif
5323 val = -val;
5324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326#ifdef FEAT_FLOAT
5327 if (rettv->v_type == VAR_FLOAT)
5328 {
5329 clear_tv(rettv);
5330 rettv->vval.v_float = f;
5331 }
5332 else
5333#endif
5334 {
5335 clear_tv(rettv);
5336 rettv->v_type = VAR_NUMBER;
5337 rettv->vval.v_number = val;
5338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341
5342 return ret;
5343}
5344
5345/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005346 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5347 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5349 */
5350 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005351eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356{
5357 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 long len = -1;
5361 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 if (verbose)
5368 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 return FAIL;
5370 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#ifdef FEAT_FLOAT
5372 else if (rettv->v_type == VAR_FLOAT)
5373 {
5374 if (verbose)
5375 EMSG(_(e_float_as_string));
5376 return FAIL;
5377 }
5378#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005380 init_tv(&var1);
5381 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 /*
5385 * dict.name
5386 */
5387 key = *arg + 1;
5388 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5389 ;
5390 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 /*
5397 * something[idx]
5398 *
5399 * Get the (first) variable from inside the [].
5400 */
5401 *arg = skipwhite(*arg + 1);
5402 if (**arg == ':')
5403 empty1 = TRUE;
5404 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5405 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5407 {
5408 /* not a number or string */
5409 clear_tv(&var1);
5410 return FAIL;
5411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412
5413 /*
5414 * Get the second variable from inside the [:].
5415 */
5416 if (**arg == ':')
5417 {
5418 range = TRUE;
5419 *arg = skipwhite(*arg + 1);
5420 if (**arg == ']')
5421 empty2 = TRUE;
5422 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005424 if (!empty1)
5425 clear_tv(&var1);
5426 return FAIL;
5427 }
5428 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5429 {
5430 /* not a number or string */
5431 if (!empty1)
5432 clear_tv(&var1);
5433 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 return FAIL;
5435 }
5436 }
5437
5438 /* Check for the ']'. */
5439 if (**arg != ']')
5440 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005441 if (verbose)
5442 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 clear_tv(&var1);
5444 if (range)
5445 clear_tv(&var2);
5446 return FAIL;
5447 }
5448 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
5450
5451 if (evaluate)
5452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 n1 = 0;
5454 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 n1 = get_tv_number(&var1);
5457 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 if (range)
5460 {
5461 if (empty2)
5462 n2 = -1;
5463 else
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 n2 = get_tv_number(&var2);
5466 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 }
5469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 {
5472 case VAR_NUMBER:
5473 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 len = (long)STRLEN(s);
5476 if (range)
5477 {
5478 /* The resulting variable is a substring. If the indexes
5479 * are out of range the result is empty. */
5480 if (n1 < 0)
5481 {
5482 n1 = len + n1;
5483 if (n1 < 0)
5484 n1 = 0;
5485 }
5486 if (n2 < 0)
5487 n2 = len + n2;
5488 else if (n2 >= len)
5489 n2 = len;
5490 if (n1 >= len || n2 < 0 || n1 > n2)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5494 }
5495 else
5496 {
5497 /* The resulting variable is a string of a single
5498 * character. If the index is too big or negative the
5499 * result is empty. */
5500 if (n1 >= len || n1 < 0)
5501 s = NULL;
5502 else
5503 s = vim_strnsave(s + n1, 1);
5504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 clear_tv(rettv);
5506 rettv->v_type = VAR_STRING;
5507 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 break;
5509
5510 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 if (n1 < 0)
5513 n1 = len + n1;
5514 if (!empty1 && (n1 < 0 || n1 >= len))
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 /* For a range we allow invalid values and return an empty
5517 * list. A list index out of range is an error. */
5518 if (!range)
5519 {
5520 if (verbose)
5521 EMSGN(_(e_listidx), n1);
5522 return FAIL;
5523 }
5524 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 }
5526 if (range)
5527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530
5531 if (n2 < 0)
5532 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005533 else if (n2 >= len)
5534 n2 = len - 1;
5535 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 l = list_alloc();
5538 if (l == NULL)
5539 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 n1 <= n2; ++n1)
5542 {
5543 if (list_append_tv(l, &item->li_tv) == FAIL)
5544 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005545 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 return FAIL;
5547 }
5548 item = item->li_next;
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_LIST;
5552 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005553 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 }
5555 else
5556 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 clear_tv(rettv);
5559 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562
5563 case VAR_DICT:
5564 if (range)
5565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005566 if (verbose)
5567 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (len == -1)
5569 clear_tv(&var1);
5570 return FAIL;
5571 }
5572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 if (len == -1)
5576 {
5577 key = get_tv_string(&var1);
5578 if (*key == NUL)
5579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580 if (verbose)
5581 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 }
5586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005587 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005589 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 if (len == -1)
5592 clear_tv(&var1);
5593 if (item == NULL)
5594 return FAIL;
5595
5596 copy_tv(&item->di_tv, &var1);
5597 clear_tv(rettv);
5598 *rettv = var1;
5599 }
5600 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 }
5603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Get an option value.
5609 * "arg" points to the '&' or '+' before the option name.
5610 * "arg" is advanced to character after the option name.
5611 * Return OK or FAIL.
5612 */
5613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 int evaluate;
5618{
5619 char_u *option_end;
5620 long numval;
5621 char_u *stringval;
5622 int opt_type;
5623 int c;
5624 int working = (**arg == '+'); /* has("+option") */
5625 int ret = OK;
5626 int opt_flags;
5627
5628 /*
5629 * Isolate the option name and find its value.
5630 */
5631 option_end = find_option_end(arg, &opt_flags);
5632 if (option_end == NULL)
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 EMSG2(_("E112: Option name missing: %s"), *arg);
5636 return FAIL;
5637 }
5638
5639 if (!evaluate)
5640 {
5641 *arg = option_end;
5642 return OK;
5643 }
5644
5645 c = *option_end;
5646 *option_end = NUL;
5647 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 if (opt_type == -3) /* invalid name */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E113: Unknown option: %s"), *arg);
5654 ret = FAIL;
5655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 if (opt_type == -2) /* hidden string option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_STRING;
5661 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == -1) /* hidden number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else if (opt_type == 1) /* number option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_NUMBER;
5671 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else /* string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 }
5679 else if (working && (opt_type == -2 || opt_type == -1))
5680 ret = FAIL;
5681
5682 *option_end = c; /* put back for error messages */
5683 *arg = option_end;
5684
5685 return ret;
5686}
5687
5688/*
5689 * Allocate a variable for a string constant.
5690 * Return OK or FAIL.
5691 */
5692 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 int evaluate;
5697{
5698 char_u *p;
5699 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int extra = 0;
5701
5702 /*
5703 * Find the end of the string, skipping backslashed characters.
5704 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 if (*p == '\\' && p[1] != NUL)
5708 {
5709 ++p;
5710 /* A "\<x>" form occupies at least 4 characters, and produces up
5711 * to 6 characters: reserve space for 2 extra */
5712 if (*p == '<')
5713 extra += 2;
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716
5717 if (*p != '"')
5718 {
5719 EMSG2(_("E114: Missing quote: %s"), *arg);
5720 return FAIL;
5721 }
5722
5723 /* If only parsing, set *arg and return here */
5724 if (!evaluate)
5725 {
5726 *arg = p + 1;
5727 return OK;
5728 }
5729
5730 /*
5731 * Copy the string into allocated memory, handling backslashed
5732 * characters.
5733 */
5734 name = alloc((unsigned)(p - *arg + extra));
5735 if (name == NULL)
5736 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 rettv->v_type = VAR_STRING;
5738 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 if (*p == '\\')
5743 {
5744 switch (*++p)
5745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 case 'b': *name++ = BS; ++p; break;
5747 case 'e': *name++ = ESC; ++p; break;
5748 case 'f': *name++ = FF; ++p; break;
5749 case 'n': *name++ = NL; ++p; break;
5750 case 'r': *name++ = CAR; ++p; break;
5751 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
5753 case 'X': /* hex: "\x1", "\x12" */
5754 case 'x':
5755 case 'u': /* Unicode: "\u0023" */
5756 case 'U':
5757 if (vim_isxdigit(p[1]))
5758 {
5759 int n, nr;
5760 int c = toupper(*p);
5761
5762 if (c == 'X')
5763 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005764 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005766 else
5767 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 nr = 0;
5769 while (--n >= 0 && vim_isxdigit(p[1]))
5770 {
5771 ++p;
5772 nr = (nr << 4) + hex2nr(*p);
5773 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_MBYTE
5776 /* For "\u" store the number according to
5777 * 'encoding'. */
5778 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 else
5781#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 break;
5785
5786 /* octal: "\1", "\12", "\123" */
5787 case '0':
5788 case '1':
5789 case '2':
5790 case '3':
5791 case '4':
5792 case '5':
5793 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 case '7': *name = *p++ - '0';
5795 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name = (*name << 3) + *p++ - '0';
5798 if (*p >= '0' && *p <= '7')
5799 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 break;
5803
5804 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (extra != 0)
5807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810 }
5811 /* FALLTHROUGH */
5812
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816 }
5817 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 *arg = p + 1;
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 return OK;
5825}
5826
5827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 * Return OK or FAIL.
5830 */
5831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 int evaluate;
5836{
5837 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 int reduce = 0;
5840
5841 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 */
5844 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 break;
5850 ++reduce;
5851 ++p;
5852 }
5853 }
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 return FAIL;
5859 }
5860
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 if (!evaluate)
5863 {
5864 *arg = p + 1;
5865 return OK;
5866 }
5867
5868 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 */
5871 str = alloc((unsigned)((p - *arg) - reduce));
5872 if (str == NULL)
5873 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 rettv->v_type = VAR_STRING;
5875 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 break;
5883 ++p;
5884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 *arg = p + 1;
5889
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 return OK;
5891}
5892
5893/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 * Allocate a variable for a List and fill it from "*arg".
5895 * Return OK or FAIL.
5896 */
5897 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005898get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 int evaluate;
5902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l = NULL;
5904 typval_T tv;
5905 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906
5907 if (evaluate)
5908 {
5909 l = list_alloc();
5910 if (l == NULL)
5911 return FAIL;
5912 }
5913
5914 *arg = skipwhite(*arg + 1);
5915 while (**arg != ']' && **arg != NUL)
5916 {
5917 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5918 goto failret;
5919 if (evaluate)
5920 {
5921 item = listitem_alloc();
5922 if (item != NULL)
5923 {
5924 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005925 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 list_append(l, item);
5927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005928 else
5929 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 }
5931
5932 if (**arg == ']')
5933 break;
5934 if (**arg != ',')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 goto failret;
5938 }
5939 *arg = skipwhite(*arg + 1);
5940 }
5941
5942 if (**arg != ']')
5943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945failret:
5946 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 return FAIL;
5949 }
5950
5951 *arg = skipwhite(*arg + 1);
5952 if (evaluate)
5953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005954 rettv->v_type = VAR_LIST;
5955 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 ++l->lv_refcount;
5957 }
5958
5959 return OK;
5960}
5961
5962/*
5963 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005964 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005966 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967list_alloc()
5968{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005969 list_T *l;
5970
5971 l = (list_T *)alloc_clear(sizeof(list_T));
5972 if (l != NULL)
5973 {
5974 /* Prepend the list to the list of lists for garbage collection. */
5975 if (first_list != NULL)
5976 first_list->lv_used_prev = l;
5977 l->lv_used_prev = NULL;
5978 l->lv_used_next = first_list;
5979 first_list = l;
5980 }
5981 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982}
5983
5984/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005985 * Allocate an empty list for a return value.
5986 * Returns OK or FAIL.
5987 */
5988 static int
5989rettv_list_alloc(rettv)
5990 typval_T *rettv;
5991{
5992 list_T *l = list_alloc();
5993
5994 if (l == NULL)
5995 return FAIL;
5996
5997 rettv->vval.v_list = l;
5998 rettv->v_type = VAR_LIST;
5999 ++l->lv_refcount;
6000 return OK;
6001}
6002
6003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 * Unreference a list: decrement the reference count and free it when it
6005 * becomes zero.
6006 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006007 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006011 if (l != NULL && --l->lv_refcount <= 0)
6012 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006016 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 * Ignores the reference count.
6018 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006019 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006020list_free(l, recurse)
6021 list_T *l;
6022 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Remove the list from the list of lists for garbage collection. */
6027 if (l->lv_used_prev == NULL)
6028 first_list = l->lv_used_next;
6029 else
6030 l->lv_used_prev->lv_used_next = l->lv_used_next;
6031 if (l->lv_used_next != NULL)
6032 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6033
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 /* Remove the item before deleting it. */
6037 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (recurse || (item->li_tv.v_type != VAR_LIST
6039 && item->li_tv.v_type != VAR_DICT))
6040 clear_tv(&item->li_tv);
6041 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 }
6043 vim_free(l);
6044}
6045
6046/*
6047 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006048 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006050 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051listitem_alloc()
6052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006057 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 vim_free(item);
6065}
6066
6067/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006068 * Remove a list item from a List and free it. Also clears the value.
6069 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006070 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 list_T *l;
6073 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006075 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076 listitem_free(item);
6077}
6078
6079/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 * Get the number of items in a list.
6081 */
6082 static long
6083list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (l == NULL)
6087 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE when two lists have exactly the same values.
6093 */
6094 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 list_T *l1;
6097 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006103 if (l1 == NULL || l2 == NULL)
6104 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 if (l1 == l2)
6106 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 if (list_len(l1) != list_len(l2))
6108 return FALSE;
6109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 for (item1 = l1->lv_first, item2 = l2->lv_first;
6111 item1 != NULL && item2 != NULL;
6112 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return FALSE;
6115 return item1 == NULL && item2 == NULL;
6116}
6117
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006118#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6119 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006120/*
6121 * Return the dictitem that an entry in a hashtable points to.
6122 */
6123 dictitem_T *
6124dict_lookup(hi)
6125 hashitem_T *hi;
6126{
6127 return HI2DI(hi);
6128}
6129#endif
6130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 * Return TRUE when two dictionaries have exactly the same key/values.
6133 */
6134 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dict_T *d1;
6137 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
6142 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006145 if (d1 == NULL || d2 == NULL)
6146 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006147 if (d1 == d2)
6148 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 if (dict_len(d1) != dict_len(d2))
6150 return FALSE;
6151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 if (!HASHITEM_EMPTY(hi))
6156 {
6157 item2 = dict_find(d2, hi->hi_key, -1);
6158 if (item2 == NULL)
6159 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 return FALSE;
6162 --todo;
6163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 }
6165 return TRUE;
6166}
6167
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168static int tv_equal_recurse_limit;
6169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 */
6175 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 typval_T *tv1;
6178 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 int ic; /* ignore case */
6180 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181{
6182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006185 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006190 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 * recursiveness to a limit. We guess they are equal then.
6192 * A fixed limit has the problem of still taking an awful long time.
6193 * Reduce the limit every time running into it. That should work fine for
6194 * deeply linked structures that are not recursively linked and catch
6195 * recursiveness quickly. */
6196 if (!recursive)
6197 tv_equal_recurse_limit = 1000;
6198 if (recursive_cnt >= tv_equal_recurse_limit)
6199 {
6200 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 ++recursive_cnt;
6214 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 case VAR_FUNC:
6219 return (tv1->vval.v_string != NULL
6220 && tv2->vval.v_string != NULL
6221 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6222
6223 case VAR_NUMBER:
6224 return tv1->vval.v_number == tv2->vval.v_number;
6225
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006226#ifdef FEAT_FLOAT
6227 case VAR_FLOAT:
6228 return tv1->vval.v_float == tv2->vval.v_float;
6229#endif
6230
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_STRING:
6232 s1 = get_tv_string_buf(tv1, buf1);
6233 s2 = get_tv_string_buf(tv2, buf2);
6234 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236
6237 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238 return TRUE;
6239}
6240
6241/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 * Locate item with index "n" in list "l" and return it.
6243 * A negative index is counted from the end; -1 is the last item.
6244 * Returns NULL when "n" is out of range.
6245 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 long n;
6250{
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 long idx;
6253
6254 if (l == NULL)
6255 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006256
6257 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 n = l->lv_len + n;
6260
6261 /* Check for index out of range. */
6262 if (n < 0 || n >= l->lv_len)
6263 return NULL;
6264
6265 /* When there is a cached index may start search from there. */
6266 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268 if (n < l->lv_idx / 2)
6269 {
6270 /* closest to the start of the list */
6271 item = l->lv_first;
6272 idx = 0;
6273 }
6274 else if (n > (l->lv_idx + l->lv_len) / 2)
6275 {
6276 /* closest to the end of the list */
6277 item = l->lv_last;
6278 idx = l->lv_len - 1;
6279 }
6280 else
6281 {
6282 /* closest to the cached index */
6283 item = l->lv_idx_item;
6284 idx = l->lv_idx;
6285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 }
6287 else
6288 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289 if (n < l->lv_len / 2)
6290 {
6291 /* closest to the start of the list */
6292 item = l->lv_first;
6293 idx = 0;
6294 }
6295 else
6296 {
6297 /* closest to the end of the list */
6298 item = l->lv_last;
6299 idx = l->lv_len - 1;
6300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006302
6303 while (n > idx)
6304 {
6305 /* search forward */
6306 item = item->li_next;
6307 ++idx;
6308 }
6309 while (n < idx)
6310 {
6311 /* search backward */
6312 item = item->li_prev;
6313 --idx;
6314 }
6315
6316 /* cache the used index */
6317 l->lv_idx = idx;
6318 l->lv_idx_item = item;
6319
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 return item;
6321}
6322
6323/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006324 * Get list item "l[idx]" as a number.
6325 */
6326 static long
6327list_find_nr(l, idx, errorp)
6328 list_T *l;
6329 long idx;
6330 int *errorp; /* set to TRUE when something wrong */
6331{
6332 listitem_T *li;
6333
6334 li = list_find(l, idx);
6335 if (li == NULL)
6336 {
6337 if (errorp != NULL)
6338 *errorp = TRUE;
6339 return -1L;
6340 }
6341 return get_tv_number_chk(&li->li_tv, errorp);
6342}
6343
6344/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006345 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6346 */
6347 char_u *
6348list_find_str(l, idx)
6349 list_T *l;
6350 long idx;
6351{
6352 listitem_T *li;
6353
6354 li = list_find(l, idx - 1);
6355 if (li == NULL)
6356 {
6357 EMSGN(_(e_listidx), idx);
6358 return NULL;
6359 }
6360 return get_tv_string(&li->li_tv);
6361}
6362
6363/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006364 * Locate "item" list "l" and return its index.
6365 * Returns -1 when "item" is not in the list.
6366 */
6367 static long
6368list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 list_T *l;
6370 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371{
6372 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374
6375 if (l == NULL)
6376 return -1;
6377 idx = 0;
6378 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6379 ++idx;
6380 if (li == NULL)
6381 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006382 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383}
6384
6385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 * Append item "item" to the end of list "l".
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 list_T *l;
6391 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392{
6393 if (l->lv_last == NULL)
6394 {
6395 /* empty list */
6396 l->lv_first = item;
6397 l->lv_last = item;
6398 item->li_prev = NULL;
6399 }
6400 else
6401 {
6402 l->lv_last->li_next = item;
6403 item->li_prev = l->lv_last;
6404 l->lv_last = item;
6405 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 item->li_next = NULL;
6408}
6409
6410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006414 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 list_T *l;
6417 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 copy_tv(tv, &li->li_tv);
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006429 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 * Return FAIL when out of memory.
6431 */
6432 int
6433list_append_dict(list, dict)
6434 list_T *list;
6435 dict_T *dict;
6436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 li->li_tv.v_type = VAR_DICT;
6442 li->li_tv.v_lock = 0;
6443 li->li_tv.vval.v_dict = dict;
6444 list_append(list, li);
6445 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 return OK;
6447}
6448
6449/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006451 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Returns FAIL when out of memory.
6453 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006454 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006455list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006456 list_T *l;
6457 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459{
6460 listitem_T *li = listitem_alloc();
6461
6462 if (li == NULL)
6463 return FAIL;
6464 list_append(l, li);
6465 li->li_tv.v_type = VAR_STRING;
6466 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006467 if (str == NULL)
6468 li->li_tv.vval.v_string = NULL;
6469 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006470 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471 return FAIL;
6472 return OK;
6473}
6474
6475/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476 * Append "n" to list "l".
6477 * Returns FAIL when out of memory.
6478 */
6479 static int
6480list_append_number(l, n)
6481 list_T *l;
6482 varnumber_T n;
6483{
6484 listitem_T *li;
6485
6486 li = listitem_alloc();
6487 if (li == NULL)
6488 return FAIL;
6489 li->li_tv.v_type = VAR_NUMBER;
6490 li->li_tv.v_lock = 0;
6491 li->li_tv.vval.v_number = n;
6492 list_append(l, li);
6493 return OK;
6494}
6495
6496/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 * If "item" is NULL append at the end.
6499 * Return FAIL when out of memory.
6500 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006501 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
6504 typval_T *tv;
6505 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508
6509 if (ni == NULL)
6510 return FAIL;
6511 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006512 list_insert(l, ni, item);
6513 return OK;
6514}
6515
6516 void
6517list_insert(l, ni, item)
6518 list_T *l;
6519 listitem_T *ni;
6520 listitem_T *item;
6521{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 if (item == NULL)
6523 /* Append new item at end of list. */
6524 list_append(l, ni);
6525 else
6526 {
6527 /* Insert new item before existing item. */
6528 ni->li_prev = item->li_prev;
6529 ni->li_next = item;
6530 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 ++l->lv_idx;
6534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 l->lv_idx_item = NULL;
6539 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543}
6544
6545/*
6546 * Extend "l1" with "l2".
6547 * If "bef" is NULL append at the end, otherwise insert before this item.
6548 * Returns FAIL when out of memory.
6549 */
6550 static int
6551list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l1;
6553 list_T *l2;
6554 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006557 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006559 /* We also quit the loop when we have inserted the original item count of
6560 * the list, avoid a hang when we extend a list with itself. */
6561 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6563 return FAIL;
6564 return OK;
6565}
6566
6567/*
6568 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6569 * Return FAIL when out of memory.
6570 */
6571 static int
6572list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l1;
6574 list_T *l2;
6575 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006579 if (l1 == NULL || l2 == NULL)
6580 return FAIL;
6581
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 if (l == NULL)
6585 return FAIL;
6586 tv->v_type = VAR_LIST;
6587 tv->vval.v_list = l;
6588
6589 /* append all items from the second list */
6590 return list_extend(l, l2, NULL);
6591}
6592
6593/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 * Returns NULL when out of memory.
6598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604{
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 list_T *copy;
6606 listitem_T *item;
6607 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608
6609 if (orig == NULL)
6610 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611
6612 copy = list_alloc();
6613 if (copy != NULL)
6614 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 if (copyID != 0)
6616 {
6617 /* Do this before adding the items, because one of the items may
6618 * refer back to this list. */
6619 orig->lv_copyID = copyID;
6620 orig->lv_copylist = copy;
6621 }
6622 for (item = orig->lv_first; item != NULL && !got_int;
6623 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 {
6625 ni = listitem_alloc();
6626 if (ni == NULL)
6627 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006629 {
6630 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6631 {
6632 vim_free(ni);
6633 break;
6634 }
6635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006637 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 list_append(copy, ni);
6639 }
6640 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006641 if (item != NULL)
6642 {
6643 list_unref(copy);
6644 copy = NULL;
6645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 }
6647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 return copy;
6649}
6650
6651/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006653 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006654 * This used to be called list_remove, but that conflicts with a Sun header
6655 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006657 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006658vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 list_T *l;
6660 listitem_T *item;
6661 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* notify watchers */
6666 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006668 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 list_fix_watch(l, ip);
6670 if (ip == item2)
6671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673
6674 if (item2->li_next == NULL)
6675 l->lv_last = item->li_prev;
6676 else
6677 item2->li_next->li_prev = item->li_prev;
6678 if (item->li_prev == NULL)
6679 l->lv_first = item2->li_next;
6680 else
6681 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006682 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683}
6684
6685/*
6686 * Return an allocated string with the string representation of a list.
6687 * May return NULL.
6688 */
6689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006691 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693{
6694 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695
6696 if (tv->vval.v_list == NULL)
6697 return NULL;
6698 ga_init2(&ga, (int)sizeof(char), 80);
6699 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006701 {
6702 vim_free(ga.ga_data);
6703 return NULL;
6704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705 ga_append(&ga, ']');
6706 ga_append(&ga, NUL);
6707 return (char_u *)ga.ga_data;
6708}
6709
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710typedef struct join_S {
6711 char_u *s;
6712 char_u *tofree;
6713} join_T;
6714
6715 static int
6716list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6717 garray_T *gap; /* to store the result in */
6718 list_T *l;
6719 char_u *sep;
6720 int echo_style;
6721 int copyID;
6722 garray_T *join_gap; /* to keep each list item string */
6723{
6724 int i;
6725 join_T *p;
6726 int len;
6727 int sumlen = 0;
6728 int first = TRUE;
6729 char_u *tofree;
6730 char_u numbuf[NUMBUFLEN];
6731 listitem_T *item;
6732 char_u *s;
6733
6734 /* Stringify each item in the list. */
6735 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6736 {
6737 if (echo_style)
6738 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6739 else
6740 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6741 if (s == NULL)
6742 return FAIL;
6743
6744 len = (int)STRLEN(s);
6745 sumlen += len;
6746
Bram Moolenaarcde88542015-08-11 19:14:00 +02006747 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006748 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6749 if (tofree != NULL || s != numbuf)
6750 {
6751 p->s = s;
6752 p->tofree = tofree;
6753 }
6754 else
6755 {
6756 p->s = vim_strnsave(s, len);
6757 p->tofree = p->s;
6758 }
6759
6760 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006761 if (did_echo_string_emsg) /* recursion error, bail out */
6762 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 }
6764
6765 /* Allocate result buffer with its total size, avoid re-allocation and
6766 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6767 if (join_gap->ga_len >= 2)
6768 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6769 if (ga_grow(gap, sumlen + 2) == FAIL)
6770 return FAIL;
6771
6772 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6773 {
6774 if (first)
6775 first = FALSE;
6776 else
6777 ga_concat(gap, sep);
6778 p = ((join_T *)join_gap->ga_data) + i;
6779
6780 if (p->s != NULL)
6781 ga_concat(gap, p->s);
6782 line_breakcheck();
6783 }
6784
6785 return OK;
6786}
6787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006790 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006793 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006794list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006798 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006799 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 garray_T join_ga;
6802 int retval;
6803 join_T *p;
6804 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805
Bram Moolenaard39a7512015-04-16 22:51:22 +02006806 if (l->lv_len < 1)
6807 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6809 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6810
6811 /* Dispose each item in join_ga. */
6812 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = (join_T *)join_ga.ga_data;
6815 for (i = 0; i < join_ga.ga_len; ++i)
6816 {
6817 vim_free(p->tofree);
6818 ++p;
6819 }
6820 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822
6823 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824}
6825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Garbage collection for lists and dictionaries.
6828 *
6829 * We use reference counts to be able to free most items right away when they
6830 * are no longer used. But for composite items it's possible that it becomes
6831 * unused while the reference count is > 0: When there is a recursive
6832 * reference. Example:
6833 * :let l = [1, 2, 3]
6834 * :let d = {9: l}
6835 * :let l[1] = d
6836 *
6837 * Since this is quite unusual we handle this with garbage collection: every
6838 * once in a while find out which lists and dicts are not referenced from any
6839 * variable.
6840 *
6841 * Here is a good reference text about garbage collection (refers to Python
6842 * but it applies to all reference-counting mechanisms):
6843 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845
6846/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 * Do garbage collection for lists and dicts.
6848 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 int
6851garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 buf_T *buf;
6856 win_T *wp;
6857 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006858 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006859 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006861#ifdef FEAT_WINDOWS
6862 tabpage_T *tp;
6863#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006865 /* Only do this once. */
6866 want_garbage_collect = FALSE;
6867 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006868 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 /* We advance by two because we add one for items referenced through
6871 * previous_funccal. */
6872 current_copyID += COPYID_INC;
6873 copyID = current_copyID;
6874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /*
6876 * 1. Go through all accessible variables and mark all lists and dicts
6877 * with copyID.
6878 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879
6880 /* Don't free variables in the previous_funccal list unless they are only
6881 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006882 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6884 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6886 NULL);
6887 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6888 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /* script-local variables */
6892 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* buffer-local variables */
6896 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6903 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006904#ifdef FEAT_AUTOCMD
6905 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#ifdef FEAT_WINDOWS
6911 /* tabpage-local variables */
6912 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#endif
6916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
6920 /* function-local variables */
6921 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6924 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 }
6926
Bram Moolenaard812df62008-11-09 12:46:09 +00006927 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006929
Bram Moolenaar1dced572012-04-05 16:54:08 +02006930#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006932#endif
6933
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006936#endif
6937
6938#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 /*
6945 * 2. Free lists and dictionaries that are not referenced.
6946 */
6947 did_free = free_unref_items(copyID);
6948
6949 /*
6950 * 3. Check if any funccal can be freed now.
6951 */
6952 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 if (can_free_funccal(*pfc, copyID))
6955 {
6956 fc = *pfc;
6957 *pfc = fc->caller;
6958 free_funccal(fc, TRUE);
6959 did_free = TRUE;
6960 did_free_funccal = TRUE;
6961 }
6962 else
6963 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (did_free_funccal)
6966 /* When a funccal was freed some more items might be garbage
6967 * collected, so run again. */
6968 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006970 else if (p_verbose > 0)
6971 {
6972 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6973 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974
6975 return did_free;
6976}
6977
6978/*
6979 * Free lists and dictionaries that are no longer referenced.
6980 */
6981 static int
6982free_unref_items(copyID)
6983 int copyID;
6984{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dict_T *dd, *dd_next;
6986 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 int did_free = FALSE;
6988
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 */
6992 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 {
6994 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 /* Free the Dictionary and ordinary items it contains, but don't
6998 * recurse into Lists and Dictionaries, they will be in the list
6999 * of dicts or list of lists. */
7000 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007003 dd = dd_next;
7004 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005
7006 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 * Go through the list of lists and free items without the copyID.
7008 * But don't free a list that has a watcher (used in a for loop), these
7009 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
7011 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 {
7013 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7015 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007017 /* Free the List and ordinary items it contains, but don't recurse
7018 * into Lists and Dictionaries, they will be in the list of dicts
7019 * or list of lists. */
7020 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 ll = ll_next;
7024 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 return did_free;
7026}
7027
7028/*
7029 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 * "list_stack" is used to add lists to be marked. Can be NULL.
7031 *
7032 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 int
7035set_ref_in_ht(ht, copyID, list_stack)
7036 hashtab_T *ht;
7037 int copyID;
7038 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039{
7040 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 hashtab_T *cur_ht;
7044 ht_stack_T *ht_stack = NULL;
7045 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 cur_ht = ht;
7048 for (;;)
7049 {
7050 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 /* Mark each item in the hashtab. If the item contains a hashtab
7053 * it is added to ht_stack, if it contains a list it is added to
7054 * list_stack. */
7055 todo = (int)cur_ht->ht_used;
7056 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7057 if (!HASHITEM_EMPTY(hi))
7058 {
7059 --todo;
7060 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7061 &ht_stack, list_stack);
7062 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064
7065 if (ht_stack == NULL)
7066 break;
7067
7068 /* take an item from the stack */
7069 cur_ht = ht_stack->ht;
7070 tempitem = ht_stack;
7071 ht_stack = ht_stack->prev;
7072 free(tempitem);
7073 }
7074
7075 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076}
7077
7078/*
7079 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7081 *
7082 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 int
7085set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 list_T *l;
7087 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007090 listitem_T *li;
7091 int abort = FALSE;
7092 list_T *cur_l;
7093 list_stack_T *list_stack = NULL;
7094 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 cur_l = l;
7097 for (;;)
7098 {
7099 if (!abort)
7100 /* Mark each item in the list. If the item contains a hashtab
7101 * it is added to ht_stack, if it contains a list it is added to
7102 * list_stack. */
7103 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7104 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7105 ht_stack, &list_stack);
7106 if (list_stack == NULL)
7107 break;
7108
7109 /* take an item from the stack */
7110 cur_l = list_stack->list;
7111 tempitem = list_stack;
7112 list_stack = list_stack->prev;
7113 free(tempitem);
7114 }
7115
7116 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117}
7118
7119/*
7120 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 * "list_stack" is used to add lists to be marked. Can be NULL.
7122 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7123 *
7124 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007126 int
7127set_ref_in_item(tv, copyID, ht_stack, list_stack)
7128 typval_T *tv;
7129 int copyID;
7130 ht_stack_T **ht_stack;
7131 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007132{
7133 dict_T *dd;
7134 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136
7137 switch (tv->v_type)
7138 {
7139 case VAR_DICT:
7140 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007141 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007142 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143 /* Didn't see this dict yet. */
7144 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 if (ht_stack == NULL)
7146 {
7147 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7148 }
7149 else
7150 {
7151 ht_stack_T *newitem = (ht_stack_T*)malloc(
7152 sizeof(ht_stack_T));
7153 if (newitem == NULL)
7154 abort = TRUE;
7155 else
7156 {
7157 newitem->ht = &dd->dv_hashtab;
7158 newitem->prev = *ht_stack;
7159 *ht_stack = newitem;
7160 }
7161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164
7165 case VAR_LIST:
7166 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007167 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169 /* Didn't see this list yet. */
7170 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 if (list_stack == NULL)
7172 {
7173 abort = set_ref_in_list(ll, copyID, ht_stack);
7174 }
7175 else
7176 {
7177 list_stack_T *newitem = (list_stack_T*)malloc(
7178 sizeof(list_stack_T));
7179 if (newitem == NULL)
7180 abort = TRUE;
7181 else
7182 {
7183 newitem->list = ll;
7184 newitem->prev = *list_stack;
7185 *list_stack = newitem;
7186 }
7187 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192}
7193
7194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 * Allocate an empty header for a dictionary.
7196 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007197 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198dict_alloc()
7199{
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007205 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 if (first_dict != NULL)
7207 first_dict->dv_used_prev = d;
7208 d->dv_used_next = first_dict;
7209 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007210 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007213 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007214 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007217 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
7221/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007222 * Allocate an empty dict for a return value.
7223 * Returns OK or FAIL.
7224 */
7225 static int
7226rettv_dict_alloc(rettv)
7227 typval_T *rettv;
7228{
7229 dict_T *d = dict_alloc();
7230
7231 if (d == NULL)
7232 return FAIL;
7233
7234 rettv->vval.v_dict = d;
7235 rettv->v_type = VAR_DICT;
7236 ++d->dv_refcount;
7237 return OK;
7238}
7239
7240
7241/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 * Unreference a Dictionary: decrement the reference count and free it when it
7243 * becomes zero.
7244 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007245 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007249 if (d != NULL && --d->dv_refcount <= 0)
7250 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251}
7252
7253/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007254 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 * Ignores the reference count.
7256 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007257 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007258dict_free(d, recurse)
7259 dict_T *d;
7260 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007264 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007266 /* Remove the dict from the list of dicts for garbage collection. */
7267 if (d->dv_used_prev == NULL)
7268 first_dict = d->dv_used_next;
7269 else
7270 d->dv_used_prev->dv_used_next = d->dv_used_next;
7271 if (d->dv_used_next != NULL)
7272 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7273
7274 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if (!HASHITEM_EMPTY(hi))
7280 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007281 /* Remove the item before deleting it, just in case there is
7282 * something recursive causing trouble. */
7283 di = HI2DI(hi);
7284 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007285 if (recurse || (di->di_tv.v_type != VAR_LIST
7286 && di->di_tv.v_type != VAR_DICT))
7287 clear_tv(&di->di_tv);
7288 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 --todo;
7290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 vim_free(d);
7294}
7295
7296/*
7297 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 * The "key" is copied to the new item.
7299 * Note that the value of the item "di_tv" still needs to be initialized!
7300 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007302 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303dictitem_alloc(key)
7304 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305{
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007312 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315}
7316
7317/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Make a copy of a Dictionary item.
7319 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323{
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007326 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7327 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 if (di != NULL)
7329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007331 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 copy_tv(&org->di_tv, &di->di_tv);
7333 }
7334 return di;
7335}
7336
7337/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 * Remove item "item" from Dictionary "dict" and free it.
7339 */
7340 static void
7341dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 dict_T *dict;
7343 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 if (HASHITEM_EMPTY(hi))
7349 EMSG2(_(e_intern2), "dictitem_remove()");
7350 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 dictitem_free(item);
7353}
7354
7355/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 * Free a dict item. Also clears the value.
7357 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007358 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007363 if (item->di_flags & DI_FLAGS_ALLOC)
7364 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365}
7366
7367/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7369 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 * Returns NULL when out of memory.
7372 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378{
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 dict_T *copy;
7380 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007381 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383
7384 if (orig == NULL)
7385 return NULL;
7386
7387 copy = dict_alloc();
7388 if (copy != NULL)
7389 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 if (copyID != 0)
7391 {
7392 orig->dv_copyID = copyID;
7393 orig->dv_copydict = copy;
7394 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007395 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 --todo;
7401
7402 di = dictitem_alloc(hi->hi_key);
7403 if (di == NULL)
7404 break;
7405 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 {
7407 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7408 copyID) == FAIL)
7409 {
7410 vim_free(di);
7411 break;
7412 }
7413 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 else
7415 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7416 if (dict_add(copy, di) == FAIL)
7417 {
7418 dictitem_free(di);
7419 break;
7420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007423
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (todo > 0)
7426 {
7427 dict_unref(copy);
7428 copy = NULL;
7429 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 }
7431
7432 return copy;
7433}
7434
7435/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007437 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007439 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 dict_T *d;
7442 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445}
7446
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007448 * Add a number or string entry to dictionary "d".
7449 * When "str" is NULL use number "nr", otherwise use "str".
7450 * Returns FAIL when out of memory and when key already exists.
7451 */
7452 int
7453dict_add_nr_str(d, key, nr, str)
7454 dict_T *d;
7455 char *key;
7456 long nr;
7457 char_u *str;
7458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 if (str == NULL)
7466 {
7467 item->di_tv.v_type = VAR_NUMBER;
7468 item->di_tv.vval.v_number = nr;
7469 }
7470 else
7471 {
7472 item->di_tv.v_type = VAR_STRING;
7473 item->di_tv.vval.v_string = vim_strsave(str);
7474 }
7475 if (dict_add(d, item) == FAIL)
7476 {
7477 dictitem_free(item);
7478 return FAIL;
7479 }
7480 return OK;
7481}
7482
7483/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007484 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007485 * Returns FAIL when out of memory and when key already exists.
7486 */
7487 int
7488dict_add_list(d, key, list)
7489 dict_T *d;
7490 char *key;
7491 list_T *list;
7492{
7493 dictitem_T *item;
7494
7495 item = dictitem_alloc((char_u *)key);
7496 if (item == NULL)
7497 return FAIL;
7498 item->di_tv.v_lock = 0;
7499 item->di_tv.v_type = VAR_LIST;
7500 item->di_tv.vval.v_list = list;
7501 if (dict_add(d, item) == FAIL)
7502 {
7503 dictitem_free(item);
7504 return FAIL;
7505 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007506 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007507 return OK;
7508}
7509
7510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 * Get the number of items in a Dictionary.
7512 */
7513 static long
7514dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517 if (d == NULL)
7518 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007519 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007520}
7521
7522/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 * Find item "key[len]" in Dictionary "d".
7524 * If "len" is negative use strlen(key).
7525 * Returns NULL when not found.
7526 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007527 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 char_u *key;
7531 int len;
7532{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533#define AKEYLEN 200
7534 char_u buf[AKEYLEN];
7535 char_u *akey;
7536 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 if (len < 0)
7540 akey = key;
7541 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007542 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 tofree = akey = vim_strnsave(key, len);
7544 if (akey == NULL)
7545 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 else
7548 {
7549 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007550 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 akey = buf;
7552 }
7553
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 vim_free(tofree);
7556 if (HASHITEM_EMPTY(hi))
7557 return NULL;
7558 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559}
7560
7561/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 * Get a string item from a dictionary.
7563 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564 * Returns NULL if the entry doesn't exist or out of memory.
7565 */
7566 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007567get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007568 dict_T *d;
7569 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007570 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571{
7572 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007574
7575 di = dict_find(d, key, -1);
7576 if (di == NULL)
7577 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578 s = get_tv_string(&di->di_tv);
7579 if (save && s != NULL)
7580 s = vim_strsave(s);
7581 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582}
7583
7584/*
7585 * Get a number item from a dictionary.
7586 * Returns 0 if the entry doesn't exist or out of memory.
7587 */
7588 long
7589get_dict_number(d, key)
7590 dict_T *d;
7591 char_u *key;
7592{
7593 dictitem_T *di;
7594
7595 di = dict_find(d, key, -1);
7596 if (di == NULL)
7597 return 0;
7598 return get_tv_number(&di->di_tv);
7599}
7600
7601/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 * Return an allocated string with the string representation of a Dictionary.
7603 * May return NULL.
7604 */
7605 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007608 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609{
7610 garray_T ga;
7611 int first = TRUE;
7612 char_u *tofree;
7613 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 return NULL;
7621 ga_init2(&ga, (int)sizeof(char), 80);
7622 ga_append(&ga, '{');
7623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007624 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 --todo;
7630
7631 if (first)
7632 first = FALSE;
7633 else
7634 ga_concat(&ga, (char_u *)", ");
7635
7636 tofree = string_quote(hi->hi_key, FALSE);
7637 if (tofree != NULL)
7638 {
7639 ga_concat(&ga, tofree);
7640 vim_free(tofree);
7641 }
7642 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644 if (s != NULL)
7645 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007647 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007648 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007649 line_breakcheck();
7650
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007653 if (todo > 0)
7654 {
7655 vim_free(ga.ga_data);
7656 return NULL;
7657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 ga_append(&ga, '}');
7660 ga_append(&ga, NUL);
7661 return (char_u *)ga.ga_data;
7662}
7663
7664/*
7665 * Allocate a variable for a Dictionary and fill it from "*arg".
7666 * Return OK or FAIL. Returns NOTDONE for {expr}.
7667 */
7668 static int
7669get_dict_tv(arg, rettv, evaluate)
7670 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 int evaluate;
7673{
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 dict_T *d = NULL;
7675 typval_T tvkey;
7676 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007677 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 /*
7683 * First check if it's not a curly-braces thing: {expr}.
7684 * Must do this without evaluating, otherwise a function may be called
7685 * twice. Unfortunately this means we need to call eval1() twice for the
7686 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 if (*start != '}')
7690 {
7691 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7692 return FAIL;
7693 if (*start == '}')
7694 return NOTDONE;
7695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696
7697 if (evaluate)
7698 {
7699 d = dict_alloc();
7700 if (d == NULL)
7701 return FAIL;
7702 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 tvkey.v_type = VAR_UNKNOWN;
7704 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705
7706 *arg = skipwhite(*arg + 1);
7707 while (**arg != '}' && **arg != NUL)
7708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 if (**arg != ':')
7712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007713 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007717 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007719 key = get_tv_string_buf_chk(&tvkey, buf);
7720 if (key == NULL || *key == NUL)
7721 {
7722 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7723 if (key != NULL)
7724 EMSG(_(e_emptykey));
7725 clear_tv(&tvkey);
7726 goto failret;
7727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729
7730 *arg = skipwhite(*arg + 1);
7731 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7732 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007733 if (evaluate)
7734 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 goto failret;
7736 }
7737 if (evaluate)
7738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 if (item != NULL)
7741 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007742 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 clear_tv(&tv);
7745 goto failret;
7746 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 item = dictitem_alloc(key);
7748 clear_tv(&tvkey);
7749 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007752 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 if (dict_add(d, item) == FAIL)
7754 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 }
7756 }
7757
7758 if (**arg == '}')
7759 break;
7760 if (**arg != ',')
7761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007762 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 goto failret;
7764 }
7765 *arg = skipwhite(*arg + 1);
7766 }
7767
7768 if (**arg != '}')
7769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007770 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771failret:
7772 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007773 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 return FAIL;
7775 }
7776
7777 *arg = skipwhite(*arg + 1);
7778 if (evaluate)
7779 {
7780 rettv->v_type = VAR_DICT;
7781 rettv->vval.v_dict = d;
7782 ++d->dv_refcount;
7783 }
7784
7785 return OK;
7786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 * Return a string with the string representation of a variable.
7790 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007793 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007794 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 */
7796 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007797echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007800 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007801 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 static int recurse = 0;
7804 char_u *r = NULL;
7805
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007808 if (!did_echo_string_emsg)
7809 {
7810 /* Only give this message once for a recursive call to avoid
7811 * flooding the user with errors. And stop iterating over lists
7812 * and dicts. */
7813 did_echo_string_emsg = TRUE;
7814 EMSG(_("E724: variable nested too deep for displaying"));
7815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007817 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 }
7819 ++recurse;
7820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 switch (tv->v_type)
7822 {
7823 case VAR_FUNC:
7824 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 r = tv->vval.v_string;
7826 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007827
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829 if (tv->vval.v_list == NULL)
7830 {
7831 *tofree = NULL;
7832 r = NULL;
7833 }
7834 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7835 {
7836 *tofree = NULL;
7837 r = (char_u *)"[...]";
7838 }
7839 else
7840 {
7841 tv->vval.v_list->lv_copyID = copyID;
7842 *tofree = list2string(tv, copyID);
7843 r = *tofree;
7844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar8c711452005-01-14 21:53:12 +00007847 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 if (tv->vval.v_dict == NULL)
7849 {
7850 *tofree = NULL;
7851 r = NULL;
7852 }
7853 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7854 {
7855 *tofree = NULL;
7856 r = (char_u *)"{...}";
7857 }
7858 else
7859 {
7860 tv->vval.v_dict->dv_copyID = copyID;
7861 *tofree = dict2string(tv, copyID);
7862 r = *tofree;
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 case VAR_STRING:
7867 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 *tofree = NULL;
7869 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 case VAR_FLOAT:
7874 *tofree = NULL;
7875 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7876 r = numbuf;
7877 break;
7878#endif
7879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884
Bram Moolenaar8502c702014-06-17 12:51:16 +02007885 if (--recurse == 0)
7886 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888}
7889
7890/*
7891 * Return a string with the string representation of a variable.
7892 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7893 * "numbuf" is used for a number.
7894 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007895 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 */
7897 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007898tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 char_u **tofree;
7901 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903{
7904 switch (tv->v_type)
7905 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 case VAR_FUNC:
7907 *tofree = string_quote(tv->vval.v_string, TRUE);
7908 return *tofree;
7909 case VAR_STRING:
7910 *tofree = string_quote(tv->vval.v_string, FALSE);
7911 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 case VAR_FLOAT:
7914 *tofree = NULL;
7915 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7916 return numbuf;
7917#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007925 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926}
7927
7928/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 * Return string "str" in ' quotes, doubling ' characters.
7930 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 */
7933 static char_u *
7934string_quote(str, function)
7935 char_u *str;
7936 int function;
7937{
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 char_u *p, *r, *s;
7940
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 len = (function ? 13 : 3);
7942 if (str != NULL)
7943 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007944 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 for (p = str; *p != NUL; mb_ptr_adv(p))
7946 if (*p == '\'')
7947 ++len;
7948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 s = r = alloc(len);
7950 if (r != NULL)
7951 {
7952 if (function)
7953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 r += 10;
7956 }
7957 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 if (str != NULL)
7960 for (p = str; *p != NUL; )
7961 {
7962 if (*p == '\'')
7963 *r++ = '\'';
7964 MB_COPY_CHAR(p, r);
7965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 if (function)
7968 *r++ = ')';
7969 *r++ = NUL;
7970 }
7971 return s;
7972}
7973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974#ifdef FEAT_FLOAT
7975/*
7976 * Convert the string "text" to a floating point number.
7977 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7978 * this always uses a decimal point.
7979 * Returns the length of the text that was consumed.
7980 */
7981 static int
7982string2float(text, value)
7983 char_u *text;
7984 float_T *value; /* result stored here */
7985{
7986 char *s = (char *)text;
7987 float_T f;
7988
7989 f = strtod(s, &s);
7990 *value = f;
7991 return (int)((char_u *)s - text);
7992}
7993#endif
7994
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Get the value of an environment variable.
7997 * "arg" is pointing to the '$'. It is advanced to after the name.
7998 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007999 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 */
8001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 int evaluate;
8006{
8007 char_u *string = NULL;
8008 int len;
8009 int cc;
8010 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008011 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013 ++*arg;
8014 name = *arg;
8015 len = get_env_len(arg);
8016 if (evaluate)
8017 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008018 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008019 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008020
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008021 cc = name[len];
8022 name[len] = NUL;
8023 /* first try vim_getenv(), fast for normal environment vars */
8024 string = vim_getenv(name, &mustfree);
8025 if (string != NULL && *string != NUL)
8026 {
8027 if (!mustfree)
8028 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008030 else
8031 {
8032 if (mustfree)
8033 vim_free(string);
8034
8035 /* next try expanding things like $VIM and ${HOME} */
8036 string = expand_env_save(name - 1);
8037 if (string != NULL && *string == '$')
8038 {
8039 vim_free(string);
8040 string = NULL;
8041 }
8042 }
8043 name[len] = cc;
8044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->v_type = VAR_STRING;
8046 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048
8049 return OK;
8050}
8051
8052/*
8053 * Array with names and number of arguments of all internal functions
8054 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8055 */
8056static struct fst
8057{
8058 char *f_name; /* function name */
8059 char f_min_argc; /* minimal number of arguments */
8060 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008062 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063} functions[] =
8064{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065#ifdef FEAT_FLOAT
8066 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008067 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008069 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008070 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"append", 2, 2, f_append},
8072 {"argc", 0, 0, f_argc},
8073 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008074 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008075 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008076#ifdef FEAT_FLOAT
8077 {"asin", 1, 1, f_asin}, /* WJMc */
8078#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008079 {"assert_equal", 2, 3, f_assert_equal},
8080 {"assert_false", 1, 2, f_assert_false},
8081 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008084 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008087 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"bufexists", 1, 1, f_bufexists},
8089 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8090 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8091 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8092 {"buflisted", 1, 1, f_buflisted},
8093 {"bufloaded", 1, 1, f_bufloaded},
8094 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008095 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"bufwinnr", 1, 1, f_bufwinnr},
8097 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008098 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008099 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"ceil", 1, 1, f_ceil},
8103#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008104 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008105 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008107 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008109#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008110 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008111 {"complete_add", 1, 1, f_complete_add},
8112 {"complete_check", 0, 0, f_complete_check},
8113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008118 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008120 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008122 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008123 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"delete", 1, 1, f_delete},
8125 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008126 {"diff_filler", 1, 1, f_diff_filler},
8127 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008128 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"eventhandler", 0, 0, f_eventhandler},
8132 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008133 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135#ifdef FEAT_FLOAT
8136 {"exp", 1, 1, f_exp},
8137#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008138 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008139 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008140 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8142 {"filereadable", 1, 1, f_filereadable},
8143 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008145 {"finddir", 1, 3, f_finddir},
8146 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"float2nr", 1, 1, f_float2nr},
8149 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008152 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"fnamemodify", 2, 2, f_fnamemodify},
8154 {"foldclosed", 1, 1, f_foldclosed},
8155 {"foldclosedend", 1, 1, f_foldclosedend},
8156 {"foldlevel", 1, 1, f_foldlevel},
8157 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008158 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008160 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008161 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008162 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008163 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008164 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"getchar", 0, 1, f_getchar},
8166 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008167 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"getcmdline", 0, 0, f_getcmdline},
8169 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008170 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008171 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008172 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008174 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008175 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getfsize", 1, 1, f_getfsize},
8177 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008178 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008180 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008181 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008182 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008183 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008184 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008185 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008187 {"gettabvar", 2, 3, f_gettabvar},
8188 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"getwinposx", 0, 0, f_getwinposx},
8190 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008191 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008192 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008193 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008194 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008197 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008198 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8200 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8201 {"histadd", 2, 2, f_histadd},
8202 {"histdel", 1, 2, f_histdel},
8203 {"histget", 1, 2, f_histget},
8204 {"histnr", 1, 1, f_histnr},
8205 {"hlID", 1, 1, f_hlID},
8206 {"hlexists", 1, 1, f_hlexists},
8207 {"hostname", 0, 0, f_hostname},
8208 {"iconv", 3, 3, f_iconv},
8209 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008211 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008213 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"inputrestore", 0, 0, f_inputrestore},
8215 {"inputsave", 0, 0, f_inputsave},
8216 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008217 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008218 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008220 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008222 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008223 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"libcall", 3, 3, f_libcall},
8227 {"libcallnr", 3, 3, f_libcallnr},
8228 {"line", 1, 1, f_line},
8229 {"line2byte", 1, 1, f_line2byte},
8230 {"lispindent", 1, 1, f_lispindent},
8231 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008233 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234 {"log10", 1, 1, f_log10},
8235#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008236#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008237 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008238#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008240 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008241 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008242 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008243 {"matchadd", 2, 5, f_matchadd},
8244 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008245 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008246 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008247 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008248 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008249 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008250 {"max", 1, 1, f_max},
8251 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008252#ifdef vim_mkdir
8253 {"mkdir", 1, 3, f_mkdir},
8254#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008256#ifdef FEAT_MZSCHEME
8257 {"mzeval", 1, 1, f_mzeval},
8258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008260 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008261 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008262 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008263#ifdef FEAT_FLOAT
8264 {"pow", 2, 2, f_pow},
8265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008267 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008268 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008269#ifdef FEAT_PYTHON3
8270 {"py3eval", 1, 1, f_py3eval},
8271#endif
8272#ifdef FEAT_PYTHON
8273 {"pyeval", 1, 1, f_pyeval},
8274#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008275 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008276 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008277 {"reltime", 0, 2, f_reltime},
8278 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"remote_expr", 2, 3, f_remote_expr},
8280 {"remote_foreground", 1, 1, f_remote_foreground},
8281 {"remote_peek", 1, 2, f_remote_peek},
8282 {"remote_read", 1, 1, f_remote_read},
8283 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008284 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008286 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008288 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008289#ifdef FEAT_FLOAT
8290 {"round", 1, 1, f_round},
8291#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008292 {"screenattr", 2, 2, f_screenattr},
8293 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008294 {"screencol", 0, 0, f_screencol},
8295 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008296 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008297 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008298 {"searchpair", 3, 7, f_searchpair},
8299 {"searchpairpos", 3, 7, f_searchpairpos},
8300 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"server2client", 2, 2, f_server2client},
8302 {"serverlist", 0, 0, f_serverlist},
8303 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008304 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"setcmdpos", 1, 1, f_setcmdpos},
8306 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008307 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008308 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008309 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008310 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008312 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008313 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008315#ifdef FEAT_CRYPT
8316 {"sha256", 1, 1, f_sha256},
8317#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008318 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008319 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#ifdef FEAT_FLOAT
8322 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008323 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008324#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008325 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008326 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008327 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008328 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008329 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330#ifdef FEAT_FLOAT
8331 {"sqrt", 1, 1, f_sqrt},
8332 {"str2float", 1, 1, f_str2float},
8333#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008334 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008335 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008336 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#ifdef HAVE_STRFTIME
8338 {"strftime", 1, 2, f_strftime},
8339#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008341 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"strlen", 1, 1, f_strlen},
8343 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008344 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008346 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008347 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"substitute", 4, 4, f_substitute},
8349 {"synID", 3, 3, f_synID},
8350 {"synIDattr", 2, 3, f_synIDattr},
8351 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008352 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008353 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008354 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008355 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008356 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008357 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008358 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008359 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008360 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008361#ifdef FEAT_FLOAT
8362 {"tan", 1, 1, f_tan},
8363 {"tanh", 1, 1, f_tanh},
8364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008366 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"tolower", 1, 1, f_tolower},
8368 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008369 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
8371 {"trunc", 1, 1, f_trunc},
8372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008374 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008375 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008376 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008377 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"virtcol", 1, 1, f_virtcol},
8379 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008380 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"winbufnr", 1, 1, f_winbufnr},
8382 {"wincol", 0, 0, f_wincol},
8383 {"winheight", 1, 1, f_winheight},
8384 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008385 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008387 {"winrestview", 1, 1, f_winrestview},
8388 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008390 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008391 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392};
8393
8394#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8395
8396/*
8397 * Function given to ExpandGeneric() to obtain the list of internal
8398 * or user defined function names.
8399 */
8400 char_u *
8401get_function_name(xp, idx)
8402 expand_T *xp;
8403 int idx;
8404{
8405 static int intidx = -1;
8406 char_u *name;
8407
8408 if (idx == 0)
8409 intidx = -1;
8410 if (intidx < 0)
8411 {
8412 name = get_user_func_name(xp, idx);
8413 if (name != NULL)
8414 return name;
8415 }
8416 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8417 {
8418 STRCPY(IObuff, functions[intidx].f_name);
8419 STRCAT(IObuff, "(");
8420 if (functions[intidx].f_max_argc == 0)
8421 STRCAT(IObuff, ")");
8422 return IObuff;
8423 }
8424
8425 return NULL;
8426}
8427
8428/*
8429 * Function given to ExpandGeneric() to obtain the list of internal or
8430 * user defined variable or function names.
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 char_u *
8433get_expr_name(xp, idx)
8434 expand_T *xp;
8435 int idx;
8436{
8437 static int intidx = -1;
8438 char_u *name;
8439
8440 if (idx == 0)
8441 intidx = -1;
8442 if (intidx < 0)
8443 {
8444 name = get_function_name(xp, idx);
8445 if (name != NULL)
8446 return name;
8447 }
8448 return get_user_var_name(xp, ++intidx);
8449}
8450
8451#endif /* FEAT_CMDL_COMPL */
8452
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008453#if defined(EBCDIC) || defined(PROTO)
8454/*
8455 * Compare struct fst by function name.
8456 */
8457 static int
8458compare_func_name(s1, s2)
8459 const void *s1;
8460 const void *s2;
8461{
8462 struct fst *p1 = (struct fst *)s1;
8463 struct fst *p2 = (struct fst *)s2;
8464
8465 return STRCMP(p1->f_name, p2->f_name);
8466}
8467
8468/*
8469 * Sort the function table by function name.
8470 * The sorting of the table above is ASCII dependant.
8471 * On machines using EBCDIC we have to sort it.
8472 */
8473 static void
8474sortFunctions()
8475{
8476 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8477
8478 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8479}
8480#endif
8481
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
8484 * Find internal function in table above.
8485 * Return index, or -1 if not found
8486 */
8487 static int
8488find_internal_func(name)
8489 char_u *name; /* name of the function */
8490{
8491 int first = 0;
8492 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8493 int cmp;
8494 int x;
8495
8496 /*
8497 * Find the function name in the table. Binary search.
8498 */
8499 while (first <= last)
8500 {
8501 x = first + ((unsigned)(last - first) >> 1);
8502 cmp = STRCMP(name, functions[x].f_name);
8503 if (cmp < 0)
8504 last = x - 1;
8505 else if (cmp > 0)
8506 first = x + 1;
8507 else
8508 return x;
8509 }
8510 return -1;
8511}
8512
8513/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8515 * name it contains, otherwise return "name".
8516 */
8517 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008518deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 char_u *name;
8520 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008521 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522{
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 int cc;
8525
8526 cc = name[*lenp];
8527 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008528 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {
8534 *lenp = 0;
8535 return (char_u *)""; /* just in case */
8536 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008537 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 }
8540
8541 return name;
8542}
8543
8544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 * Allocate a variable for the result of a function.
8546 * Return OK or FAIL.
8547 */
8548 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8550 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 char_u *name; /* name of the function */
8552 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 char_u **arg; /* argument, pointing to the '(' */
8555 linenr_T firstline; /* first line of range */
8556 linenr_T lastline; /* last line of range */
8557 int *doesrange; /* return: function handled range */
8558 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 char_u *argp;
8562 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008563 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 int argcount = 0; /* number of arguments found */
8565
8566 /*
8567 * Get the arguments.
8568 */
8569 argp = *arg;
8570 while (argcount < MAX_FUNC_ARGS)
8571 {
8572 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8573 if (*argp == ')' || *argp == ',' || *argp == NUL)
8574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8576 {
8577 ret = FAIL;
8578 break;
8579 }
8580 ++argcount;
8581 if (*argp != ',')
8582 break;
8583 }
8584 if (*argp == ')')
8585 ++argp;
8586 else
8587 ret = FAIL;
8588
8589 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008591 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 {
8594 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008595 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008597 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 *arg = skipwhite(argp);
8604 return ret;
8605}
8606
8607
8608/*
8609 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008610 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008611 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 */
8613 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008614call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008615 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008616 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008620 typval_T *argvars; /* vars for arguments, must have "argcount"
8621 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 linenr_T firstline; /* first line of range */
8623 linenr_T lastline; /* last line of range */
8624 int *doesrange; /* return: function handled range */
8625 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#define ERROR_UNKNOWN 0
8630#define ERROR_TOOMANY 1
8631#define ERROR_TOOFEW 2
8632#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633#define ERROR_DICT 4
8634#define ERROR_NONE 5
8635#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 int error = ERROR_NONE;
8637 int i;
8638 int llen;
8639 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#define FLEN_FIXED 40
8641 char_u fname_buf[FLEN_FIXED + 1];
8642 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008643 char_u *name;
8644
8645 /* Make a copy of the name, if it comes from a funcref variable it could
8646 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008647 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008648 if (name == NULL)
8649 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 /*
8652 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8653 * Change <SNR>123_name() to K_SNR 123_name().
8654 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 llen = eval_fname_script(name);
8657 if (llen > 0)
8658 {
8659 fname_buf[0] = K_SPECIAL;
8660 fname_buf[1] = KS_EXTRA;
8661 fname_buf[2] = (int)KE_SNR;
8662 i = 3;
8663 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8664 {
8665 if (current_SID <= 0)
8666 error = ERROR_SCRIPT;
8667 else
8668 {
8669 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8670 i = (int)STRLEN(fname_buf);
8671 }
8672 }
8673 if (i + STRLEN(name + llen) < FLEN_FIXED)
8674 {
8675 STRCPY(fname_buf + i, name + llen);
8676 fname = fname_buf;
8677 }
8678 else
8679 {
8680 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8681 if (fname == NULL)
8682 error = ERROR_OTHER;
8683 else
8684 {
8685 mch_memmove(fname, fname_buf, (size_t)i);
8686 STRCPY(fname + i, name + llen);
8687 }
8688 }
8689 }
8690 else
8691 fname = name;
8692
8693 *doesrange = FALSE;
8694
8695
8696 /* execute the function if no errors detected and executing */
8697 if (evaluate && error == ERROR_NONE)
8698 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 char_u *rfname = fname;
8700
8701 /* Ignore "g:" before a function name. */
8702 if (fname[0] == 'g' && fname[1] == ':')
8703 rfname = fname + 2;
8704
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008705 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8706 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 error = ERROR_UNKNOWN;
8708
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {
8711 /*
8712 * User defined function.
8713 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717 /* Trigger FuncUndefined event, may load the function. */
8718 if (fp == NULL
8719 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008723 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008724 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
8726#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 {
8730 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008731 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008732 }
8733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (fp != NULL)
8735 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008736 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 else
8745 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008746 int did_save_redo = FALSE;
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 /*
8749 * Call the user function.
8750 * Save and restore search patterns, script variables and
8751 * redo buffer.
8752 */
8753 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008754#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008756#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008757 {
8758 saveRedobuff();
8759 did_save_redo = TRUE;
8760 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008761 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008763 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008764 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8765 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8766 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008767 /* Function was unreferenced while being used, free it
8768 * now. */
8769 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008770 if (did_save_redo)
8771 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 restore_search_patterns();
8773 error = ERROR_NONE;
8774 }
8775 }
8776 }
8777 else
8778 {
8779 /*
8780 * Find the function name in the table, call its implementation.
8781 */
8782 i = find_internal_func(fname);
8783 if (i >= 0)
8784 {
8785 if (argcount < functions[i].f_min_argc)
8786 error = ERROR_TOOFEW;
8787 else if (argcount > functions[i].f_max_argc)
8788 error = ERROR_TOOMANY;
8789 else
8790 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008791 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 error = ERROR_NONE;
8794 }
8795 }
8796 }
8797 /*
8798 * The function call (or "FuncUndefined" autocommand sequence) might
8799 * have been aborted by an error, an interrupt, or an explicitly thrown
8800 * exception that has not been caught so far. This situation can be
8801 * tested for by calling aborting(). For an error in an internal
8802 * function or for the "E132" error in call_user_func(), however, the
8803 * throw point at which the "force_abort" flag (temporarily reset by
8804 * emsg()) is normally updated has not been reached yet. We need to
8805 * update that flag first to make aborting() reliable.
8806 */
8807 update_force_abort();
8808 }
8809 if (error == ERROR_NONE)
8810 ret = OK;
8811
8812 /*
8813 * Report an error unless the argument evaluation or function call has been
8814 * cancelled due to an aborting error, an interrupt, or an exception.
8815 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 if (!aborting())
8817 {
8818 switch (error)
8819 {
8820 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008821 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008822 break;
8823 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008824 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008825 break;
8826 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008827 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008828 name);
8829 break;
8830 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008831 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 name);
8833 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008836 name);
8837 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008838 }
8839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (fname != name && fname != fname_buf)
8842 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008843 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 return ret;
8846}
8847
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008848/*
8849 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008850 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008851 */
8852 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008853emsg_funcname(ermsg, name)
8854 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008855 char_u *name;
8856{
8857 char_u *p;
8858
8859 if (*name == K_SPECIAL)
8860 p = concat_str((char_u *)"<SNR>", name + 3);
8861 else
8862 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008863 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008864 if (p != name)
8865 vim_free(p);
8866}
8867
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008868/*
8869 * Return TRUE for a non-zero Number and a non-empty String.
8870 */
8871 static int
8872non_zero_arg(argvars)
8873 typval_T *argvars;
8874{
8875 return ((argvars[0].v_type == VAR_NUMBER
8876 && argvars[0].vval.v_number != 0)
8877 || (argvars[0].v_type == VAR_STRING
8878 && argvars[0].vval.v_string != NULL
8879 && *argvars[0].vval.v_string != NUL));
8880}
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882/*********************************************
8883 * Implementation of the built-in functions
8884 */
8885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008887static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8888
8889/*
8890 * Get the float value of "argvars[0]" into "f".
8891 * Returns FAIL when the argument is not a Number or Float.
8892 */
8893 static int
8894get_float_arg(argvars, f)
8895 typval_T *argvars;
8896 float_T *f;
8897{
8898 if (argvars[0].v_type == VAR_FLOAT)
8899 {
8900 *f = argvars[0].vval.v_float;
8901 return OK;
8902 }
8903 if (argvars[0].v_type == VAR_NUMBER)
8904 {
8905 *f = (float_T)argvars[0].vval.v_number;
8906 return OK;
8907 }
8908 EMSG(_("E808: Number or Float required"));
8909 return FAIL;
8910}
8911
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008912/*
8913 * "abs(expr)" function
8914 */
8915 static void
8916f_abs(argvars, rettv)
8917 typval_T *argvars;
8918 typval_T *rettv;
8919{
8920 if (argvars[0].v_type == VAR_FLOAT)
8921 {
8922 rettv->v_type = VAR_FLOAT;
8923 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8924 }
8925 else
8926 {
8927 varnumber_T n;
8928 int error = FALSE;
8929
8930 n = get_tv_number_chk(&argvars[0], &error);
8931 if (error)
8932 rettv->vval.v_number = -1;
8933 else if (n > 0)
8934 rettv->vval.v_number = n;
8935 else
8936 rettv->vval.v_number = -n;
8937 }
8938}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008939
8940/*
8941 * "acos()" function
8942 */
8943 static void
8944f_acos(argvars, rettv)
8945 typval_T *argvars;
8946 typval_T *rettv;
8947{
8948 float_T f;
8949
8950 rettv->v_type = VAR_FLOAT;
8951 if (get_float_arg(argvars, &f) == OK)
8952 rettv->vval.v_float = acos(f);
8953 else
8954 rettv->vval.v_float = 0.0;
8955}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956#endif
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 */
8961 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008962f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008971 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008972 && !tv_check_lock(l->lv_lock,
8973 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008974 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 }
8977 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978 EMSG(_(e_listreq));
8979}
8980
8981/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008982 * "and(expr, expr)" function
8983 */
8984 static void
8985f_and(argvars, rettv)
8986 typval_T *argvars;
8987 typval_T *rettv;
8988{
8989 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8990 & get_tv_number_chk(&argvars[1], NULL);
8991}
8992
8993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 * "append(lnum, string/list)" function
8995 */
8996 static void
8997f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009000{
9001 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 list_T *l = NULL;
9004 listitem_T *li = NULL;
9005 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 long added = 0;
9007
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009008 /* When coming here from Insert mode, sync undo, so that this can be
9009 * undone separately from what was previously inserted. */
9010 if (u_sync_once == 2)
9011 {
9012 u_sync_once = 1; /* notify that u_sync() was called */
9013 u_sync(TRUE);
9014 }
9015
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 lnum = get_tv_lnum(argvars);
9017 if (lnum >= 0
9018 && lnum <= curbuf->b_ml.ml_line_count
9019 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 l = argvars[1].vval.v_list;
9024 if (l == NULL)
9025 return;
9026 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 for (;;)
9029 {
9030 if (l == NULL)
9031 tv = &argvars[1]; /* append a string */
9032 else if (li == NULL)
9033 break; /* end of list */
9034 else
9035 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 line = get_tv_string_chk(tv);
9037 if (line == NULL) /* type error */
9038 {
9039 rettv->vval.v_number = 1; /* Failed */
9040 break;
9041 }
9042 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043 ++added;
9044 if (l == NULL)
9045 break;
9046 li = li->li_next;
9047 }
9048
9049 appended_lines_mark(lnum, added);
9050 if (curwin->w_cursor.lnum > lnum)
9051 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 else
9054 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
9057/*
9058 * "argc()" function
9059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
9067
9068/*
9069 * "argidx()" function
9070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077}
9078
9079/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009080 * "arglistid()" function
9081 */
9082 static void
9083f_arglistid(argvars, rettv)
9084 typval_T *argvars UNUSED;
9085 typval_T *rettv;
9086{
9087 win_T *wp;
9088 tabpage_T *tp = NULL;
9089 long n;
9090
9091 rettv->vval.v_number = -1;
9092 if (argvars[0].v_type != VAR_UNKNOWN)
9093 {
9094 if (argvars[1].v_type != VAR_UNKNOWN)
9095 {
9096 n = get_tv_number(&argvars[1]);
9097 if (n >= 0)
9098 tp = find_tabpage(n);
9099 }
9100 else
9101 tp = curtab;
9102
9103 if (tp != NULL)
9104 {
9105 wp = find_win_by_nr(&argvars[0], tp);
9106 if (wp != NULL)
9107 rettv->vval.v_number = wp->w_alist->id;
9108 }
9109 }
9110 else
9111 rettv->vval.v_number = curwin->w_alist->id;
9112}
9113
9114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 * "argv(nr)" function
9116 */
9117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *argvars;
9120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 int idx;
9123
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009124 if (argvars[0].v_type != VAR_UNKNOWN)
9125 {
9126 idx = get_tv_number_chk(&argvars[0], NULL);
9127 if (idx >= 0 && idx < ARGCOUNT)
9128 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9129 else
9130 rettv->vval.v_string = NULL;
9131 rettv->v_type = VAR_STRING;
9132 }
9133 else if (rettv_list_alloc(rettv) == OK)
9134 for (idx = 0; idx < ARGCOUNT; ++idx)
9135 list_append_string(rettv->vval.v_list,
9136 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009139static void prepare_assert_error __ARGS((garray_T*gap));
9140static 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));
9141static void assert_error __ARGS((garray_T *gap));
9142static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9143
9144/*
9145 * Prepare "gap" for an assert error and add the sourcing position.
9146 */
9147 static void
9148prepare_assert_error(gap)
9149 garray_T *gap;
9150{
9151 char buf[NUMBUFLEN];
9152
9153 ga_init2(gap, 1, 100);
9154 ga_concat(gap, sourcing_name);
9155 sprintf(buf, " line %ld", (long)sourcing_lnum);
9156 ga_concat(gap, (char_u *)buf);
9157 ga_concat(gap, (char_u *)": ");
9158}
9159
9160/*
9161 * Fill "gap" with information about an assert error.
9162 */
9163 static void
9164fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9165 garray_T *gap;
9166 typval_T *opt_msg_tv;
9167 char_u *exp_str;
9168 typval_T *exp_tv;
9169 typval_T *got_tv;
9170{
9171 char_u numbuf[NUMBUFLEN];
9172 char_u *tofree;
9173
9174 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9175 {
9176 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9177 vim_free(tofree);
9178 }
9179 else
9180 {
9181 ga_concat(gap, (char_u *)"Expected ");
9182 if (exp_str == NULL)
9183 {
9184 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9185 vim_free(tofree);
9186 }
9187 else
9188 ga_concat(gap, exp_str);
9189 ga_concat(gap, (char_u *)" but got ");
9190 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9191 vim_free(tofree);
9192 }
9193}
Bram Moolenaar43345542015-11-29 17:35:35 +01009194
9195/*
9196 * Add an assert error to v:errors.
9197 */
9198 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009199assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009200 garray_T *gap;
9201{
9202 struct vimvar *vp = &vimvars[VV_ERRORS];
9203
9204 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9205 /* Make sure v:errors is a list. */
9206 set_vim_var_list(VV_ERRORS, list_alloc());
9207 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9208}
9209
Bram Moolenaar43345542015-11-29 17:35:35 +01009210/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009211 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009212 */
9213 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009214f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009215 typval_T *argvars;
9216 typval_T *rettv UNUSED;
9217{
9218 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009219
9220 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9221 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009222 prepare_assert_error(&ga);
9223 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9224 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009225 ga_clear(&ga);
9226 }
9227}
9228
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009229/*
9230 * Common for assert_true() and assert_false().
9231 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009232 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009234 typval_T *argvars;
9235 int isTrue;
9236{
9237 int error = FALSE;
9238 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009239
9240 if (argvars[0].v_type != VAR_NUMBER
9241 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9242 || error)
9243 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244 prepare_assert_error(&ga);
9245 fill_assert_error(&ga, &argvars[1],
9246 (char_u *)(isTrue ? "True " : "False "),
9247 NULL, &argvars[0]);
9248 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009249 ga_clear(&ga);
9250 }
9251}
9252
9253/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009254 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009255 */
9256 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009257f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009258 typval_T *argvars;
9259 typval_T *rettv UNUSED;
9260{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009261 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009262}
9263
9264/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009266 */
9267 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009269 typval_T *argvars;
9270 typval_T *rettv UNUSED;
9271{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009273}
9274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009275#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009276/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009277 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009278 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009279 static void
9280f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009281 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009282 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009283{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009284 float_T f;
9285
9286 rettv->v_type = VAR_FLOAT;
9287 if (get_float_arg(argvars, &f) == OK)
9288 rettv->vval.v_float = asin(f);
9289 else
9290 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009291}
9292
9293/*
9294 * "atan()" function
9295 */
9296 static void
9297f_atan(argvars, rettv)
9298 typval_T *argvars;
9299 typval_T *rettv;
9300{
9301 float_T f;
9302
9303 rettv->v_type = VAR_FLOAT;
9304 if (get_float_arg(argvars, &f) == OK)
9305 rettv->vval.v_float = atan(f);
9306 else
9307 rettv->vval.v_float = 0.0;
9308}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009309
9310/*
9311 * "atan2()" function
9312 */
9313 static void
9314f_atan2(argvars, rettv)
9315 typval_T *argvars;
9316 typval_T *rettv;
9317{
9318 float_T fx, fy;
9319
9320 rettv->v_type = VAR_FLOAT;
9321 if (get_float_arg(argvars, &fx) == OK
9322 && get_float_arg(&argvars[1], &fy) == OK)
9323 rettv->vval.v_float = atan2(fx, fy);
9324 else
9325 rettv->vval.v_float = 0.0;
9326}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009327#endif
9328
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329/*
9330 * "browse(save, title, initdir, default)" function
9331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337#ifdef FEAT_BROWSE
9338 int save;
9339 char_u *title;
9340 char_u *initdir;
9341 char_u *defname;
9342 char_u buf[NUMBUFLEN];
9343 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 save = get_tv_number_chk(&argvars[0], &error);
9347 title = get_tv_string_chk(&argvars[1]);
9348 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9349 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 if (error || title == NULL || initdir == NULL || defname == NULL)
9352 rettv->vval.v_string = NULL;
9353 else
9354 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009355 do_browse(save ? BROWSE_SAVE : 0,
9356 title, defname, NULL, initdir, NULL, curbuf);
9357#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009359#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009361}
9362
9363/*
9364 * "browsedir(title, initdir)" function
9365 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009367f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009368 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009370{
9371#ifdef FEAT_BROWSE
9372 char_u *title;
9373 char_u *initdir;
9374 char_u buf[NUMBUFLEN];
9375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 title = get_tv_string_chk(&argvars[0]);
9377 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 if (title == NULL || initdir == NULL)
9380 rettv->vval.v_string = NULL;
9381 else
9382 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009383 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
Bram Moolenaar33570922005-01-25 22:26:29 +00009390static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009391
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392/*
9393 * Find a buffer by number or exact name.
9394 */
9395 static buf_T *
9396find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009401 if (avar->v_type == VAR_NUMBER)
9402 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009403 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009405 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009406 if (buf == NULL)
9407 {
9408 /* No full path name match, try a match with a URL or a "nofile"
9409 * buffer, these don't use the full path. */
9410 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9411 if (buf->b_fname != NULL
9412 && (path_with_url(buf->b_fname)
9413#ifdef FEAT_QUICKFIX
9414 || bt_nofile(buf)
9415#endif
9416 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009417 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009418 break;
9419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
9421 return buf;
9422}
9423
9424/*
9425 * "bufexists(expr)" function
9426 */
9427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 typval_T *argvars;
9430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434
9435/*
9436 * "buflisted(expr)" function
9437 */
9438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 buf_T *buf;
9444
9445 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447}
9448
9449/*
9450 * "bufloaded(expr)" function
9451 */
9452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 buf_T *buf;
9458
9459 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009463static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465/*
9466 * Get buffer by number or pattern.
9467 */
9468 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009469get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009471 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 int save_magic;
9475 char_u *save_cpo;
9476 buf_T *buf;
9477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 if (tv->v_type == VAR_NUMBER)
9479 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009480 if (tv->v_type != VAR_STRING)
9481 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 if (name == NULL || *name == NUL)
9483 return curbuf;
9484 if (name[0] == '$' && name[1] == NUL)
9485 return lastbuf;
9486
9487 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9488 save_magic = p_magic;
9489 p_magic = TRUE;
9490 save_cpo = p_cpo;
9491 p_cpo = (char_u *)"";
9492
9493 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009494 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
9496 p_magic = save_magic;
9497 p_cpo = save_cpo;
9498
9499 /* If not found, try expanding the name, like done for bufexists(). */
9500 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
9503 return buf;
9504}
9505
9506/*
9507 * "bufname(expr)" function
9508 */
9509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009511 typval_T *argvars;
9512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514 buf_T *buf;
9515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009516 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009518 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 --emsg_off;
9525}
9526
9527/*
9528 * "bufnr(expr)" function
9529 */
9530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 typval_T *argvars;
9533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009536 int error = FALSE;
9537 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009541 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009542 --emsg_off;
9543
9544 /* If the buffer isn't found and the second argument is not zero create a
9545 * new buffer. */
9546 if (buf == NULL
9547 && argvars[1].v_type != VAR_UNKNOWN
9548 && get_tv_number_chk(&argvars[1], &error) != 0
9549 && !error
9550 && (name = get_tv_string_chk(&argvars[0])) != NULL
9551 && !error)
9552 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558}
9559
9560/*
9561 * "bufwinnr(nr)" function
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568#ifdef FEAT_WINDOWS
9569 win_T *wp;
9570 int winnr = 0;
9571#endif
9572 buf_T *buf;
9573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009576 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577#ifdef FEAT_WINDOWS
9578 for (wp = firstwin; wp; wp = wp->w_next)
9579 {
9580 ++winnr;
9581 if (wp->w_buffer == buf)
9582 break;
9583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587#endif
9588 --emsg_off;
9589}
9590
9591/*
9592 * "byte2line(byte)" function
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009596 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#else
9602 long boff = 0;
9603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 (linenr_T)0, &boff);
9610#endif
9611}
9612
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009613 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009614byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009617 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009618{
9619#ifdef FEAT_MBYTE
9620 char_u *t;
9621#endif
9622 char_u *str;
9623 long idx;
9624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 str = get_tv_string_chk(&argvars[0]);
9626 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009628 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009629 return;
9630
9631#ifdef FEAT_MBYTE
9632 t = str;
9633 for ( ; idx > 0; idx--)
9634 {
9635 if (*t == NUL) /* EOL reached */
9636 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009637 if (enc_utf8 && comp)
9638 t += utf_ptr2len(t);
9639 else
9640 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009641 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009642 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009643#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009644 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009646#endif
9647}
9648
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009649/*
9650 * "byteidx()" function
9651 */
9652 static void
9653f_byteidx(argvars, rettv)
9654 typval_T *argvars;
9655 typval_T *rettv;
9656{
9657 byteidx(argvars, rettv, FALSE);
9658}
9659
9660/*
9661 * "byteidxcomp()" function
9662 */
9663 static void
9664f_byteidxcomp(argvars, rettv)
9665 typval_T *argvars;
9666 typval_T *rettv;
9667{
9668 byteidx(argvars, rettv, TRUE);
9669}
9670
Bram Moolenaardb913952012-06-29 12:54:53 +02009671 int
9672func_call(name, args, selfdict, rettv)
9673 char_u *name;
9674 typval_T *args;
9675 dict_T *selfdict;
9676 typval_T *rettv;
9677{
9678 listitem_T *item;
9679 typval_T argv[MAX_FUNC_ARGS + 1];
9680 int argc = 0;
9681 int dummy;
9682 int r = 0;
9683
9684 for (item = args->vval.v_list->lv_first; item != NULL;
9685 item = item->li_next)
9686 {
9687 if (argc == MAX_FUNC_ARGS)
9688 {
9689 EMSG(_("E699: Too many arguments"));
9690 break;
9691 }
9692 /* Make a copy of each argument. This is needed to be able to set
9693 * v_lock to VAR_FIXED in the copy without changing the original list.
9694 */
9695 copy_tv(&item->li_tv, &argv[argc++]);
9696 }
9697
9698 if (item == NULL)
9699 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9701 &dummy, TRUE, selfdict);
9702
9703 /* Free the arguments. */
9704 while (argc > 0)
9705 clear_tv(&argv[--argc]);
9706
9707 return r;
9708}
9709
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009710/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009711 * "call(func, arglist)" function
9712 */
9713 static void
9714f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 typval_T *argvars;
9716 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009717{
9718 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009721 if (argvars[1].v_type != VAR_LIST)
9722 {
9723 EMSG(_(e_listreq));
9724 return;
9725 }
9726 if (argvars[1].vval.v_list == NULL)
9727 return;
9728
9729 if (argvars[0].v_type == VAR_FUNC)
9730 func = argvars[0].vval.v_string;
9731 else
9732 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 if (*func == NUL)
9734 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009735
Bram Moolenaare9a41262005-01-15 22:18:47 +00009736 if (argvars[2].v_type != VAR_UNKNOWN)
9737 {
9738 if (argvars[2].v_type != VAR_DICT)
9739 {
9740 EMSG(_(e_dictreq));
9741 return;
9742 }
9743 selfdict = argvars[2].vval.v_dict;
9744 }
9745
Bram Moolenaardb913952012-06-29 12:54:53 +02009746 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009747}
9748
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009749#ifdef FEAT_FLOAT
9750/*
9751 * "ceil({float})" function
9752 */
9753 static void
9754f_ceil(argvars, rettv)
9755 typval_T *argvars;
9756 typval_T *rettv;
9757{
9758 float_T f;
9759
9760 rettv->v_type = VAR_FLOAT;
9761 if (get_float_arg(argvars, &f) == OK)
9762 rettv->vval.v_float = ceil(f);
9763 else
9764 rettv->vval.v_float = 0.0;
9765}
9766#endif
9767
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009768/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009769 * "changenr()" function
9770 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009771 static void
9772f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009773 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009774 typval_T *rettv;
9775{
9776 rettv->vval.v_number = curbuf->b_u_seq_cur;
9777}
9778
9779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 * "char2nr(string)" function
9781 */
9782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787#ifdef FEAT_MBYTE
9788 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009789 {
9790 int utf8 = 0;
9791
9792 if (argvars[1].v_type != VAR_UNKNOWN)
9793 utf8 = get_tv_number_chk(&argvars[1], NULL);
9794
9795 if (utf8)
9796 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9797 else
9798 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 else
9801#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
9806 * "cindent(lnum)" function
9807 */
9808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813#ifdef FEAT_CINDENT
9814 pos_T pos;
9815 linenr_T lnum;
9816
9817 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9820 {
9821 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 curwin->w_cursor = pos;
9824 }
9825 else
9826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828}
9829
9830/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009831 * "clearmatches()" function
9832 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009833 static void
9834f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009835 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009836 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009837{
9838#ifdef FEAT_SEARCH_EXTRA
9839 clear_matches(curwin);
9840#endif
9841}
9842
9843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 * "col(string)" function
9845 */
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
9851 colnr_T col = 0;
9852 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009853 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009855 fp = var2fpos(&argvars[0], FALSE, &fnum);
9856 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 {
9858 if (fp->col == MAXCOL)
9859 {
9860 /* '> can be MAXCOL, get the length of the line then */
9861 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009862 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 else
9864 col = MAXCOL;
9865 }
9866 else
9867 {
9868 col = fp->col + 1;
9869#ifdef FEAT_VIRTUALEDIT
9870 /* col(".") when the cursor is on the NUL at the end of the line
9871 * because of "coladd" can be seen as an extra column. */
9872 if (virtual_active() && fp == &curwin->w_cursor)
9873 {
9874 char_u *p = ml_get_cursor();
9875
9876 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9877 curwin->w_virtcol - curwin->w_cursor.coladd))
9878 {
9879# ifdef FEAT_MBYTE
9880 int l;
9881
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009882 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 col += l;
9884# else
9885 if (*p != NUL && p[1] == NUL)
9886 ++col;
9887# endif
9888 }
9889 }
9890#endif
9891 }
9892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
Bram Moolenaar572cb562005-08-05 21:35:02 +00009896#if defined(FEAT_INS_EXPAND)
9897/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009898 * "complete()" function
9899 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009900 static void
9901f_complete(argvars, rettv)
9902 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009903 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009904{
9905 int startcol;
9906
9907 if ((State & INSERT) == 0)
9908 {
9909 EMSG(_("E785: complete() can only be used in Insert mode"));
9910 return;
9911 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009912
9913 /* Check for undo allowed here, because if something was already inserted
9914 * the line was already saved for undo and this check isn't done. */
9915 if (!undo_allowed())
9916 return;
9917
Bram Moolenaarade00832006-03-10 21:46:58 +00009918 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9919 {
9920 EMSG(_(e_invarg));
9921 return;
9922 }
9923
9924 startcol = get_tv_number_chk(&argvars[0], NULL);
9925 if (startcol <= 0)
9926 return;
9927
9928 set_completion(startcol - 1, argvars[1].vval.v_list);
9929}
9930
9931/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009932 * "complete_add()" function
9933 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009934 static void
9935f_complete_add(argvars, rettv)
9936 typval_T *argvars;
9937 typval_T *rettv;
9938{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009939 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009940}
9941
9942/*
9943 * "complete_check()" function
9944 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009945 static void
9946f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009947 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009948 typval_T *rettv;
9949{
9950 int saved = RedrawingDisabled;
9951
9952 RedrawingDisabled = 0;
9953 ins_compl_check_keys(0);
9954 rettv->vval.v_number = compl_interrupted;
9955 RedrawingDisabled = saved;
9956}
9957#endif
9958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959/*
9960 * "confirm(message, buttons[, default [, type]])" function
9961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009964 typval_T *argvars UNUSED;
9965 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
9967#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9968 char_u *message;
9969 char_u *buttons = NULL;
9970 char_u buf[NUMBUFLEN];
9971 char_u buf2[NUMBUFLEN];
9972 int def = 1;
9973 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 char_u *typestr;
9975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009977 message = get_tv_string_chk(&argvars[0]);
9978 if (message == NULL)
9979 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009980 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9983 if (buttons == NULL)
9984 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009987 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9991 if (typestr == NULL)
9992 error = TRUE;
9993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009995 switch (TOUPPER_ASC(*typestr))
9996 {
9997 case 'E': type = VIM_ERROR; break;
9998 case 'Q': type = VIM_QUESTION; break;
9999 case 'I': type = VIM_INFO; break;
10000 case 'W': type = VIM_WARNING; break;
10001 case 'G': type = VIM_GENERIC; break;
10002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 }
10004 }
10005 }
10006 }
10007
10008 if (buttons == NULL || *buttons == NUL)
10009 buttons = (char_u *)_("&Ok");
10010
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010011 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010013 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#endif
10015}
10016
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010017/*
10018 * "copy()" function
10019 */
10020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010022 typval_T *argvars;
10023 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010024{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010025 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010026}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010028#ifdef FEAT_FLOAT
10029/*
10030 * "cos()" function
10031 */
10032 static void
10033f_cos(argvars, rettv)
10034 typval_T *argvars;
10035 typval_T *rettv;
10036{
10037 float_T f;
10038
10039 rettv->v_type = VAR_FLOAT;
10040 if (get_float_arg(argvars, &f) == OK)
10041 rettv->vval.v_float = cos(f);
10042 else
10043 rettv->vval.v_float = 0.0;
10044}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010045
10046/*
10047 * "cosh()" function
10048 */
10049 static void
10050f_cosh(argvars, rettv)
10051 typval_T *argvars;
10052 typval_T *rettv;
10053{
10054 float_T f;
10055
10056 rettv->v_type = VAR_FLOAT;
10057 if (get_float_arg(argvars, &f) == OK)
10058 rettv->vval.v_float = cosh(f);
10059 else
10060 rettv->vval.v_float = 0.0;
10061}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010062#endif
10063
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010065 * "count()" function
10066 */
10067 static void
10068f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010071{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010072 long n = 0;
10073 int ic = FALSE;
10074
Bram Moolenaare9a41262005-01-15 22:18:47 +000010075 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010076 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010077 listitem_T *li;
10078 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010079 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010080
Bram Moolenaare9a41262005-01-15 22:18:47 +000010081 if ((l = argvars[0].vval.v_list) != NULL)
10082 {
10083 li = l->lv_first;
10084 if (argvars[2].v_type != VAR_UNKNOWN)
10085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 int error = FALSE;
10087
10088 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010089 if (argvars[3].v_type != VAR_UNKNOWN)
10090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 idx = get_tv_number_chk(&argvars[3], &error);
10092 if (!error)
10093 {
10094 li = list_find(l, idx);
10095 if (li == NULL)
10096 EMSGN(_(e_listidx), idx);
10097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010098 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 if (error)
10100 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010101 }
10102
10103 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010104 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010105 ++n;
10106 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010108 else if (argvars[0].v_type == VAR_DICT)
10109 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010110 int todo;
10111 dict_T *d;
10112 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010114 if ((d = argvars[0].vval.v_dict) != NULL)
10115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 int error = FALSE;
10117
Bram Moolenaare9a41262005-01-15 22:18:47 +000010118 if (argvars[2].v_type != VAR_UNKNOWN)
10119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010121 if (argvars[3].v_type != VAR_UNKNOWN)
10122 EMSG(_(e_invarg));
10123 }
10124
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010125 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010126 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010127 {
10128 if (!HASHITEM_EMPTY(hi))
10129 {
10130 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010131 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010132 ++n;
10133 }
10134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010135 }
10136 }
10137 else
10138 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010139 rettv->vval.v_number = n;
10140}
10141
10142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10144 *
10145 * Checks the existence of a cscope connection.
10146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010149 typval_T *argvars UNUSED;
10150 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152#ifdef FEAT_CSCOPE
10153 int num = 0;
10154 char_u *dbpath = NULL;
10155 char_u *prepend = NULL;
10156 char_u buf[NUMBUFLEN];
10157
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010158 if (argvars[0].v_type != VAR_UNKNOWN
10159 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 num = (int)get_tv_number(&argvars[0]);
10162 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010163 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 }
10166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168#endif
10169}
10170
10171/*
10172 * "cursor(lnum, col)" function
10173 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010174 * Moves the cursor to the specified line and column.
10175 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010183#ifdef FEAT_VIRTUALEDIT
10184 long coladd = 0;
10185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010187 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010188 if (argvars[1].v_type == VAR_UNKNOWN)
10189 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010190 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010191 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010192
Bram Moolenaar493c1782014-05-28 14:34:46 +020010193 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010194 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010195 line = pos.lnum;
10196 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010197#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010198 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010199#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010200 if (curswant >= 0)
10201 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010202 }
10203 else
10204 {
10205 line = get_tv_lnum(argvars);
10206 col = get_tv_number_chk(&argvars[1], NULL);
10207#ifdef FEAT_VIRTUALEDIT
10208 if (argvars[2].v_type != VAR_UNKNOWN)
10209 coladd = get_tv_number_chk(&argvars[2], NULL);
10210#endif
10211 }
10212 if (line < 0 || col < 0
10213#ifdef FEAT_VIRTUALEDIT
10214 || coladd < 0
10215#endif
10216 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 if (line > 0)
10219 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 if (col > 0)
10221 curwin->w_cursor.col = col - 1;
10222#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010223 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224#endif
10225
10226 /* Make sure the cursor is in a valid position. */
10227 check_cursor();
10228#ifdef FEAT_MBYTE
10229 /* Correct cursor for multi-byte character. */
10230 if (has_mbyte)
10231 mb_adjust_cursor();
10232#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010233
10234 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010235 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236}
10237
10238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010239 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010246 int noref = 0;
10247
10248 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010250 if (noref < 0 || noref > 1)
10251 EMSG(_(e_invarg));
10252 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010253 {
10254 current_copyID += COPYID_INC;
10255 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
10259/*
10260 * "delete()" function
10261 */
10262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010264 typval_T *argvars;
10265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266{
10267 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271}
10272
10273/*
10274 * "did_filetype()" function
10275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010278 typval_T *argvars UNUSED;
10279 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280{
10281#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283#endif
10284}
10285
10286/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010287 * "diff_filler()" function
10288 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010291 typval_T *argvars UNUSED;
10292 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010293{
10294#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010296#endif
10297}
10298
10299/*
10300 * "diff_hlID()" function
10301 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010304 typval_T *argvars UNUSED;
10305 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010306{
10307#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010309 static linenr_T prev_lnum = 0;
10310 static int changedtick = 0;
10311 static int fnum = 0;
10312 static int change_start = 0;
10313 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010314 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010315 int filler_lines;
10316 int col;
10317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 if (lnum < 0) /* ignore type error in {lnum} arg */
10319 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010320 if (lnum != prev_lnum
10321 || changedtick != curbuf->b_changedtick
10322 || fnum != curbuf->b_fnum)
10323 {
10324 /* New line, buffer, change: need to get the values. */
10325 filler_lines = diff_check(curwin, lnum);
10326 if (filler_lines < 0)
10327 {
10328 if (filler_lines == -1)
10329 {
10330 change_start = MAXCOL;
10331 change_end = -1;
10332 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10333 hlID = HLF_ADD; /* added line */
10334 else
10335 hlID = HLF_CHD; /* changed line */
10336 }
10337 else
10338 hlID = HLF_ADD; /* added line */
10339 }
10340 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010341 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010342 prev_lnum = lnum;
10343 changedtick = curbuf->b_changedtick;
10344 fnum = curbuf->b_fnum;
10345 }
10346
10347 if (hlID == HLF_CHD || hlID == HLF_TXD)
10348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010350 if (col >= change_start && col <= change_end)
10351 hlID = HLF_TXD; /* changed text */
10352 else
10353 hlID = HLF_CHD; /* changed line */
10354 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010355 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010356#endif
10357}
10358
10359/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010360 * "empty({expr})" function
10361 */
10362 static void
10363f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010364 typval_T *argvars;
10365 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010366{
10367 int n;
10368
10369 switch (argvars[0].v_type)
10370 {
10371 case VAR_STRING:
10372 case VAR_FUNC:
10373 n = argvars[0].vval.v_string == NULL
10374 || *argvars[0].vval.v_string == NUL;
10375 break;
10376 case VAR_NUMBER:
10377 n = argvars[0].vval.v_number == 0;
10378 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010379#ifdef FEAT_FLOAT
10380 case VAR_FLOAT:
10381 n = argvars[0].vval.v_float == 0.0;
10382 break;
10383#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010384 case VAR_LIST:
10385 n = argvars[0].vval.v_list == NULL
10386 || argvars[0].vval.v_list->lv_first == NULL;
10387 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 case VAR_DICT:
10389 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010390 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010392 default:
10393 EMSG2(_(e_intern2), "f_empty()");
10394 n = 0;
10395 }
10396
10397 rettv->vval.v_number = n;
10398}
10399
10400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 * "escape({string}, {chars})" function
10402 */
10403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *argvars;
10406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
10408 char_u buf[NUMBUFLEN];
10409
Bram Moolenaar758711c2005-02-02 23:11:38 +000010410 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10411 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413}
10414
10415/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010416 * "eval()" function
10417 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010418 static void
10419f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010420 typval_T *argvars;
10421 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010422{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010423 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 s = get_tv_string_chk(&argvars[0]);
10426 if (s != NULL)
10427 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010428
Bram Moolenaar615b9972015-01-14 17:15:05 +010010429 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10431 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010432 if (p != NULL && !aborting())
10433 EMSG2(_(e_invexpr2), p);
10434 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010436 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010438 else if (*s != NUL)
10439 EMSG(_(e_trailing));
10440}
10441
10442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * "eventhandler()" function
10444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010447 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451}
10452
10453/*
10454 * "executable()" function
10455 */
10456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010458 typval_T *argvars;
10459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010461 char_u *name = get_tv_string(&argvars[0]);
10462
10463 /* Check in $PATH and also check directly if there is a directory name. */
10464 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10465 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010466}
10467
10468/*
10469 * "exepath()" function
10470 */
10471 static void
10472f_exepath(argvars, rettv)
10473 typval_T *argvars;
10474 typval_T *rettv;
10475{
10476 char_u *p = NULL;
10477
Bram Moolenaarb5971142015-03-21 17:32:19 +010010478 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010479 rettv->v_type = VAR_STRING;
10480 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481}
10482
10483/*
10484 * "exists()" function
10485 */
10486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
10491 char_u *p;
10492 char_u *name;
10493 int n = FALSE;
10494 int len = 0;
10495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 if (*p == '$') /* environment variable */
10498 {
10499 /* first try "normal" environment variables (fast) */
10500 if (mch_getenv(p + 1) != NULL)
10501 n = TRUE;
10502 else
10503 {
10504 /* try expanding things like $VIM and ${HOME} */
10505 p = expand_env_save(p);
10506 if (p != NULL && *p != '$')
10507 n = TRUE;
10508 vim_free(p);
10509 }
10510 }
10511 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010514 if (*skipwhite(p) != NUL)
10515 n = FALSE; /* trailing garbage */
10516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 else if (*p == '*') /* internal or user defined function */
10518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010519 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 }
10521 else if (*p == ':')
10522 {
10523 n = cmd_exists(p + 1);
10524 }
10525 else if (*p == '#')
10526 {
10527#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010528 if (p[1] == '#')
10529 n = autocmd_supported(p + 2);
10530 else
10531 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#endif
10533 }
10534 else /* internal variable */
10535 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010536 char_u *tofree;
10537 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010539 /* get_name_len() takes care of expanding curly braces */
10540 name = p;
10541 len = get_name_len(&p, &tofree, TRUE, FALSE);
10542 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010544 if (tofree != NULL)
10545 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010546 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010547 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010549 /* handle d.key, l[idx], f(expr) */
10550 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10551 if (n)
10552 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 }
10554 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010555 if (*p != NUL)
10556 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010558 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562}
10563
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010564#ifdef FEAT_FLOAT
10565/*
10566 * "exp()" function
10567 */
10568 static void
10569f_exp(argvars, rettv)
10570 typval_T *argvars;
10571 typval_T *rettv;
10572{
10573 float_T f;
10574
10575 rettv->v_type = VAR_FLOAT;
10576 if (get_float_arg(argvars, &f) == OK)
10577 rettv->vval.v_float = exp(f);
10578 else
10579 rettv->vval.v_float = 0.0;
10580}
10581#endif
10582
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583/*
10584 * "expand()" function
10585 */
10586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 typval_T *argvars;
10589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
10591 char_u *s;
10592 int len;
10593 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010594 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010596 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010597 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010600 if (argvars[1].v_type != VAR_UNKNOWN
10601 && argvars[2].v_type != VAR_UNKNOWN
10602 && get_tv_number_chk(&argvars[2], &error)
10603 && !error)
10604 {
10605 rettv->v_type = VAR_LIST;
10606 rettv->vval.v_list = NULL;
10607 }
10608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 if (*s == '%' || *s == '#' || *s == '<')
10611 {
10612 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010613 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010615 if (rettv->v_type == VAR_LIST)
10616 {
10617 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10618 list_append_string(rettv->vval.v_list, result, -1);
10619 else
10620 vim_free(result);
10621 }
10622 else
10623 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 }
10625 else
10626 {
10627 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010628 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 if (argvars[1].v_type != VAR_UNKNOWN
10630 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010631 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 if (!error)
10633 {
10634 ExpandInit(&xpc);
10635 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010636 if (p_wic)
10637 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010638 if (rettv->v_type == VAR_STRING)
10639 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10640 options, WILD_ALL);
10641 else if (rettv_list_alloc(rettv) != FAIL)
10642 {
10643 int i;
10644
10645 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10646 for (i = 0; i < xpc.xp_numfiles; i++)
10647 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10648 ExpandCleanup(&xpc);
10649 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 }
10651 else
10652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 }
10654}
10655
10656/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010657 * Go over all entries in "d2" and add them to "d1".
10658 * When "action" is "error" then a duplicate key is an error.
10659 * When "action" is "force" then a duplicate key is overwritten.
10660 * Otherwise duplicate keys are ignored ("action" is "keep").
10661 */
10662 void
10663dict_extend(d1, d2, action)
10664 dict_T *d1;
10665 dict_T *d2;
10666 char_u *action;
10667{
10668 dictitem_T *di1;
10669 hashitem_T *hi2;
10670 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010671 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010672
10673 todo = (int)d2->dv_hashtab.ht_used;
10674 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10675 {
10676 if (!HASHITEM_EMPTY(hi2))
10677 {
10678 --todo;
10679 di1 = dict_find(d1, hi2->hi_key, -1);
10680 if (d1->dv_scope != 0)
10681 {
10682 /* Disallow replacing a builtin function in l: and g:.
10683 * Check the key to be valid when adding to any
10684 * scope. */
10685 if (d1->dv_scope == VAR_DEF_SCOPE
10686 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10687 && var_check_func_name(hi2->hi_key,
10688 di1 == NULL))
10689 break;
10690 if (!valid_varname(hi2->hi_key))
10691 break;
10692 }
10693 if (di1 == NULL)
10694 {
10695 di1 = dictitem_copy(HI2DI(hi2));
10696 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10697 dictitem_free(di1);
10698 }
10699 else if (*action == 'e')
10700 {
10701 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10702 break;
10703 }
10704 else if (*action == 'f' && HI2DI(hi2) != di1)
10705 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010706 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10707 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010708 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010709 clear_tv(&di1->di_tv);
10710 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10711 }
10712 }
10713 }
10714}
10715
10716/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010717 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010718 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010719 */
10720 static void
10721f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 typval_T *argvars;
10723 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010724{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010725 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010726
Bram Moolenaare9a41262005-01-15 22:18:47 +000010727 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010728 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 list_T *l1, *l2;
10730 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010731 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010733
Bram Moolenaare9a41262005-01-15 22:18:47 +000010734 l1 = argvars[0].vval.v_list;
10735 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010736 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010737 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010738 {
10739 if (argvars[2].v_type != VAR_UNKNOWN)
10740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 before = get_tv_number_chk(&argvars[2], &error);
10742 if (error)
10743 return; /* type error; errmsg already given */
10744
Bram Moolenaar758711c2005-02-02 23:11:38 +000010745 if (before == l1->lv_len)
10746 item = NULL;
10747 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010748 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010749 item = list_find(l1, before);
10750 if (item == NULL)
10751 {
10752 EMSGN(_(e_listidx), before);
10753 return;
10754 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010755 }
10756 }
10757 else
10758 item = NULL;
10759 list_extend(l1, l2, item);
10760
Bram Moolenaare9a41262005-01-15 22:18:47 +000010761 copy_tv(&argvars[0], rettv);
10762 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010764 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10765 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010766 dict_T *d1, *d2;
10767 char_u *action;
10768 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769
10770 d1 = argvars[0].vval.v_dict;
10771 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010772 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010773 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 {
10775 /* Check the third argument. */
10776 if (argvars[2].v_type != VAR_UNKNOWN)
10777 {
10778 static char *(av[]) = {"keep", "force", "error"};
10779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010780 action = get_tv_string_chk(&argvars[2]);
10781 if (action == NULL)
10782 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783 for (i = 0; i < 3; ++i)
10784 if (STRCMP(action, av[i]) == 0)
10785 break;
10786 if (i == 3)
10787 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010788 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 return;
10790 }
10791 }
10792 else
10793 action = (char_u *)"force";
10794
Bram Moolenaara9922d62013-05-30 13:01:18 +020010795 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 copy_tv(&argvars[0], rettv);
10798 }
10799 }
10800 else
10801 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010802}
10803
10804/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010805 * "feedkeys()" function
10806 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010807 static void
10808f_feedkeys(argvars, rettv)
10809 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010810 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010811{
10812 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010813 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010814 char_u *keys, *flags;
10815 char_u nbuf[NUMBUFLEN];
10816 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010817 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010818
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010819 /* This is not allowed in the sandbox. If the commands would still be
10820 * executed in the sandbox it would be OK, but it probably happens later,
10821 * when "sandbox" is no longer set. */
10822 if (check_secure())
10823 return;
10824
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010825 keys = get_tv_string(&argvars[0]);
10826 if (*keys != NUL)
10827 {
10828 if (argvars[1].v_type != VAR_UNKNOWN)
10829 {
10830 flags = get_tv_string_buf(&argvars[1], nbuf);
10831 for ( ; *flags != NUL; ++flags)
10832 {
10833 switch (*flags)
10834 {
10835 case 'n': remap = FALSE; break;
10836 case 'm': remap = TRUE; break;
10837 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010838 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010839 }
10840 }
10841 }
10842
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010843 /* Need to escape K_SPECIAL and CSI before putting the string in the
10844 * typeahead buffer. */
10845 keys_esc = vim_strsave_escape_csi(keys);
10846 if (keys_esc != NULL)
10847 {
10848 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010849 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010850 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010851 if (vgetc_busy)
10852 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010853 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010854 }
10855}
10856
10857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 * "filereadable()" function
10859 */
10860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010862 typval_T *argvars;
10863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010865 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 char_u *p;
10867 int n;
10868
Bram Moolenaarc236c162008-07-13 17:41:49 +000010869#ifndef O_NONBLOCK
10870# define O_NONBLOCK 0
10871#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010873 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10874 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 {
10876 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010877 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879 else
10880 n = FALSE;
10881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883}
10884
10885/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010886 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 * rights to write into.
10888 */
10889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010891 typval_T *argvars;
10892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010894 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895}
10896
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010897static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010898
10899 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010900findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010902 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010903 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010904{
10905#ifdef FEAT_SEARCHPATH
10906 char_u *fname;
10907 char_u *fresult = NULL;
10908 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10909 char_u *p;
10910 char_u pathbuf[NUMBUFLEN];
10911 int count = 1;
10912 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010913 int error = FALSE;
10914#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010915
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010916 rettv->vval.v_string = NULL;
10917 rettv->v_type = VAR_STRING;
10918
10919#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010921
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010922 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10925 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010926 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 else
10928 {
10929 if (*p != NUL)
10930 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010933 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010935 }
10936
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010937 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10938 error = TRUE;
10939
10940 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 do
10943 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010944 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010945 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 fresult = find_file_in_path_option(first ? fname : NULL,
10947 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010948 0, first, path,
10949 find_what,
10950 curbuf->b_ffname,
10951 find_what == FINDFILE_DIR
10952 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010954
10955 if (fresult != NULL && rettv->v_type == VAR_LIST)
10956 list_append_string(rettv->vval.v_list, fresult, -1);
10957
10958 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010960
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010961 if (rettv->v_type == VAR_STRING)
10962 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010963#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010964}
10965
Bram Moolenaar33570922005-01-25 22:26:29 +000010966static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10967static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010968
10969/*
10970 * Implementation of map() and filter().
10971 */
10972 static void
10973filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010974 typval_T *argvars;
10975 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010976 int map;
10977{
10978 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010980 listitem_T *li, *nli;
10981 list_T *l = NULL;
10982 dictitem_T *di;
10983 hashtab_T *ht;
10984 hashitem_T *hi;
10985 dict_T *d = NULL;
10986 typval_T save_val;
10987 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010989 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010990 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010991 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010992 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010993 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010994 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010995
Bram Moolenaare9a41262005-01-15 22:18:47 +000010996 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010997 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010998 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010999 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 return;
11001 }
11002 else if (argvars[0].v_type == VAR_DICT)
11003 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011004 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011005 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 return;
11007 }
11008 else
11009 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011010 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 return;
11012 }
11013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011014 expr = get_tv_string_buf_chk(&argvars[1], buf);
11015 /* On type errors, the preceding call has already displayed an error
11016 * message. Avoid a misleading error message for an empty string that
11017 * was not passed as argument. */
11018 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 prepare_vimvar(VV_VAL, &save_val);
11021 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011022
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011023 /* We reset "did_emsg" to be able to detect whether an error
11024 * occurred during evaluation of the expression. */
11025 save_did_emsg = did_emsg;
11026 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011027
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011028 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 vimvars[VV_KEY].vv_type = VAR_STRING;
11032
11033 ht = &d->dv_hashtab;
11034 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011035 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 if (!HASHITEM_EMPTY(hi))
11039 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011040 int r;
11041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 --todo;
11043 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011044 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011045 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11046 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 break;
11048 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011049 r = filter_map_one(&di->di_tv, expr, map, &rem);
11050 clear_tv(&vimvars[VV_KEY].vv_tv);
11051 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 break;
11053 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011054 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011055 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11056 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011059 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 }
11061 }
11062 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 }
11064 else
11065 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011066 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 for (li = l->lv_first; li != NULL; li = nli)
11069 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011070 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011071 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011073 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011074 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011075 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011076 break;
11077 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011079 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011080 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011081 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011082
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011083 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011085
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011086 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088
11089 copy_tv(&argvars[0], rettv);
11090}
11091
11092 static int
11093filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011094 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011095 char_u *expr;
11096 int map;
11097 int *remp;
11098{
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011100 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011101 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 s = expr;
11105 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011106 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 if (*s != NUL) /* check for trailing chars after expr */
11108 {
11109 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011110 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011111 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 }
11113 if (map)
11114 {
11115 /* map(): replace the list item value */
11116 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011117 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118 *tv = rettv;
11119 }
11120 else
11121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 int error = FALSE;
11123
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 /* On type error, nothing has been removed; return FAIL to stop the
11128 * loop. The error message was given by get_tv_number_chk(). */
11129 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011130 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011132 retval = OK;
11133theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011135 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011136}
11137
11138/*
11139 * "filter()" function
11140 */
11141 static void
11142f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011143 typval_T *argvars;
11144 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011145{
11146 filter_map(argvars, rettv, FALSE);
11147}
11148
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011149/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 * "finddir({fname}[, {path}[, {count}]])" function
11151 */
11152 static void
11153f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011157 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158}
11159
11160/*
11161 * "findfile({fname}[, {path}[, {count}]])" function
11162 */
11163 static void
11164f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011168 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169}
11170
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011171#ifdef FEAT_FLOAT
11172/*
11173 * "float2nr({float})" function
11174 */
11175 static void
11176f_float2nr(argvars, rettv)
11177 typval_T *argvars;
11178 typval_T *rettv;
11179{
11180 float_T f;
11181
11182 if (get_float_arg(argvars, &f) == OK)
11183 {
11184 if (f < -0x7fffffff)
11185 rettv->vval.v_number = -0x7fffffff;
11186 else if (f > 0x7fffffff)
11187 rettv->vval.v_number = 0x7fffffff;
11188 else
11189 rettv->vval.v_number = (varnumber_T)f;
11190 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011191}
11192
11193/*
11194 * "floor({float})" function
11195 */
11196 static void
11197f_floor(argvars, rettv)
11198 typval_T *argvars;
11199 typval_T *rettv;
11200{
11201 float_T f;
11202
11203 rettv->v_type = VAR_FLOAT;
11204 if (get_float_arg(argvars, &f) == OK)
11205 rettv->vval.v_float = floor(f);
11206 else
11207 rettv->vval.v_float = 0.0;
11208}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011209
11210/*
11211 * "fmod()" function
11212 */
11213 static void
11214f_fmod(argvars, rettv)
11215 typval_T *argvars;
11216 typval_T *rettv;
11217{
11218 float_T fx, fy;
11219
11220 rettv->v_type = VAR_FLOAT;
11221 if (get_float_arg(argvars, &fx) == OK
11222 && get_float_arg(&argvars[1], &fy) == OK)
11223 rettv->vval.v_float = fmod(fx, fy);
11224 else
11225 rettv->vval.v_float = 0.0;
11226}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011227#endif
11228
Bram Moolenaar0d660222005-01-07 21:51:51 +000011229/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011230 * "fnameescape({string})" function
11231 */
11232 static void
11233f_fnameescape(argvars, rettv)
11234 typval_T *argvars;
11235 typval_T *rettv;
11236{
11237 rettv->vval.v_string = vim_strsave_fnameescape(
11238 get_tv_string(&argvars[0]), FALSE);
11239 rettv->v_type = VAR_STRING;
11240}
11241
11242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 * "fnamemodify({fname}, {mods})" function
11244 */
11245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *argvars;
11248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
11250 char_u *fname;
11251 char_u *mods;
11252 int usedlen = 0;
11253 int len;
11254 char_u *fbuf = NULL;
11255 char_u buf[NUMBUFLEN];
11256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 fname = get_tv_string_chk(&argvars[0]);
11258 mods = get_tv_string_buf_chk(&argvars[1], buf);
11259 if (fname == NULL || mods == NULL)
11260 fname = NULL;
11261 else
11262 {
11263 len = (int)STRLEN(fname);
11264 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 vim_free(fbuf);
11273}
11274
Bram Moolenaar33570922005-01-25 22:26:29 +000011275static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276
11277/*
11278 * "foldclosed()" function
11279 */
11280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011282 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011284 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
11286#ifdef FEAT_FOLDING
11287 linenr_T lnum;
11288 linenr_T first, last;
11289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11292 {
11293 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11294 {
11295 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 return;
11300 }
11301 }
11302#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304}
11305
11306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307 * "foldclosed()" function
11308 */
11309 static void
11310f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011311 typval_T *argvars;
11312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011313{
11314 foldclosed_both(argvars, rettv, FALSE);
11315}
11316
11317/*
11318 * "foldclosedend()" function
11319 */
11320 static void
11321f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011324{
11325 foldclosed_both(argvars, rettv, TRUE);
11326}
11327
11328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 * "foldlevel()" function
11330 */
11331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011333 typval_T *argvars UNUSED;
11334 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
11336#ifdef FEAT_FOLDING
11337 linenr_T lnum;
11338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343}
11344
11345/*
11346 * "foldtext()" function
11347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011350 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353#ifdef FEAT_FOLDING
11354 linenr_T lnum;
11355 char_u *s;
11356 char_u *r;
11357 int len;
11358 char *txt;
11359#endif
11360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011364 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11365 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11366 <= curbuf->b_ml.ml_line_count
11367 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 {
11369 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011370 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11371 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 {
11373 if (!linewhite(lnum))
11374 break;
11375 ++lnum;
11376 }
11377
11378 /* Find interesting text in this line. */
11379 s = skipwhite(ml_get(lnum));
11380 /* skip C comment-start */
11381 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011384 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011385 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011386 {
11387 s = skipwhite(ml_get(lnum + 1));
11388 if (*s == '*')
11389 s = skipwhite(s + 1);
11390 }
11391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 txt = _("+-%s%3ld lines: ");
11393 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 + 20 /* for %3ld */
11396 + STRLEN(s))); /* concatenated */
11397 if (r != NULL)
11398 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011399 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11400 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11401 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 len = (int)STRLEN(r);
11403 STRCAT(r, s);
11404 /* remove 'foldmarker' and 'commentstring' */
11405 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 }
11408 }
11409#endif
11410}
11411
11412/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011413 * "foldtextresult(lnum)" function
11414 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011417 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011418 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011419{
11420#ifdef FEAT_FOLDING
11421 linenr_T lnum;
11422 char_u *text;
11423 char_u buf[51];
11424 foldinfo_T foldinfo;
11425 int fold_count;
11426#endif
11427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->v_type = VAR_STRING;
11429 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011430#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 /* treat illegal types and illegal string values for {lnum} the same */
11433 if (lnum < 0)
11434 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011435 fold_count = foldedCount(curwin, lnum, &foldinfo);
11436 if (fold_count > 0)
11437 {
11438 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11439 &foldinfo, buf);
11440 if (text == buf)
11441 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011443 }
11444#endif
11445}
11446
11447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 * "foreground()" function
11449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011452 typval_T *argvars UNUSED;
11453 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455#ifdef FEAT_GUI
11456 if (gui.in_use)
11457 gui_mch_set_foreground();
11458#else
11459# ifdef WIN32
11460 win32_set_foreground();
11461# endif
11462#endif
11463}
11464
11465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 * "function()" function
11467 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472{
11473 char_u *s;
11474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011476 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011478 /* Don't check an autoload name for existence here. */
11479 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011480 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481 else
11482 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011483 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011484 {
11485 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011486 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011487
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011488 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11489 * also be called from another script. Using trans_function_name()
11490 * would also work, but some plugins depend on the name being
11491 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011492 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011493 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011494 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011495 if (rettv->vval.v_string != NULL)
11496 {
11497 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011498 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011499 }
11500 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011501 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011502 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011504 }
11505}
11506
11507/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011508 * "garbagecollect()" function
11509 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011510 static void
11511f_garbagecollect(argvars, rettv)
11512 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011513 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011514{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011515 /* This is postponed until we are back at the toplevel, because we may be
11516 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11517 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011518
11519 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11520 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011521}
11522
11523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011524 * "get()" function
11525 */
11526 static void
11527f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011530{
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 listitem_T *li;
11532 list_T *l;
11533 dictitem_T *di;
11534 dict_T *d;
11535 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541 int error = FALSE;
11542
11543 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11544 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011545 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011546 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 else if (argvars[0].v_type == VAR_DICT)
11549 {
11550 if ((d = argvars[0].vval.v_dict) != NULL)
11551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011552 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 if (di != NULL)
11554 tv = &di->di_tv;
11555 }
11556 }
11557 else
11558 EMSG2(_(e_listdictarg), "get()");
11559
11560 if (tv == NULL)
11561 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011562 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 copy_tv(&argvars[2], rettv);
11564 }
11565 else
11566 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567}
11568
Bram Moolenaar342337a2005-07-21 21:11:17 +000011569static 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 +000011570
11571/*
11572 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573 * Return a range (from start to end) of lines in rettv from the specified
11574 * buffer.
11575 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011576 */
11577 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011578get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011579 buf_T *buf;
11580 linenr_T start;
11581 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011583 typval_T *rettv;
11584{
11585 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011586
Bram Moolenaar959a1432013-12-14 12:17:38 +010011587 rettv->v_type = VAR_STRING;
11588 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011589 if (retlist && rettv_list_alloc(rettv) == FAIL)
11590 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011591
11592 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11593 return;
11594
11595 if (!retlist)
11596 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11598 p = ml_get_buf(buf, start, FALSE);
11599 else
11600 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011601 rettv->vval.v_string = vim_strsave(p);
11602 }
11603 else
11604 {
11605 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606 return;
11607
11608 if (start < 1)
11609 start = 1;
11610 if (end > buf->b_ml.ml_line_count)
11611 end = buf->b_ml.ml_line_count;
11612 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011613 if (list_append_string(rettv->vval.v_list,
11614 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011615 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011616 }
11617}
11618
11619/*
11620 * "getbufline()" function
11621 */
11622 static void
11623f_getbufline(argvars, rettv)
11624 typval_T *argvars;
11625 typval_T *rettv;
11626{
11627 linenr_T lnum;
11628 linenr_T end;
11629 buf_T *buf;
11630
11631 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11632 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011633 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011634 --emsg_off;
11635
Bram Moolenaar661b1822005-07-28 22:36:45 +000011636 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011637 if (argvars[2].v_type == VAR_UNKNOWN)
11638 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011639 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011640 end = get_tv_lnum_buf(&argvars[2], buf);
11641
Bram Moolenaar342337a2005-07-21 21:11:17 +000011642 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011643}
11644
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645/*
11646 * "getbufvar()" function
11647 */
11648 static void
11649f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011650 typval_T *argvars;
11651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011652{
11653 buf_T *buf;
11654 buf_T *save_curbuf;
11655 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011657 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11660 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011662 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011664 rettv->v_type = VAR_STRING;
11665 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666
11667 if (buf != NULL && varname != NULL)
11668 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011669 /* set curbuf to be our buf, temporarily */
11670 save_curbuf = curbuf;
11671 curbuf = buf;
11672
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011674 {
11675 if (get_option_tv(&varname, rettv, TRUE) == OK)
11676 done = TRUE;
11677 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011678 else if (STRCMP(varname, "changedtick") == 0)
11679 {
11680 rettv->v_type = VAR_NUMBER;
11681 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011682 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011683 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684 else
11685 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011686 /* Look up the variable. */
11687 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11688 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11689 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011691 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011693 done = TRUE;
11694 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011695 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011696
11697 /* restore previous notion of curbuf */
11698 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699 }
11700
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011701 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11702 /* use the default value */
11703 copy_tv(&argvars[2], rettv);
11704
Bram Moolenaar0d660222005-01-07 21:51:51 +000011705 --emsg_off;
11706}
11707
11708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 * "getchar()" function
11710 */
11711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011713 typval_T *argvars;
11714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715{
11716 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011719 /* Position the cursor. Needed after a message that ends in a space. */
11720 windgoto(msg_row, msg_col);
11721
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 ++no_mapping;
11723 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011724 for (;;)
11725 {
11726 if (argvars[0].v_type == VAR_UNKNOWN)
11727 /* getchar(): blocking wait. */
11728 n = safe_vgetc();
11729 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11730 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011731 n = vpeekc_any();
11732 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011733 /* illegal argument or getchar(0) and no char avail: return zero */
11734 n = 0;
11735 else
11736 /* getchar(0) and char avail: return char */
11737 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011738
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011739 if (n == K_IGNORE)
11740 continue;
11741 break;
11742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 --no_mapping;
11744 --allow_keys;
11745
Bram Moolenaar219b8702006-11-01 14:32:36 +000011746 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11747 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11748 vimvars[VV_MOUSE_COL].vv_nr = 0;
11749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 if (IS_SPECIAL(n) || mod_mask != 0)
11752 {
11753 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11754 int i = 0;
11755
11756 /* Turn a special key into three bytes, plus modifier. */
11757 if (mod_mask != 0)
11758 {
11759 temp[i++] = K_SPECIAL;
11760 temp[i++] = KS_MODIFIER;
11761 temp[i++] = mod_mask;
11762 }
11763 if (IS_SPECIAL(n))
11764 {
11765 temp[i++] = K_SPECIAL;
11766 temp[i++] = K_SECOND(n);
11767 temp[i++] = K_THIRD(n);
11768 }
11769#ifdef FEAT_MBYTE
11770 else if (has_mbyte)
11771 i += (*mb_char2bytes)(n, temp + i);
11772#endif
11773 else
11774 temp[i++] = n;
11775 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011778
11779#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011780 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011781 {
11782 int row = mouse_row;
11783 int col = mouse_col;
11784 win_T *win;
11785 linenr_T lnum;
11786# ifdef FEAT_WINDOWS
11787 win_T *wp;
11788# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011789 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011790
11791 if (row >= 0 && col >= 0)
11792 {
11793 /* Find the window at the mouse coordinates and compute the
11794 * text position. */
11795 win = mouse_find_win(&row, &col);
11796 (void)mouse_comp_pos(win, &row, &col, &lnum);
11797# ifdef FEAT_WINDOWS
11798 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011799 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011800# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011801 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011802 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11803 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11804 }
11805 }
11806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 }
11808}
11809
11810/*
11811 * "getcharmod()" function
11812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819}
11820
11821/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011822 * "getcharsearch()" function
11823 */
11824 static void
11825f_getcharsearch(argvars, rettv)
11826 typval_T *argvars UNUSED;
11827 typval_T *rettv;
11828{
11829 if (rettv_dict_alloc(rettv) != FAIL)
11830 {
11831 dict_T *dict = rettv->vval.v_dict;
11832
11833 dict_add_nr_str(dict, "char", 0L, last_csearch());
11834 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11835 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11836 }
11837}
11838
11839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 * "getcmdline()" function
11841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011844 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->v_type = VAR_STRING;
11848 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849}
11850
11851/*
11852 * "getcmdpos()" function
11853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860}
11861
11862/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011863 * "getcmdtype()" function
11864 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011865 static void
11866f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011867 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011868 typval_T *rettv;
11869{
11870 rettv->v_type = VAR_STRING;
11871 rettv->vval.v_string = alloc(2);
11872 if (rettv->vval.v_string != NULL)
11873 {
11874 rettv->vval.v_string[0] = get_cmdline_type();
11875 rettv->vval.v_string[1] = NUL;
11876 }
11877}
11878
11879/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011880 * "getcmdwintype()" function
11881 */
11882 static void
11883f_getcmdwintype(argvars, rettv)
11884 typval_T *argvars UNUSED;
11885 typval_T *rettv;
11886{
11887 rettv->v_type = VAR_STRING;
11888 rettv->vval.v_string = NULL;
11889#ifdef FEAT_CMDWIN
11890 rettv->vval.v_string = alloc(2);
11891 if (rettv->vval.v_string != NULL)
11892 {
11893 rettv->vval.v_string[0] = cmdwin_type;
11894 rettv->vval.v_string[1] = NUL;
11895 }
11896#endif
11897}
11898
11899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 * "getcwd()" function
11901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011907 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011910 rettv->vval.v_string = NULL;
11911 cwd = alloc(MAXPATHL);
11912 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011914 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11915 {
11916 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011918 if (rettv->vval.v_string != NULL)
11919 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011921 }
11922 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 }
11924}
11925
11926/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011927 * "getfontname()" function
11928 */
11929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011932 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011933{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 rettv->v_type = VAR_STRING;
11935 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011936#ifdef FEAT_GUI
11937 if (gui.in_use)
11938 {
11939 GuiFont font;
11940 char_u *name = NULL;
11941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011942 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011943 {
11944 /* Get the "Normal" font. Either the name saved by
11945 * hl_set_font_name() or from the font ID. */
11946 font = gui.norm_font;
11947 name = hl_get_font_name();
11948 }
11949 else
11950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011952 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11953 return;
11954 font = gui_mch_get_font(name, FALSE);
11955 if (font == NOFONT)
11956 return; /* Invalid font name, return empty string. */
11957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011959 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011960 gui_mch_free_font(font);
11961 }
11962#endif
11963}
11964
11965/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011966 * "getfperm({fname})" function
11967 */
11968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 typval_T *argvars;
11971 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011972{
11973 char_u *fname;
11974 struct stat st;
11975 char_u *perm = NULL;
11976 char_u flags[] = "rwx";
11977 int i;
11978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011982 if (mch_stat((char *)fname, &st) >= 0)
11983 {
11984 perm = vim_strsave((char_u *)"---------");
11985 if (perm != NULL)
11986 {
11987 for (i = 0; i < 9; i++)
11988 {
11989 if (st.st_mode & (1 << (8 - i)))
11990 perm[i] = flags[i % 3];
11991 }
11992 }
11993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011995}
11996
11997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 * "getfsize({fname})" function
11999 */
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
12005 char_u *fname;
12006 struct stat st;
12007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011
12012 if (mch_stat((char *)fname, &st) >= 0)
12013 {
12014 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012019
12020 /* non-perfect check for overflow */
12021 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12022 rettv->vval.v_number = -2;
12023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 }
12025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027}
12028
12029/*
12030 * "getftime({fname})" function
12031 */
12032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *argvars;
12035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
12037 char_u *fname;
12038 struct stat st;
12039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041
12042 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046}
12047
12048/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012049 * "getftype({fname})" function
12050 */
12051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012055{
12056 char_u *fname;
12057 struct stat st;
12058 char_u *type = NULL;
12059 char *t;
12060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012064 if (mch_lstat((char *)fname, &st) >= 0)
12065 {
12066#ifdef S_ISREG
12067 if (S_ISREG(st.st_mode))
12068 t = "file";
12069 else if (S_ISDIR(st.st_mode))
12070 t = "dir";
12071# ifdef S_ISLNK
12072 else if (S_ISLNK(st.st_mode))
12073 t = "link";
12074# endif
12075# ifdef S_ISBLK
12076 else if (S_ISBLK(st.st_mode))
12077 t = "bdev";
12078# endif
12079# ifdef S_ISCHR
12080 else if (S_ISCHR(st.st_mode))
12081 t = "cdev";
12082# endif
12083# ifdef S_ISFIFO
12084 else if (S_ISFIFO(st.st_mode))
12085 t = "fifo";
12086# endif
12087# ifdef S_ISSOCK
12088 else if (S_ISSOCK(st.st_mode))
12089 t = "fifo";
12090# endif
12091 else
12092 t = "other";
12093#else
12094# ifdef S_IFMT
12095 switch (st.st_mode & S_IFMT)
12096 {
12097 case S_IFREG: t = "file"; break;
12098 case S_IFDIR: t = "dir"; break;
12099# ifdef S_IFLNK
12100 case S_IFLNK: t = "link"; break;
12101# endif
12102# ifdef S_IFBLK
12103 case S_IFBLK: t = "bdev"; break;
12104# endif
12105# ifdef S_IFCHR
12106 case S_IFCHR: t = "cdev"; break;
12107# endif
12108# ifdef S_IFIFO
12109 case S_IFIFO: t = "fifo"; break;
12110# endif
12111# ifdef S_IFSOCK
12112 case S_IFSOCK: t = "socket"; break;
12113# endif
12114 default: t = "other";
12115 }
12116# else
12117 if (mch_isdir(fname))
12118 t = "dir";
12119 else
12120 t = "file";
12121# endif
12122#endif
12123 type = vim_strsave((char_u *)t);
12124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012126}
12127
12128/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012129 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130 */
12131 static void
12132f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012133 typval_T *argvars;
12134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135{
12136 linenr_T lnum;
12137 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012138 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139
12140 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012141 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012142 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012143 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012144 retlist = FALSE;
12145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012147 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012149 retlist = TRUE;
12150 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012151
Bram Moolenaar342337a2005-07-21 21:11:17 +000012152 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153}
12154
12155/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012156 * "getmatches()" function
12157 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012158 static void
12159f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012160 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012161 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012162{
12163#ifdef FEAT_SEARCH_EXTRA
12164 dict_T *dict;
12165 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012166 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012167
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012168 if (rettv_list_alloc(rettv) == OK)
12169 {
12170 while (cur != NULL)
12171 {
12172 dict = dict_alloc();
12173 if (dict == NULL)
12174 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012175 if (cur->match.regprog == NULL)
12176 {
12177 /* match added with matchaddpos() */
12178 for (i = 0; i < MAXPOSMATCH; ++i)
12179 {
12180 llpos_T *llpos;
12181 char buf[6];
12182 list_T *l;
12183
12184 llpos = &cur->pos.pos[i];
12185 if (llpos->lnum == 0)
12186 break;
12187 l = list_alloc();
12188 if (l == NULL)
12189 break;
12190 list_append_number(l, (varnumber_T)llpos->lnum);
12191 if (llpos->col > 0)
12192 {
12193 list_append_number(l, (varnumber_T)llpos->col);
12194 list_append_number(l, (varnumber_T)llpos->len);
12195 }
12196 sprintf(buf, "pos%d", i + 1);
12197 dict_add_list(dict, buf, l);
12198 }
12199 }
12200 else
12201 {
12202 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12203 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012204 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012205 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12206 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012207# ifdef FEAT_CONCEAL
12208 if (cur->conceal_char)
12209 {
12210 char_u buf[MB_MAXBYTES + 1];
12211
12212 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12213 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12214 }
12215# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012216 list_append_dict(rettv->vval.v_list, dict);
12217 cur = cur->next;
12218 }
12219 }
12220#endif
12221}
12222
12223/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012224 * "getpid()" function
12225 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012226 static void
12227f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012228 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012229 typval_T *rettv;
12230{
12231 rettv->vval.v_number = mch_get_pid();
12232}
12233
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012234static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12235
12236/*
12237 * "getcurpos()" function
12238 */
12239 static void
12240f_getcurpos(argvars, rettv)
12241 typval_T *argvars;
12242 typval_T *rettv;
12243{
12244 getpos_both(argvars, rettv, TRUE);
12245}
12246
Bram Moolenaar18081e32008-02-20 19:11:07 +000012247/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012248 * "getpos(string)" function
12249 */
12250 static void
12251f_getpos(argvars, rettv)
12252 typval_T *argvars;
12253 typval_T *rettv;
12254{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012255 getpos_both(argvars, rettv, FALSE);
12256}
12257
12258 static void
12259getpos_both(argvars, rettv, getcurpos)
12260 typval_T *argvars;
12261 typval_T *rettv;
12262 int getcurpos;
12263{
Bram Moolenaara5525202006-03-02 22:52:09 +000012264 pos_T *fp;
12265 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012266 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012267
12268 if (rettv_list_alloc(rettv) == OK)
12269 {
12270 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012271 if (getcurpos)
12272 fp = &curwin->w_cursor;
12273 else
12274 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012275 if (fnum != -1)
12276 list_append_number(l, (varnumber_T)fnum);
12277 else
12278 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012279 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12280 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012281 list_append_number(l, (fp != NULL)
12282 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012283 : (varnumber_T)0);
12284 list_append_number(l,
12285#ifdef FEAT_VIRTUALEDIT
12286 (fp != NULL) ? (varnumber_T)fp->coladd :
12287#endif
12288 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012289 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012290 list_append_number(l, curwin->w_curswant == MAXCOL ?
12291 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012292 }
12293 else
12294 rettv->vval.v_number = FALSE;
12295}
12296
12297/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012298 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012299 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012300 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012301f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012302 typval_T *argvars UNUSED;
12303 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012304{
12305#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012306 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012307#endif
12308
Bram Moolenaar2641f772005-03-25 21:58:17 +000012309#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012310 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012311 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012312 wp = NULL;
12313 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12314 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012315 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012316 if (wp == NULL)
12317 return;
12318 }
12319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012320 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012321 }
12322#endif
12323}
12324
12325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 * "getreg()" function
12327 */
12328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *argvars;
12331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332{
12333 char_u *strregname;
12334 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012335 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012336 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012337 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012339 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 strregname = get_tv_string_chk(&argvars[0]);
12342 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012345 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012346 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12347 return_list = get_tv_number_chk(&argvars[2], &error);
12348 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012351 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012352
12353 if (error)
12354 return;
12355
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 regname = (strregname == NULL ? '"' : *strregname);
12357 if (regname == 0)
12358 regname = '"';
12359
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012360 if (return_list)
12361 {
12362 rettv->v_type = VAR_LIST;
12363 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12364 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012365 if (rettv->vval.v_list != NULL)
12366 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012367 }
12368 else
12369 {
12370 rettv->v_type = VAR_STRING;
12371 rettv->vval.v_string = get_reg_contents(regname,
12372 arg2 ? GREG_EXPR_SRC : 0);
12373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
12377 * "getregtype()" function
12378 */
12379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
12384 char_u *strregname;
12385 int regname;
12386 char_u buf[NUMBUFLEN + 2];
12387 long reglen = 0;
12388
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012389 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 {
12391 strregname = get_tv_string_chk(&argvars[0]);
12392 if (strregname == NULL) /* type error; errmsg already given */
12393 {
12394 rettv->v_type = VAR_STRING;
12395 rettv->vval.v_string = NULL;
12396 return;
12397 }
12398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 else
12400 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012401 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402
12403 regname = (strregname == NULL ? '"' : *strregname);
12404 if (regname == 0)
12405 regname = '"';
12406
12407 buf[0] = NUL;
12408 buf[1] = NUL;
12409 switch (get_reg_type(regname, &reglen))
12410 {
12411 case MLINE: buf[0] = 'V'; break;
12412 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 case MBLOCK:
12414 buf[0] = Ctrl_V;
12415 sprintf((char *)buf + 1, "%ld", reglen + 1);
12416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 rettv->v_type = VAR_STRING;
12419 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420}
12421
12422/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012423 * "gettabvar()" function
12424 */
12425 static void
12426f_gettabvar(argvars, rettv)
12427 typval_T *argvars;
12428 typval_T *rettv;
12429{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012430 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012431 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012432 dictitem_T *v;
12433 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012434 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012435
12436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
12438
12439 varname = get_tv_string_chk(&argvars[1]);
12440 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12441 if (tp != NULL && varname != NULL)
12442 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012443 /* Set tp to be our tabpage, temporarily. Also set the window to the
12444 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012445 if (switch_win(&oldcurwin, &oldtabpage,
12446 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012447 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012448 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012449 /* look up the variable */
12450 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12451 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12452 if (v != NULL)
12453 {
12454 copy_tv(&v->di_tv, rettv);
12455 done = TRUE;
12456 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012457 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012458
12459 /* restore previous notion of curwin */
12460 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012461 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012462
12463 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012464 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012465}
12466
12467/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012468 * "gettabwinvar()" function
12469 */
12470 static void
12471f_gettabwinvar(argvars, rettv)
12472 typval_T *argvars;
12473 typval_T *rettv;
12474{
12475 getwinvar(argvars, rettv, 1);
12476}
12477
12478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 * "getwinposx()" function
12480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012483 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487#ifdef FEAT_GUI
12488 if (gui.in_use)
12489 {
12490 int x, y;
12491
12492 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 }
12495#endif
12496}
12497
12498/*
12499 * "getwinposy()" function
12500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012503 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507#ifdef FEAT_GUI
12508 if (gui.in_use)
12509 {
12510 int x, y;
12511
12512 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 }
12515#endif
12516}
12517
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012518/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012519 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012520 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012521 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012522find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012523 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012524 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012525{
12526#ifdef FEAT_WINDOWS
12527 win_T *wp;
12528#endif
12529 int nr;
12530
12531 nr = get_tv_number_chk(vp, NULL);
12532
12533#ifdef FEAT_WINDOWS
12534 if (nr < 0)
12535 return NULL;
12536 if (nr == 0)
12537 return curwin;
12538
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012539 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12540 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012541 if (--nr <= 0)
12542 break;
12543 return wp;
12544#else
12545 if (nr == 0 || nr == 1)
12546 return curwin;
12547 return NULL;
12548#endif
12549}
12550
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551/*
12552 * "getwinvar()" function
12553 */
12554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012559 getwinvar(argvars, rettv, 0);
12560}
12561
12562/*
12563 * getwinvar() and gettabwinvar()
12564 */
12565 static void
12566getwinvar(argvars, rettv, off)
12567 typval_T *argvars;
12568 typval_T *rettv;
12569 int off; /* 1 for gettabwinvar() */
12570{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012571 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012573 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012574 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012575 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012576#ifdef FEAT_WINDOWS
12577 win_T *oldcurwin;
12578 tabpage_T *oldtabpage;
12579 int need_switch_win;
12580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012582#ifdef FEAT_WINDOWS
12583 if (off == 1)
12584 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12585 else
12586 tp = curtab;
12587#endif
12588 win = find_win_by_nr(&argvars[off], tp);
12589 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012590 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012592 rettv->v_type = VAR_STRING;
12593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594
12595 if (win != NULL && varname != NULL)
12596 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012597#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012598 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012599 * otherwise the window is not valid. Only do this when needed,
12600 * autocommands get blocked. */
12601 need_switch_win = !(tp == curtab && win == curwin);
12602 if (!need_switch_win
12603 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12604#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012606 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012607 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012608 if (get_option_tv(&varname, rettv, 1) == OK)
12609 done = TRUE;
12610 }
12611 else
12612 {
12613 /* Look up the variable. */
12614 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12615 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12616 varname, FALSE);
12617 if (v != NULL)
12618 {
12619 copy_tv(&v->di_tv, rettv);
12620 done = TRUE;
12621 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012624
Bram Moolenaarba117c22015-09-29 16:53:22 +020012625#ifdef FEAT_WINDOWS
12626 if (need_switch_win)
12627 /* restore previous notion of curwin */
12628 restore_win(oldcurwin, oldtabpage, TRUE);
12629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 }
12631
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012632 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12633 /* use the default return value */
12634 copy_tv(&argvars[off + 2], rettv);
12635
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 --emsg_off;
12637}
12638
12639/*
12640 * "glob()" function
12641 */
12642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *argvars;
12645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012647 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012649 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012651 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012652 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012654 if (argvars[1].v_type != VAR_UNKNOWN)
12655 {
12656 if (get_tv_number_chk(&argvars[1], &error))
12657 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012659 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012660 if (get_tv_number_chk(&argvars[2], &error))
12661 {
12662 rettv->v_type = VAR_LIST;
12663 rettv->vval.v_list = NULL;
12664 }
12665 if (argvars[3].v_type != VAR_UNKNOWN
12666 && get_tv_number_chk(&argvars[3], &error))
12667 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012668 }
12669 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012670 if (!error)
12671 {
12672 ExpandInit(&xpc);
12673 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012674 if (p_wic)
12675 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012676 if (rettv->v_type == VAR_STRING)
12677 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012678 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012679 else if (rettv_list_alloc(rettv) != FAIL)
12680 {
12681 int i;
12682
12683 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12684 NULL, options, WILD_ALL_KEEP);
12685 for (i = 0; i < xpc.xp_numfiles; i++)
12686 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12687
12688 ExpandCleanup(&xpc);
12689 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012690 }
12691 else
12692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693}
12694
12695/*
12696 * "globpath()" function
12697 */
12698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 typval_T *argvars;
12701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012703 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012706 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012707 garray_T ga;
12708 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012710 /* When the optional second argument is non-zero, don't remove matches
12711 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012713 if (argvars[2].v_type != VAR_UNKNOWN)
12714 {
12715 if (get_tv_number_chk(&argvars[2], &error))
12716 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012717 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012718 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012719 if (get_tv_number_chk(&argvars[3], &error))
12720 {
12721 rettv->v_type = VAR_LIST;
12722 rettv->vval.v_list = NULL;
12723 }
12724 if (argvars[4].v_type != VAR_UNKNOWN
12725 && get_tv_number_chk(&argvars[4], &error))
12726 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012727 }
12728 }
12729 if (file != NULL && !error)
12730 {
12731 ga_init2(&ga, (int)sizeof(char_u *), 10);
12732 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12733 if (rettv->v_type == VAR_STRING)
12734 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12735 else if (rettv_list_alloc(rettv) != FAIL)
12736 for (i = 0; i < ga.ga_len; ++i)
12737 list_append_string(rettv->vval.v_list,
12738 ((char_u **)(ga.ga_data))[i], -1);
12739 ga_clear_strings(&ga);
12740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012746 * "glob2regpat()" function
12747 */
12748 static void
12749f_glob2regpat(argvars, rettv)
12750 typval_T *argvars;
12751 typval_T *rettv;
12752{
12753 char_u *pat = get_tv_string_chk(&argvars[0]);
12754
12755 rettv->v_type = VAR_STRING;
12756 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12757}
12758
12759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 * "has()" function
12761 */
12762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766{
12767 int i;
12768 char_u *name;
12769 int n = FALSE;
12770 static char *(has_list[]) =
12771 {
12772#ifdef AMIGA
12773 "amiga",
12774# ifdef FEAT_ARP
12775 "arp",
12776# endif
12777#endif
12778#ifdef __BEOS__
12779 "beos",
12780#endif
12781#ifdef MSDOS
12782# ifdef DJGPP
12783 "dos32",
12784# else
12785 "dos16",
12786# endif
12787#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012788#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 "mac",
12790#endif
12791#if defined(MACOS_X_UNIX)
12792 "macunix",
12793#endif
12794#ifdef OS2
12795 "os2",
12796#endif
12797#ifdef __QNX__
12798 "qnx",
12799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800#ifdef UNIX
12801 "unix",
12802#endif
12803#ifdef VMS
12804 "vms",
12805#endif
12806#ifdef WIN16
12807 "win16",
12808#endif
12809#ifdef WIN32
12810 "win32",
12811#endif
12812#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12813 "win32unix",
12814#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012815#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 "win64",
12817#endif
12818#ifdef EBCDIC
12819 "ebcdic",
12820#endif
12821#ifndef CASE_INSENSITIVE_FILENAME
12822 "fname_case",
12823#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012824#ifdef HAVE_ACL
12825 "acl",
12826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827#ifdef FEAT_ARABIC
12828 "arabic",
12829#endif
12830#ifdef FEAT_AUTOCMD
12831 "autocmd",
12832#endif
12833#ifdef FEAT_BEVAL
12834 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012835# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12836 "balloon_multiline",
12837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838#endif
12839#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12840 "builtin_terms",
12841# ifdef ALL_BUILTIN_TCAPS
12842 "all_builtin_terms",
12843# endif
12844#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012845#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12846 || defined(FEAT_GUI_W32) \
12847 || defined(FEAT_GUI_MOTIF))
12848 "browsefilter",
12849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850#ifdef FEAT_BYTEOFF
12851 "byte_offset",
12852#endif
12853#ifdef FEAT_CINDENT
12854 "cindent",
12855#endif
12856#ifdef FEAT_CLIENTSERVER
12857 "clientserver",
12858#endif
12859#ifdef FEAT_CLIPBOARD
12860 "clipboard",
12861#endif
12862#ifdef FEAT_CMDL_COMPL
12863 "cmdline_compl",
12864#endif
12865#ifdef FEAT_CMDHIST
12866 "cmdline_hist",
12867#endif
12868#ifdef FEAT_COMMENTS
12869 "comments",
12870#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012871#ifdef FEAT_CONCEAL
12872 "conceal",
12873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874#ifdef FEAT_CRYPT
12875 "cryptv",
12876#endif
12877#ifdef FEAT_CSCOPE
12878 "cscope",
12879#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012880#ifdef FEAT_CURSORBIND
12881 "cursorbind",
12882#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012883#ifdef CURSOR_SHAPE
12884 "cursorshape",
12885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886#ifdef DEBUG
12887 "debug",
12888#endif
12889#ifdef FEAT_CON_DIALOG
12890 "dialog_con",
12891#endif
12892#ifdef FEAT_GUI_DIALOG
12893 "dialog_gui",
12894#endif
12895#ifdef FEAT_DIFF
12896 "diff",
12897#endif
12898#ifdef FEAT_DIGRAPHS
12899 "digraphs",
12900#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012901#ifdef FEAT_DIRECTX
12902 "directx",
12903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#ifdef FEAT_DND
12905 "dnd",
12906#endif
12907#ifdef FEAT_EMACS_TAGS
12908 "emacs_tags",
12909#endif
12910 "eval", /* always present, of course! */
12911#ifdef FEAT_EX_EXTRA
12912 "ex_extra",
12913#endif
12914#ifdef FEAT_SEARCH_EXTRA
12915 "extra_search",
12916#endif
12917#ifdef FEAT_FKMAP
12918 "farsi",
12919#endif
12920#ifdef FEAT_SEARCHPATH
12921 "file_in_path",
12922#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012923#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012924 "filterpipe",
12925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926#ifdef FEAT_FIND_ID
12927 "find_in_path",
12928#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012929#ifdef FEAT_FLOAT
12930 "float",
12931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932#ifdef FEAT_FOLDING
12933 "folding",
12934#endif
12935#ifdef FEAT_FOOTER
12936 "footer",
12937#endif
12938#if !defined(USE_SYSTEM) && defined(UNIX)
12939 "fork",
12940#endif
12941#ifdef FEAT_GETTEXT
12942 "gettext",
12943#endif
12944#ifdef FEAT_GUI
12945 "gui",
12946#endif
12947#ifdef FEAT_GUI_ATHENA
12948# ifdef FEAT_GUI_NEXTAW
12949 "gui_neXtaw",
12950# else
12951 "gui_athena",
12952# endif
12953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954#ifdef FEAT_GUI_GTK
12955 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012958#ifdef FEAT_GUI_GNOME
12959 "gui_gnome",
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef FEAT_GUI_MAC
12962 "gui_mac",
12963#endif
12964#ifdef FEAT_GUI_MOTIF
12965 "gui_motif",
12966#endif
12967#ifdef FEAT_GUI_PHOTON
12968 "gui_photon",
12969#endif
12970#ifdef FEAT_GUI_W16
12971 "gui_win16",
12972#endif
12973#ifdef FEAT_GUI_W32
12974 "gui_win32",
12975#endif
12976#ifdef FEAT_HANGULIN
12977 "hangul_input",
12978#endif
12979#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12980 "iconv",
12981#endif
12982#ifdef FEAT_INS_EXPAND
12983 "insert_expand",
12984#endif
12985#ifdef FEAT_JUMPLIST
12986 "jumplist",
12987#endif
12988#ifdef FEAT_KEYMAP
12989 "keymap",
12990#endif
12991#ifdef FEAT_LANGMAP
12992 "langmap",
12993#endif
12994#ifdef FEAT_LIBCALL
12995 "libcall",
12996#endif
12997#ifdef FEAT_LINEBREAK
12998 "linebreak",
12999#endif
13000#ifdef FEAT_LISP
13001 "lispindent",
13002#endif
13003#ifdef FEAT_LISTCMDS
13004 "listcmds",
13005#endif
13006#ifdef FEAT_LOCALMAP
13007 "localmap",
13008#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013009#ifdef FEAT_LUA
13010# ifndef DYNAMIC_LUA
13011 "lua",
13012# endif
13013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014#ifdef FEAT_MENU
13015 "menu",
13016#endif
13017#ifdef FEAT_SESSION
13018 "mksession",
13019#endif
13020#ifdef FEAT_MODIFY_FNAME
13021 "modify_fname",
13022#endif
13023#ifdef FEAT_MOUSE
13024 "mouse",
13025#endif
13026#ifdef FEAT_MOUSESHAPE
13027 "mouseshape",
13028#endif
13029#if defined(UNIX) || defined(VMS)
13030# ifdef FEAT_MOUSE_DEC
13031 "mouse_dec",
13032# endif
13033# ifdef FEAT_MOUSE_GPM
13034 "mouse_gpm",
13035# endif
13036# ifdef FEAT_MOUSE_JSB
13037 "mouse_jsbterm",
13038# endif
13039# ifdef FEAT_MOUSE_NET
13040 "mouse_netterm",
13041# endif
13042# ifdef FEAT_MOUSE_PTERM
13043 "mouse_pterm",
13044# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013045# ifdef FEAT_MOUSE_SGR
13046 "mouse_sgr",
13047# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013048# ifdef FEAT_SYSMOUSE
13049 "mouse_sysmouse",
13050# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013051# ifdef FEAT_MOUSE_URXVT
13052 "mouse_urxvt",
13053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054# ifdef FEAT_MOUSE_XTERM
13055 "mouse_xterm",
13056# endif
13057#endif
13058#ifdef FEAT_MBYTE
13059 "multi_byte",
13060#endif
13061#ifdef FEAT_MBYTE_IME
13062 "multi_byte_ime",
13063#endif
13064#ifdef FEAT_MULTI_LANG
13065 "multi_lang",
13066#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013067#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013068#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013069 "mzscheme",
13070#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072#ifdef FEAT_OLE
13073 "ole",
13074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#ifdef FEAT_PATH_EXTRA
13076 "path_extra",
13077#endif
13078#ifdef FEAT_PERL
13079#ifndef DYNAMIC_PERL
13080 "perl",
13081#endif
13082#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013083#ifdef FEAT_PERSISTENT_UNDO
13084 "persistent_undo",
13085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086#ifdef FEAT_PYTHON
13087#ifndef DYNAMIC_PYTHON
13088 "python",
13089#endif
13090#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013091#ifdef FEAT_PYTHON3
13092#ifndef DYNAMIC_PYTHON3
13093 "python3",
13094#endif
13095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096#ifdef FEAT_POSTSCRIPT
13097 "postscript",
13098#endif
13099#ifdef FEAT_PRINTER
13100 "printer",
13101#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013102#ifdef FEAT_PROFILE
13103 "profile",
13104#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013105#ifdef FEAT_RELTIME
13106 "reltime",
13107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108#ifdef FEAT_QUICKFIX
13109 "quickfix",
13110#endif
13111#ifdef FEAT_RIGHTLEFT
13112 "rightleft",
13113#endif
13114#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13115 "ruby",
13116#endif
13117#ifdef FEAT_SCROLLBIND
13118 "scrollbind",
13119#endif
13120#ifdef FEAT_CMDL_INFO
13121 "showcmd",
13122 "cmdline_info",
13123#endif
13124#ifdef FEAT_SIGNS
13125 "signs",
13126#endif
13127#ifdef FEAT_SMARTINDENT
13128 "smartindent",
13129#endif
13130#ifdef FEAT_SNIFF
13131 "sniff",
13132#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013133#ifdef STARTUPTIME
13134 "startuptime",
13135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#ifdef FEAT_STL_OPT
13137 "statusline",
13138#endif
13139#ifdef FEAT_SUN_WORKSHOP
13140 "sun_workshop",
13141#endif
13142#ifdef FEAT_NETBEANS_INTG
13143 "netbeans_intg",
13144#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013145#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013146 "spell",
13147#endif
13148#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 "syntax",
13150#endif
13151#if defined(USE_SYSTEM) || !defined(UNIX)
13152 "system",
13153#endif
13154#ifdef FEAT_TAG_BINS
13155 "tag_binary",
13156#endif
13157#ifdef FEAT_TAG_OLDSTATIC
13158 "tag_old_static",
13159#endif
13160#ifdef FEAT_TAG_ANYWHITE
13161 "tag_any_white",
13162#endif
13163#ifdef FEAT_TCL
13164# ifndef DYNAMIC_TCL
13165 "tcl",
13166# endif
13167#endif
13168#ifdef TERMINFO
13169 "terminfo",
13170#endif
13171#ifdef FEAT_TERMRESPONSE
13172 "termresponse",
13173#endif
13174#ifdef FEAT_TEXTOBJ
13175 "textobjects",
13176#endif
13177#ifdef HAVE_TGETENT
13178 "tgetent",
13179#endif
13180#ifdef FEAT_TITLE
13181 "title",
13182#endif
13183#ifdef FEAT_TOOLBAR
13184 "toolbar",
13185#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013186#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13187 "unnamedplus",
13188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189#ifdef FEAT_USR_CMDS
13190 "user-commands", /* was accidentally included in 5.4 */
13191 "user_commands",
13192#endif
13193#ifdef FEAT_VIMINFO
13194 "viminfo",
13195#endif
13196#ifdef FEAT_VERTSPLIT
13197 "vertsplit",
13198#endif
13199#ifdef FEAT_VIRTUALEDIT
13200 "virtualedit",
13201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203#ifdef FEAT_VISUALEXTRA
13204 "visualextra",
13205#endif
13206#ifdef FEAT_VREPLACE
13207 "vreplace",
13208#endif
13209#ifdef FEAT_WILDIGN
13210 "wildignore",
13211#endif
13212#ifdef FEAT_WILDMENU
13213 "wildmenu",
13214#endif
13215#ifdef FEAT_WINDOWS
13216 "windows",
13217#endif
13218#ifdef FEAT_WAK
13219 "winaltkeys",
13220#endif
13221#ifdef FEAT_WRITEBACKUP
13222 "writebackup",
13223#endif
13224#ifdef FEAT_XIM
13225 "xim",
13226#endif
13227#ifdef FEAT_XFONTSET
13228 "xfontset",
13229#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013230#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013231 "xpm",
13232 "xpm_w32", /* for backward compatibility */
13233#else
13234# if defined(HAVE_XPM)
13235 "xpm",
13236# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238#ifdef USE_XSMP
13239 "xsmp",
13240#endif
13241#ifdef USE_XSMP_INTERACT
13242 "xsmp_interact",
13243#endif
13244#ifdef FEAT_XCLIPBOARD
13245 "xterm_clipboard",
13246#endif
13247#ifdef FEAT_XTERM_SAVE
13248 "xterm_save",
13249#endif
13250#if defined(UNIX) && defined(FEAT_X11)
13251 "X11",
13252#endif
13253 NULL
13254 };
13255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 for (i = 0; has_list[i] != NULL; ++i)
13258 if (STRICMP(name, has_list[i]) == 0)
13259 {
13260 n = TRUE;
13261 break;
13262 }
13263
13264 if (n == FALSE)
13265 {
13266 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013267 {
13268 if (name[5] == '-'
13269 && STRLEN(name) > 11
13270 && vim_isdigit(name[6])
13271 && vim_isdigit(name[8])
13272 && vim_isdigit(name[10]))
13273 {
13274 int major = atoi((char *)name + 6);
13275 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013276
13277 /* Expect "patch-9.9.01234". */
13278 n = (major < VIM_VERSION_MAJOR
13279 || (major == VIM_VERSION_MAJOR
13280 && (minor < VIM_VERSION_MINOR
13281 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013282 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013283 }
13284 else
13285 n = has_patch(atoi((char *)name + 5));
13286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 else if (STRICMP(name, "vim_starting") == 0)
13288 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013289#ifdef FEAT_MBYTE
13290 else if (STRICMP(name, "multi_byte_encoding") == 0)
13291 n = has_mbyte;
13292#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013293#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13294 else if (STRICMP(name, "balloon_multiline") == 0)
13295 n = multiline_balloon_available();
13296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297#ifdef DYNAMIC_TCL
13298 else if (STRICMP(name, "tcl") == 0)
13299 n = tcl_enabled(FALSE);
13300#endif
13301#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13302 else if (STRICMP(name, "iconv") == 0)
13303 n = iconv_enabled(FALSE);
13304#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013305#ifdef DYNAMIC_LUA
13306 else if (STRICMP(name, "lua") == 0)
13307 n = lua_enabled(FALSE);
13308#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013309#ifdef DYNAMIC_MZSCHEME
13310 else if (STRICMP(name, "mzscheme") == 0)
13311 n = mzscheme_enabled(FALSE);
13312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef DYNAMIC_RUBY
13314 else if (STRICMP(name, "ruby") == 0)
13315 n = ruby_enabled(FALSE);
13316#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013317#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318#ifdef DYNAMIC_PYTHON
13319 else if (STRICMP(name, "python") == 0)
13320 n = python_enabled(FALSE);
13321#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013322#endif
13323#ifdef FEAT_PYTHON3
13324#ifdef DYNAMIC_PYTHON3
13325 else if (STRICMP(name, "python3") == 0)
13326 n = python3_enabled(FALSE);
13327#endif
13328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329#ifdef DYNAMIC_PERL
13330 else if (STRICMP(name, "perl") == 0)
13331 n = perl_enabled(FALSE);
13332#endif
13333#ifdef FEAT_GUI
13334 else if (STRICMP(name, "gui_running") == 0)
13335 n = (gui.in_use || gui.starting);
13336# ifdef FEAT_GUI_W32
13337 else if (STRICMP(name, "gui_win32s") == 0)
13338 n = gui_is_win32s();
13339# endif
13340# ifdef FEAT_BROWSE
13341 else if (STRICMP(name, "browse") == 0)
13342 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13343# endif
13344#endif
13345#ifdef FEAT_SYN_HL
13346 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013347 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#endif
13349#if defined(WIN3264)
13350 else if (STRICMP(name, "win95") == 0)
13351 n = mch_windows95();
13352#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013353#ifdef FEAT_NETBEANS_INTG
13354 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013355 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 }
13358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360}
13361
13362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013363 * "has_key()" function
13364 */
13365 static void
13366f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013369{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013370 if (argvars[0].v_type != VAR_DICT)
13371 {
13372 EMSG(_(e_dictreq));
13373 return;
13374 }
13375 if (argvars[0].vval.v_dict == NULL)
13376 return;
13377
13378 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013379 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013380}
13381
13382/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013383 * "haslocaldir()" function
13384 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013385 static void
13386f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013387 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013388 typval_T *rettv;
13389{
13390 rettv->vval.v_number = (curwin->w_localdir != NULL);
13391}
13392
13393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 * "hasmapto()" function
13395 */
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400{
13401 char_u *name;
13402 char_u *mode;
13403 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013404 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013407 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 mode = (char_u *)"nvo";
13409 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013412 if (argvars[2].v_type != VAR_UNKNOWN)
13413 abbr = get_tv_number(&argvars[2]);
13414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415
Bram Moolenaar2c932302006-03-18 21:42:09 +000013416 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420}
13421
13422/*
13423 * "histadd()" function
13424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429{
13430#ifdef FEAT_CMDHIST
13431 int histype;
13432 char_u *str;
13433 char_u buf[NUMBUFLEN];
13434#endif
13435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 if (check_restricted() || check_secure())
13438 return;
13439#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13441 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 if (histype >= 0)
13443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 if (*str != NUL)
13446 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013447 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 return;
13451 }
13452 }
13453#endif
13454}
13455
13456/*
13457 * "histdel()" function
13458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013461 typval_T *argvars UNUSED;
13462 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463{
13464#ifdef FEAT_CMDHIST
13465 int n;
13466 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13470 if (str == NULL)
13471 n = 0;
13472 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 else
13480 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 get_tv_string_buf(&argvars[1], buf));
13483 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484#endif
13485}
13486
13487/*
13488 * "histget()" function
13489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494{
13495#ifdef FEAT_CMDHIST
13496 int type;
13497 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13501 if (str == NULL)
13502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 {
13505 type = get_histtype(str);
13506 if (argvars[1].v_type == VAR_UNKNOWN)
13507 idx = get_history_idx(type);
13508 else
13509 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13510 /* -1 on type error */
13511 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517}
13518
13519/*
13520 * "histnr()" function
13521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013524 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526{
13527 int i;
13528
13529#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 char_u *history = get_tv_string_chk(&argvars[0]);
13531
13532 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 if (i >= HIST_CMD && i < HIST_COUNT)
13534 i = get_history_idx(i);
13535 else
13536#endif
13537 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539}
13540
13541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 * "highlightID(name)" function
13543 */
13544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 typval_T *argvars;
13547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550}
13551
13552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553 * "highlight_exists()" function
13554 */
13555 static void
13556f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559{
13560 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13561}
13562
13563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 * "hostname()" function
13565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013568 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
13571 char_u hostname[256];
13572
13573 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->v_type = VAR_STRING;
13575 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576}
13577
13578/*
13579 * iconv() function
13580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013583 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585{
13586#ifdef FEAT_MBYTE
13587 char_u buf1[NUMBUFLEN];
13588 char_u buf2[NUMBUFLEN];
13589 char_u *from, *to, *str;
13590 vimconv_T vimconv;
13591#endif
13592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->v_type = VAR_STRING;
13594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595
13596#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 str = get_tv_string(&argvars[0]);
13598 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13599 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 vimconv.vc_type = CONV_NONE;
13601 convert_setup(&vimconv, from, to);
13602
13603 /* If the encodings are equal, no conversion needed. */
13604 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608
13609 convert_setup(&vimconv, NULL, NULL);
13610 vim_free(from);
13611 vim_free(to);
13612#endif
13613}
13614
13615/*
13616 * "indent()" function
13617 */
13618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013620 typval_T *argvars;
13621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
13623 linenr_T lnum;
13624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630}
13631
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013632/*
13633 * "index()" function
13634 */
13635 static void
13636f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *argvars;
13638 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013639{
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 list_T *l;
13641 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013642 long idx = 0;
13643 int ic = FALSE;
13644
13645 rettv->vval.v_number = -1;
13646 if (argvars[0].v_type != VAR_LIST)
13647 {
13648 EMSG(_(e_listreq));
13649 return;
13650 }
13651 l = argvars[0].vval.v_list;
13652 if (l != NULL)
13653 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013654 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013655 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 int error = FALSE;
13658
Bram Moolenaar758711c2005-02-02 23:11:38 +000013659 /* Start at specified item. Use the cached index that list_find()
13660 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013662 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013663 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 ic = get_tv_number_chk(&argvars[3], &error);
13665 if (error)
13666 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013668
Bram Moolenaar758711c2005-02-02 23:11:38 +000013669 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013670 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013671 {
13672 rettv->vval.v_number = idx;
13673 break;
13674 }
13675 }
13676}
13677
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678static int inputsecret_flag = 0;
13679
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013680static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13681
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013683 * This function is used by f_input() and f_inputdialog() functions. The third
13684 * argument to f_input() specifies the type of completion to use at the
13685 * prompt. The third argument to f_inputdialog() specifies the value to return
13686 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 */
13688 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013689get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013692 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 char_u *p = NULL;
13696 int c;
13697 char_u buf[NUMBUFLEN];
13698 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013700 int xp_type = EXPAND_NOTHING;
13701 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705
13706#ifdef NO_CONSOLE_INPUT
13707 /* While starting up, there is no place to enter text. */
13708 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#endif
13711
13712 cmd_silent = FALSE; /* Want to see the prompt. */
13713 if (prompt != NULL)
13714 {
13715 /* Only the part of the message after the last NL is considered as
13716 * prompt for the command line */
13717 p = vim_strrchr(prompt, '\n');
13718 if (p == NULL)
13719 p = prompt;
13720 else
13721 {
13722 ++p;
13723 c = *p;
13724 *p = NUL;
13725 msg_start();
13726 msg_clr_eos();
13727 msg_puts_attr(prompt, echo_attr);
13728 msg_didout = FALSE;
13729 msg_starthere();
13730 *p = c;
13731 }
13732 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 if (argvars[1].v_type != VAR_UNKNOWN)
13735 {
13736 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13737 if (defstr != NULL)
13738 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013740 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013741 {
13742 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013743 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013744 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013745
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013746 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013747 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013748
Bram Moolenaar4463f292005-09-25 22:20:24 +000013749 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13750 if (xp_name == NULL)
13751 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013752
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013753 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013754
Bram Moolenaar4463f292005-09-25 22:20:24 +000013755 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13756 &xp_arg) == FAIL)
13757 return;
13758 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013759 }
13760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013762 {
13763# ifdef FEAT_EX_EXTRA
13764 int save_ex_normal_busy = ex_normal_busy;
13765 ex_normal_busy = 0;
13766# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013768 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13769 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013770# ifdef FEAT_EX_EXTRA
13771 ex_normal_busy = save_ex_normal_busy;
13772# endif
13773 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013774 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013775 && argvars[1].v_type != VAR_UNKNOWN
13776 && argvars[2].v_type != VAR_UNKNOWN)
13777 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13778 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013779
13780 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 /* since the user typed this, no need to wait for return */
13783 need_wait_return = FALSE;
13784 msg_didout = FALSE;
13785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 cmd_silent = cmd_silent_save;
13787}
13788
13789/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013790 * "input()" function
13791 * Also handles inputsecret() when inputsecret is set.
13792 */
13793 static void
13794f_input(argvars, rettv)
13795 typval_T *argvars;
13796 typval_T *rettv;
13797{
13798 get_user_input(argvars, rettv, FALSE);
13799}
13800
13801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 * "inputdialog()" function
13803 */
13804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 typval_T *argvars;
13807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808{
13809#if defined(FEAT_GUI_TEXTDIALOG)
13810 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13811 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13812 {
13813 char_u *message;
13814 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 message = get_tv_string_chk(&argvars[0]);
13818 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013819 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013820 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 else
13822 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 if (message != NULL && defstr != NULL
13824 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013825 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 else
13828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 if (message != NULL && defstr != NULL
13830 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013831 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->vval.v_string = vim_strsave(
13833 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 }
13839 else
13840#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013841 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842}
13843
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013844/*
13845 * "inputlist()" function
13846 */
13847 static void
13848f_inputlist(argvars, rettv)
13849 typval_T *argvars;
13850 typval_T *rettv;
13851{
13852 listitem_T *li;
13853 int selected;
13854 int mouse_used;
13855
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013856#ifdef NO_CONSOLE_INPUT
13857 /* While starting up, there is no place to enter text. */
13858 if (no_console_input())
13859 return;
13860#endif
13861 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13862 {
13863 EMSG2(_(e_listarg), "inputlist()");
13864 return;
13865 }
13866
13867 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013868 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013869 lines_left = Rows; /* avoid more prompt */
13870 msg_scroll = TRUE;
13871 msg_clr_eos();
13872
13873 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13874 {
13875 msg_puts(get_tv_string(&li->li_tv));
13876 msg_putchar('\n');
13877 }
13878
13879 /* Ask for choice. */
13880 selected = prompt_for_number(&mouse_used);
13881 if (mouse_used)
13882 selected -= lines_left;
13883
13884 rettv->vval.v_number = selected;
13885}
13886
13887
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13889
13890/*
13891 * "inputrestore()" function
13892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897{
13898 if (ga_userinput.ga_len > 0)
13899 {
13900 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13902 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013903 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 }
13905 else if (p_verbose > 1)
13906 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013907 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 }
13910}
13911
13912/*
13913 * "inputsave()" function
13914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013920 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 if (ga_grow(&ga_userinput, 1) == OK)
13922 {
13923 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13924 + ga_userinput.ga_len);
13925 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013926 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 }
13928 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930}
13931
13932/*
13933 * "inputsecret()" function
13934 */
13935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013936f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 typval_T *argvars;
13938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939{
13940 ++cmdline_star;
13941 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 --cmdline_star;
13944 --inputsecret_flag;
13945}
13946
13947/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013948 * "insert()" function
13949 */
13950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013952 typval_T *argvars;
13953 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954{
13955 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013956 listitem_T *item;
13957 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013959
13960 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013962 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013963 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013964 {
13965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 before = get_tv_number_chk(&argvars[2], &error);
13967 if (error)
13968 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013969
Bram Moolenaar758711c2005-02-02 23:11:38 +000013970 if (before == l->lv_len)
13971 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013972 else
13973 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013974 item = list_find(l, before);
13975 if (item == NULL)
13976 {
13977 EMSGN(_(e_listidx), before);
13978 l = NULL;
13979 }
13980 }
13981 if (l != NULL)
13982 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013983 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013984 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013985 }
13986 }
13987}
13988
13989/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013990 * "invert(expr)" function
13991 */
13992 static void
13993f_invert(argvars, rettv)
13994 typval_T *argvars;
13995 typval_T *rettv;
13996{
13997 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13998}
13999
14000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 * "isdirectory()" function
14002 */
14003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014005 typval_T *argvars;
14006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014008 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009}
14010
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014011/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014012 * "islocked()" function
14013 */
14014 static void
14015f_islocked(argvars, rettv)
14016 typval_T *argvars;
14017 typval_T *rettv;
14018{
14019 lval_T lv;
14020 char_u *end;
14021 dictitem_T *di;
14022
14023 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014024 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14025 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014026 if (end != NULL && lv.ll_name != NULL)
14027 {
14028 if (*end != NUL)
14029 EMSG(_(e_trailing));
14030 else
14031 {
14032 if (lv.ll_tv == NULL)
14033 {
14034 if (check_changedtick(lv.ll_name))
14035 rettv->vval.v_number = 1; /* always locked */
14036 else
14037 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014038 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014039 if (di != NULL)
14040 {
14041 /* Consider a variable locked when:
14042 * 1. the variable itself is locked
14043 * 2. the value of the variable is locked.
14044 * 3. the List or Dict value is locked.
14045 */
14046 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14047 || tv_islocked(&di->di_tv));
14048 }
14049 }
14050 }
14051 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014052 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014053 else if (lv.ll_newkey != NULL)
14054 EMSG2(_(e_dictkey), lv.ll_newkey);
14055 else if (lv.ll_list != NULL)
14056 /* List item. */
14057 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14058 else
14059 /* Dictionary item. */
14060 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14061 }
14062 }
14063
14064 clear_lval(&lv);
14065}
14066
Bram Moolenaar33570922005-01-25 22:26:29 +000014067static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014068
14069/*
14070 * Turn a dict into a list:
14071 * "what" == 0: list of keys
14072 * "what" == 1: list of values
14073 * "what" == 2: list of items
14074 */
14075 static void
14076dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014079 int what;
14080{
Bram Moolenaar33570922005-01-25 22:26:29 +000014081 list_T *l2;
14082 dictitem_T *di;
14083 hashitem_T *hi;
14084 listitem_T *li;
14085 listitem_T *li2;
14086 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014087 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014088
Bram Moolenaar8c711452005-01-14 21:53:12 +000014089 if (argvars[0].v_type != VAR_DICT)
14090 {
14091 EMSG(_(e_dictreq));
14092 return;
14093 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014094 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014095 return;
14096
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014097 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014098 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014100 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014103 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014105 --todo;
14106 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014108 li = listitem_alloc();
14109 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014110 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014111 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014112
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014113 if (what == 0)
14114 {
14115 /* keys() */
14116 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014117 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014118 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14119 }
14120 else if (what == 1)
14121 {
14122 /* values() */
14123 copy_tv(&di->di_tv, &li->li_tv);
14124 }
14125 else
14126 {
14127 /* items() */
14128 l2 = list_alloc();
14129 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014130 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 li->li_tv.vval.v_list = l2;
14132 if (l2 == NULL)
14133 break;
14134 ++l2->lv_refcount;
14135
14136 li2 = listitem_alloc();
14137 if (li2 == NULL)
14138 break;
14139 list_append(l2, li2);
14140 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014141 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014142 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14143
14144 li2 = listitem_alloc();
14145 if (li2 == NULL)
14146 break;
14147 list_append(l2, li2);
14148 copy_tv(&di->di_tv, &li2->li_tv);
14149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014150 }
14151 }
14152}
14153
14154/*
14155 * "items(dict)" function
14156 */
14157 static void
14158f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014161{
14162 dict_list(argvars, rettv, 2);
14163}
14164
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014166 * "join()" function
14167 */
14168 static void
14169f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *argvars;
14171 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014172{
14173 garray_T ga;
14174 char_u *sep;
14175
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014176 if (argvars[0].v_type != VAR_LIST)
14177 {
14178 EMSG(_(e_listreq));
14179 return;
14180 }
14181 if (argvars[0].vval.v_list == NULL)
14182 return;
14183 if (argvars[1].v_type == VAR_UNKNOWN)
14184 sep = (char_u *)" ";
14185 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014187
14188 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189
14190 if (sep != NULL)
14191 {
14192 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014193 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 ga_append(&ga, NUL);
14195 rettv->vval.v_string = (char_u *)ga.ga_data;
14196 }
14197 else
14198 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014199}
14200
14201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014202 * "keys()" function
14203 */
14204 static void
14205f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014208{
14209 dict_list(argvars, rettv, 0);
14210}
14211
14212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 * "last_buffer_nr()" function.
14214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014217 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219{
14220 int n = 0;
14221 buf_T *buf;
14222
14223 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14224 if (n < buf->b_fnum)
14225 n = buf->b_fnum;
14226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014228}
14229
14230/*
14231 * "len()" function
14232 */
14233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 typval_T *argvars;
14236 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014237{
14238 switch (argvars[0].v_type)
14239 {
14240 case VAR_STRING:
14241 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = (varnumber_T)STRLEN(
14243 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014244 break;
14245 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014248 case VAR_DICT:
14249 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14250 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014251 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014252 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014253 break;
14254 }
14255}
14256
Bram Moolenaar33570922005-01-25 22:26:29 +000014257static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014258
14259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 typval_T *argvars;
14262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 int type;
14264{
14265#ifdef FEAT_LIBCALL
14266 char_u *string_in;
14267 char_u **string_result;
14268 int nr_result;
14269#endif
14270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014272 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274
14275 if (check_restricted() || check_secure())
14276 return;
14277
14278#ifdef FEAT_LIBCALL
14279 /* The first two args must be strings, otherwise its meaningless */
14280 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14281 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014282 string_in = NULL;
14283 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284 string_in = argvars[2].vval.v_string;
14285 if (type == VAR_NUMBER)
14286 string_result = NULL;
14287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014289 if (mch_libcall(argvars[0].vval.v_string,
14290 argvars[1].vval.v_string,
14291 string_in,
14292 argvars[2].vval.v_number,
14293 string_result,
14294 &nr_result) == OK
14295 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014296 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014297 }
14298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299}
14300
14301/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014302 * "libcall()" function
14303 */
14304 static void
14305f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014306 typval_T *argvars;
14307 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308{
14309 libcall_common(argvars, rettv, VAR_STRING);
14310}
14311
14312/*
14313 * "libcallnr()" function
14314 */
14315 static void
14316f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014317 typval_T *argvars;
14318 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319{
14320 libcall_common(argvars, rettv, VAR_NUMBER);
14321}
14322
14323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 * "line(string)" function
14325 */
14326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
14331 linenr_T lnum = 0;
14332 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014333 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014335 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 if (fp != NULL)
14337 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
14342 * "line2byte(lnum)" function
14343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
14349#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351#else
14352 linenr_T lnum;
14353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14359 if (rettv->vval.v_number >= 0)
14360 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361#endif
14362}
14363
14364/*
14365 * "lispindent(lnum)" function
14366 */
14367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
14372#ifdef FEAT_LISP
14373 pos_T pos;
14374 linenr_T lnum;
14375
14376 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14379 {
14380 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 curwin->w_cursor = pos;
14383 }
14384 else
14385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387}
14388
14389/*
14390 * "localtime()" function
14391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014394 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398}
14399
Bram Moolenaar33570922005-01-25 22:26:29 +000014400static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 typval_T *argvars;
14405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 int exact;
14407{
14408 char_u *keys;
14409 char_u *which;
14410 char_u buf[NUMBUFLEN];
14411 char_u *keys_buf = NULL;
14412 char_u *rhs;
14413 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014414 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014415 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014416 mapblock_T *mp;
14417 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
14419 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420 rettv->v_type = VAR_STRING;
14421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 if (*keys == NUL)
14425 return;
14426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014431 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014432 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014433 if (argvars[3].v_type != VAR_UNKNOWN)
14434 get_dict = get_tv_number(&argvars[3]);
14435 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 else
14438 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 if (which == NULL)
14440 return;
14441
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 mode = get_map_mode(&which, 0);
14443
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014444 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014445 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014447
14448 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014450 /* Return a string. */
14451 if (rhs != NULL)
14452 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453
Bram Moolenaarbd743252010-10-20 21:23:33 +020014454 }
14455 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14456 {
14457 /* Return a dictionary. */
14458 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14459 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14460 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
Bram Moolenaarbd743252010-10-20 21:23:33 +020014462 dict_add_nr_str(dict, "lhs", 0L, lhs);
14463 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14464 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14465 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14466 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14467 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14468 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014469 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014470 dict_add_nr_str(dict, "mode", 0L, mapmode);
14471
14472 vim_free(lhs);
14473 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 }
14475}
14476
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014477#ifdef FEAT_FLOAT
14478/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014479 * "log()" function
14480 */
14481 static void
14482f_log(argvars, rettv)
14483 typval_T *argvars;
14484 typval_T *rettv;
14485{
14486 float_T f;
14487
14488 rettv->v_type = VAR_FLOAT;
14489 if (get_float_arg(argvars, &f) == OK)
14490 rettv->vval.v_float = log(f);
14491 else
14492 rettv->vval.v_float = 0.0;
14493}
14494
14495/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014496 * "log10()" function
14497 */
14498 static void
14499f_log10(argvars, rettv)
14500 typval_T *argvars;
14501 typval_T *rettv;
14502{
14503 float_T f;
14504
14505 rettv->v_type = VAR_FLOAT;
14506 if (get_float_arg(argvars, &f) == OK)
14507 rettv->vval.v_float = log10(f);
14508 else
14509 rettv->vval.v_float = 0.0;
14510}
14511#endif
14512
Bram Moolenaar1dced572012-04-05 16:54:08 +020014513#ifdef FEAT_LUA
14514/*
14515 * "luaeval()" function
14516 */
14517 static void
14518f_luaeval(argvars, rettv)
14519 typval_T *argvars;
14520 typval_T *rettv;
14521{
14522 char_u *str;
14523 char_u buf[NUMBUFLEN];
14524
14525 str = get_tv_string_buf(&argvars[0], buf);
14526 do_luaeval(str, argvars + 1, rettv);
14527}
14528#endif
14529
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014531 * "map()" function
14532 */
14533 static void
14534f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 typval_T *argvars;
14536 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014537{
14538 filter_map(argvars, rettv, TRUE);
14539}
14540
14541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 */
14544 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 typval_T *argvars;
14547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550}
14551
14552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 */
14555 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *argvars;
14558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561}
14562
Bram Moolenaar33570922005-01-25 22:26:29 +000014563static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564
14565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 typval_T *argvars;
14568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 int type;
14570{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014571 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014572 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014573 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 char_u *pat;
14575 regmatch_T regmatch;
14576 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014577 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 char_u *save_cpo;
14579 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014580 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014581 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014582 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 list_T *l = NULL;
14584 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014585 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014586 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587
14588 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14589 save_cpo = p_cpo;
14590 p_cpo = (char_u *)"";
14591
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014592 rettv->vval.v_number = -1;
14593 if (type == 3)
14594 {
14595 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014596 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014597 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014598 }
14599 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv->v_type = VAR_STRING;
14602 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014605 if (argvars[0].v_type == VAR_LIST)
14606 {
14607 if ((l = argvars[0].vval.v_list) == NULL)
14608 goto theend;
14609 li = l->lv_first;
14610 }
14611 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014612 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014613 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014614 len = (long)STRLEN(str);
14615 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14618 if (pat == NULL)
14619 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014620
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 int error = FALSE;
14624
14625 start = get_tv_number_chk(&argvars[2], &error);
14626 if (error)
14627 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014628 if (l != NULL)
14629 {
14630 li = list_find(l, start);
14631 if (li == NULL)
14632 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014633 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014634 }
14635 else
14636 {
14637 if (start < 0)
14638 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014639 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014640 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014641 /* When "count" argument is there ignore matches before "start",
14642 * otherwise skip part of the string. Differs when pattern is "^"
14643 * or "\<". */
14644 if (argvars[3].v_type != VAR_UNKNOWN)
14645 startcol = start;
14646 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014647 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014648 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014649 len -= start;
14650 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014651 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014652
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 nth = get_tv_number_chk(&argvars[3], &error);
14655 if (error)
14656 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 }
14658
14659 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14660 if (regmatch.regprog != NULL)
14661 {
14662 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014663
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014664 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014665 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014666 if (l != NULL)
14667 {
14668 if (li == NULL)
14669 {
14670 match = FALSE;
14671 break;
14672 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014674 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014675 if (str == NULL)
14676 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014677 }
14678
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014679 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014680
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014681 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014682 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014683 if (l == NULL && !match)
14684 break;
14685
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014686 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014687 if (l != NULL)
14688 {
14689 li = li->li_next;
14690 ++idx;
14691 }
14692 else
14693 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014694#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014695 startcol = (colnr_T)(regmatch.startp[0]
14696 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014697#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014698 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014699#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014700 if (startcol > (colnr_T)len
14701 || str + startcol <= regmatch.startp[0])
14702 {
14703 match = FALSE;
14704 break;
14705 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014706 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014707 }
14708
14709 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014712 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014713 int i;
14714
14715 /* return list with matched string and submatches */
14716 for (i = 0; i < NSUBEXP; ++i)
14717 {
14718 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014719 {
14720 if (list_append_string(rettv->vval.v_list,
14721 (char_u *)"", 0) == FAIL)
14722 break;
14723 }
14724 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014725 regmatch.startp[i],
14726 (int)(regmatch.endp[i] - regmatch.startp[i]))
14727 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 }
14730 }
14731 else if (type == 2)
14732 {
14733 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734 if (l != NULL)
14735 copy_tv(&li->li_tv, rettv);
14736 else
14737 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014739 }
14740 else if (l != NULL)
14741 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 else
14743 {
14744 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 (varnumber_T)(regmatch.startp[0] - str);
14747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014750 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 }
14752 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014753 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 }
14755
14756theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 p_cpo = save_cpo;
14759}
14760
14761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 * "match()" function
14763 */
14764 static void
14765f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014766 typval_T *argvars;
14767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768{
14769 find_some_match(argvars, rettv, 1);
14770}
14771
14772/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014773 * "matchadd()" function
14774 */
14775 static void
14776f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014777 typval_T *argvars UNUSED;
14778 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014779{
14780#ifdef FEAT_SEARCH_EXTRA
14781 char_u buf[NUMBUFLEN];
14782 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14783 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14784 int prio = 10; /* default priority */
14785 int id = -1;
14786 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014787 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014788
14789 rettv->vval.v_number = -1;
14790
14791 if (grp == NULL || pat == NULL)
14792 return;
14793 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014794 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014795 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014796 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014797 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014798 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014799 if (argvars[4].v_type != VAR_UNKNOWN)
14800 {
14801 if (argvars[4].v_type != VAR_DICT)
14802 {
14803 EMSG(_(e_dictreq));
14804 return;
14805 }
14806 if (dict_find(argvars[4].vval.v_dict,
14807 (char_u *)"conceal", -1) != NULL)
14808 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14809 (char_u *)"conceal", FALSE);
14810 }
14811 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014812 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014813 if (error == TRUE)
14814 return;
14815 if (id >= 1 && id <= 3)
14816 {
14817 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14818 return;
14819 }
14820
Bram Moolenaar6561d522015-07-21 15:48:27 +020014821 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14822 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014823#endif
14824}
14825
14826/*
14827 * "matchaddpos()" function
14828 */
14829 static void
14830f_matchaddpos(argvars, rettv)
14831 typval_T *argvars UNUSED;
14832 typval_T *rettv UNUSED;
14833{
14834#ifdef FEAT_SEARCH_EXTRA
14835 char_u buf[NUMBUFLEN];
14836 char_u *group;
14837 int prio = 10;
14838 int id = -1;
14839 int error = FALSE;
14840 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014841 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014842
14843 rettv->vval.v_number = -1;
14844
14845 group = get_tv_string_buf_chk(&argvars[0], buf);
14846 if (group == NULL)
14847 return;
14848
14849 if (argvars[1].v_type != VAR_LIST)
14850 {
14851 EMSG2(_(e_listarg), "matchaddpos()");
14852 return;
14853 }
14854 l = argvars[1].vval.v_list;
14855 if (l == NULL)
14856 return;
14857
14858 if (argvars[2].v_type != VAR_UNKNOWN)
14859 {
14860 prio = get_tv_number_chk(&argvars[2], &error);
14861 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014862 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014863 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014864 if (argvars[4].v_type != VAR_UNKNOWN)
14865 {
14866 if (argvars[4].v_type != VAR_DICT)
14867 {
14868 EMSG(_(e_dictreq));
14869 return;
14870 }
14871 if (dict_find(argvars[4].vval.v_dict,
14872 (char_u *)"conceal", -1) != NULL)
14873 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14874 (char_u *)"conceal", FALSE);
14875 }
14876 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014877 }
14878 if (error == TRUE)
14879 return;
14880
14881 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14882 if (id == 1 || id == 2)
14883 {
14884 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14885 return;
14886 }
14887
Bram Moolenaar6561d522015-07-21 15:48:27 +020014888 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14889 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014890#endif
14891}
14892
14893/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014894 * "matcharg()" function
14895 */
14896 static void
14897f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014898 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014899 typval_T *rettv;
14900{
14901 if (rettv_list_alloc(rettv) == OK)
14902 {
14903#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014904 int id = get_tv_number(&argvars[0]);
14905 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014906
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014907 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014908 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014909 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14910 {
14911 list_append_string(rettv->vval.v_list,
14912 syn_id2name(m->hlg_id), -1);
14913 list_append_string(rettv->vval.v_list, m->pattern, -1);
14914 }
14915 else
14916 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014917 list_append_string(rettv->vval.v_list, NULL, -1);
14918 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014919 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014920 }
14921#endif
14922 }
14923}
14924
14925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014926 * "matchdelete()" function
14927 */
14928 static void
14929f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014930 typval_T *argvars UNUSED;
14931 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014932{
14933#ifdef FEAT_SEARCH_EXTRA
14934 rettv->vval.v_number = match_delete(curwin,
14935 (int)get_tv_number(&argvars[0]), TRUE);
14936#endif
14937}
14938
14939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 * "matchend()" function
14941 */
14942 static void
14943f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 typval_T *argvars;
14945 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946{
14947 find_some_match(argvars, rettv, 0);
14948}
14949
14950/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014951 * "matchlist()" function
14952 */
14953 static void
14954f_matchlist(argvars, rettv)
14955 typval_T *argvars;
14956 typval_T *rettv;
14957{
14958 find_some_match(argvars, rettv, 3);
14959}
14960
14961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962 * "matchstr()" function
14963 */
14964 static void
14965f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014966 typval_T *argvars;
14967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968{
14969 find_some_match(argvars, rettv, 2);
14970}
14971
Bram Moolenaar33570922005-01-25 22:26:29 +000014972static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014973
14974 static void
14975max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014978 int domax;
14979{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014980 long n = 0;
14981 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014982 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014983
14984 if (argvars[0].v_type == VAR_LIST)
14985 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014986 list_T *l;
14987 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014988
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014989 l = argvars[0].vval.v_list;
14990 if (l != NULL)
14991 {
14992 li = l->lv_first;
14993 if (li != NULL)
14994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014995 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014996 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014997 {
14998 li = li->li_next;
14999 if (li == NULL)
15000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015001 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015002 if (domax ? i > n : i < n)
15003 n = i;
15004 }
15005 }
15006 }
15007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015008 else if (argvars[0].v_type == VAR_DICT)
15009 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015011 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015012 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015013 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015014
15015 d = argvars[0].vval.v_dict;
15016 if (d != NULL)
15017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015018 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015019 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015021 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015023 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015024 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 if (first)
15026 {
15027 n = i;
15028 first = FALSE;
15029 }
15030 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015031 n = i;
15032 }
15033 }
15034 }
15035 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015036 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015037 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015038 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015039}
15040
15041/*
15042 * "max()" function
15043 */
15044 static void
15045f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015046 typval_T *argvars;
15047 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015048{
15049 max_min(argvars, rettv, TRUE);
15050}
15051
15052/*
15053 * "min()" function
15054 */
15055 static void
15056f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 typval_T *argvars;
15058 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015059{
15060 max_min(argvars, rettv, FALSE);
15061}
15062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015063static int mkdir_recurse __ARGS((char_u *dir, int prot));
15064
15065/*
15066 * Create the directory in which "dir" is located, and higher levels when
15067 * needed.
15068 */
15069 static int
15070mkdir_recurse(dir, prot)
15071 char_u *dir;
15072 int prot;
15073{
15074 char_u *p;
15075 char_u *updir;
15076 int r = FAIL;
15077
15078 /* Get end of directory name in "dir".
15079 * We're done when it's "/" or "c:/". */
15080 p = gettail_sep(dir);
15081 if (p <= get_past_head(dir))
15082 return OK;
15083
15084 /* If the directory exists we're done. Otherwise: create it.*/
15085 updir = vim_strnsave(dir, (int)(p - dir));
15086 if (updir == NULL)
15087 return FAIL;
15088 if (mch_isdir(updir))
15089 r = OK;
15090 else if (mkdir_recurse(updir, prot) == OK)
15091 r = vim_mkdir_emsg(updir, prot);
15092 vim_free(updir);
15093 return r;
15094}
15095
15096#ifdef vim_mkdir
15097/*
15098 * "mkdir()" function
15099 */
15100 static void
15101f_mkdir(argvars, rettv)
15102 typval_T *argvars;
15103 typval_T *rettv;
15104{
15105 char_u *dir;
15106 char_u buf[NUMBUFLEN];
15107 int prot = 0755;
15108
15109 rettv->vval.v_number = FAIL;
15110 if (check_restricted() || check_secure())
15111 return;
15112
15113 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015114 if (*dir == NUL)
15115 rettv->vval.v_number = FAIL;
15116 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015117 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015118 if (*gettail(dir) == NUL)
15119 /* remove trailing slashes */
15120 *gettail_sep(dir) = NUL;
15121
15122 if (argvars[1].v_type != VAR_UNKNOWN)
15123 {
15124 if (argvars[2].v_type != VAR_UNKNOWN)
15125 prot = get_tv_number_chk(&argvars[2], NULL);
15126 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15127 mkdir_recurse(dir, prot);
15128 }
15129 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015130 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015131}
15132#endif
15133
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 * "mode()" function
15136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 typval_T *argvars;
15140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015142 char_u buf[3];
15143
15144 buf[1] = NUL;
15145 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 if (VIsual_active)
15148 {
15149 if (VIsual_select)
15150 buf[0] = VIsual_mode + 's' - 'v';
15151 else
15152 buf[0] = VIsual_mode;
15153 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015154 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015155 || State == CONFIRM)
15156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015158 if (State == ASKMORE)
15159 buf[1] = 'm';
15160 else if (State == CONFIRM)
15161 buf[1] = '?';
15162 }
15163 else if (State == EXTERNCMD)
15164 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 else if (State & INSERT)
15166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015167#ifdef FEAT_VREPLACE
15168 if (State & VREPLACE_FLAG)
15169 {
15170 buf[0] = 'R';
15171 buf[1] = 'v';
15172 }
15173 else
15174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 if (State & REPLACE_FLAG)
15176 buf[0] = 'R';
15177 else
15178 buf[0] = 'i';
15179 }
15180 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015183 if (exmode_active)
15184 buf[1] = 'v';
15185 }
15186 else if (exmode_active)
15187 {
15188 buf[0] = 'c';
15189 buf[1] = 'e';
15190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015194 if (finish_op)
15195 buf[1] = 'o';
15196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015198 /* Clear out the minor mode when the argument is not a non-zero number or
15199 * non-empty string. */
15200 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015201 buf[1] = NUL;
15202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015203 rettv->vval.v_string = vim_strsave(buf);
15204 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205}
15206
Bram Moolenaar429fa852013-04-15 12:27:36 +020015207#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015208/*
15209 * "mzeval()" function
15210 */
15211 static void
15212f_mzeval(argvars, rettv)
15213 typval_T *argvars;
15214 typval_T *rettv;
15215{
15216 char_u *str;
15217 char_u buf[NUMBUFLEN];
15218
15219 str = get_tv_string_buf(&argvars[0], buf);
15220 do_mzeval(str, rettv);
15221}
Bram Moolenaar75676462013-01-30 14:55:42 +010015222
15223 void
15224mzscheme_call_vim(name, args, rettv)
15225 char_u *name;
15226 typval_T *args;
15227 typval_T *rettv;
15228{
15229 typval_T argvars[3];
15230
15231 argvars[0].v_type = VAR_STRING;
15232 argvars[0].vval.v_string = name;
15233 copy_tv(args, &argvars[1]);
15234 argvars[2].v_type = VAR_UNKNOWN;
15235 f_call(argvars, rettv);
15236 clear_tv(&argvars[1]);
15237}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015238#endif
15239
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241 * "nextnonblank()" function
15242 */
15243 static void
15244f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015245 typval_T *argvars;
15246 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247{
15248 linenr_T lnum;
15249
15250 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 {
15254 lnum = 0;
15255 break;
15256 }
15257 if (*skipwhite(ml_get(lnum)) != NUL)
15258 break;
15259 }
15260 rettv->vval.v_number = lnum;
15261}
15262
15263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 * "nr2char()" function
15265 */
15266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *argvars;
15269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270{
15271 char_u buf[NUMBUFLEN];
15272
15273#ifdef FEAT_MBYTE
15274 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015275 {
15276 int utf8 = 0;
15277
15278 if (argvars[1].v_type != VAR_UNKNOWN)
15279 utf8 = get_tv_number_chk(&argvars[1], NULL);
15280 if (utf8)
15281 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15282 else
15283 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 else
15286#endif
15287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 buf[1] = NUL;
15290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 rettv->v_type = VAR_STRING;
15292 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015293}
15294
15295/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015296 * "or(expr, expr)" function
15297 */
15298 static void
15299f_or(argvars, rettv)
15300 typval_T *argvars;
15301 typval_T *rettv;
15302{
15303 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15304 | get_tv_number_chk(&argvars[1], NULL);
15305}
15306
15307/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015308 * "pathshorten()" function
15309 */
15310 static void
15311f_pathshorten(argvars, rettv)
15312 typval_T *argvars;
15313 typval_T *rettv;
15314{
15315 char_u *p;
15316
15317 rettv->v_type = VAR_STRING;
15318 p = get_tv_string_chk(&argvars[0]);
15319 if (p == NULL)
15320 rettv->vval.v_string = NULL;
15321 else
15322 {
15323 p = vim_strsave(p);
15324 rettv->vval.v_string = p;
15325 if (p != NULL)
15326 shorten_dir(p);
15327 }
15328}
15329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015330#ifdef FEAT_FLOAT
15331/*
15332 * "pow()" function
15333 */
15334 static void
15335f_pow(argvars, rettv)
15336 typval_T *argvars;
15337 typval_T *rettv;
15338{
15339 float_T fx, fy;
15340
15341 rettv->v_type = VAR_FLOAT;
15342 if (get_float_arg(argvars, &fx) == OK
15343 && get_float_arg(&argvars[1], &fy) == OK)
15344 rettv->vval.v_float = pow(fx, fy);
15345 else
15346 rettv->vval.v_float = 0.0;
15347}
15348#endif
15349
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 * "prevnonblank()" function
15352 */
15353 static void
15354f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015355 typval_T *argvars;
15356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357{
15358 linenr_T lnum;
15359
15360 lnum = get_tv_lnum(argvars);
15361 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15362 lnum = 0;
15363 else
15364 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15365 --lnum;
15366 rettv->vval.v_number = lnum;
15367}
15368
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015369#ifdef HAVE_STDARG_H
15370/* This dummy va_list is here because:
15371 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15372 * - locally in the function results in a "used before set" warning
15373 * - using va_start() to initialize it gives "function with fixed args" error */
15374static va_list ap;
15375#endif
15376
Bram Moolenaar8c711452005-01-14 21:53:12 +000015377/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015378 * "printf()" function
15379 */
15380 static void
15381f_printf(argvars, rettv)
15382 typval_T *argvars;
15383 typval_T *rettv;
15384{
15385 rettv->v_type = VAR_STRING;
15386 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015387#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015388 {
15389 char_u buf[NUMBUFLEN];
15390 int len;
15391 char_u *s;
15392 int saved_did_emsg = did_emsg;
15393 char *fmt;
15394
15395 /* Get the required length, allocate the buffer and do it for real. */
15396 did_emsg = FALSE;
15397 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015398 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015399 if (!did_emsg)
15400 {
15401 s = alloc(len + 1);
15402 if (s != NULL)
15403 {
15404 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015405 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015406 }
15407 }
15408 did_emsg |= saved_did_emsg;
15409 }
15410#endif
15411}
15412
15413/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015414 * "pumvisible()" function
15415 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015416 static void
15417f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015418 typval_T *argvars UNUSED;
15419 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015420{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015421#ifdef FEAT_INS_EXPAND
15422 if (pum_visible())
15423 rettv->vval.v_number = 1;
15424#endif
15425}
15426
Bram Moolenaardb913952012-06-29 12:54:53 +020015427#ifdef FEAT_PYTHON3
15428/*
15429 * "py3eval()" function
15430 */
15431 static void
15432f_py3eval(argvars, rettv)
15433 typval_T *argvars;
15434 typval_T *rettv;
15435{
15436 char_u *str;
15437 char_u buf[NUMBUFLEN];
15438
15439 str = get_tv_string_buf(&argvars[0], buf);
15440 do_py3eval(str, rettv);
15441}
15442#endif
15443
15444#ifdef FEAT_PYTHON
15445/*
15446 * "pyeval()" function
15447 */
15448 static void
15449f_pyeval(argvars, rettv)
15450 typval_T *argvars;
15451 typval_T *rettv;
15452{
15453 char_u *str;
15454 char_u buf[NUMBUFLEN];
15455
15456 str = get_tv_string_buf(&argvars[0], buf);
15457 do_pyeval(str, rettv);
15458}
15459#endif
15460
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015461/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015462 * "range()" function
15463 */
15464 static void
15465f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 typval_T *argvars;
15467 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015468{
15469 long start;
15470 long end;
15471 long stride = 1;
15472 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015475 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015476 if (argvars[1].v_type == VAR_UNKNOWN)
15477 {
15478 end = start - 1;
15479 start = 0;
15480 }
15481 else
15482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015484 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015485 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015486 }
15487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015488 if (error)
15489 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015490 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015491 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015492 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015493 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015494 else
15495 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015496 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015497 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015498 if (list_append_number(rettv->vval.v_list,
15499 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015500 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015501 }
15502}
15503
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015504/*
15505 * "readfile()" function
15506 */
15507 static void
15508f_readfile(argvars, rettv)
15509 typval_T *argvars;
15510 typval_T *rettv;
15511{
15512 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015513 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015514 char_u *fname;
15515 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015516 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15517 int io_size = sizeof(buf);
15518 int readlen; /* size of last fread() */
15519 char_u *prev = NULL; /* previously read bytes, if any */
15520 long prevlen = 0; /* length of data in prev */
15521 long prevsize = 0; /* size of prev buffer */
15522 long maxline = MAXLNUM;
15523 long cnt = 0;
15524 char_u *p; /* position in buf */
15525 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015527 if (argvars[1].v_type != VAR_UNKNOWN)
15528 {
15529 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15530 binary = TRUE;
15531 if (argvars[2].v_type != VAR_UNKNOWN)
15532 maxline = get_tv_number(&argvars[2]);
15533 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015535 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015536 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015537
15538 /* Always open the file in binary mode, library functions have a mind of
15539 * their own about CR-LF conversion. */
15540 fname = get_tv_string(&argvars[0]);
15541 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15542 {
15543 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15544 return;
15545 }
15546
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015547 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015549 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015550
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015551 /* This for loop processes what was read, but is also entered at end
15552 * of file so that either:
15553 * - an incomplete line gets written
15554 * - a "binary" file gets an empty line at the end if it ends in a
15555 * newline. */
15556 for (p = buf, start = buf;
15557 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15558 ++p)
15559 {
15560 if (*p == '\n' || readlen <= 0)
15561 {
15562 listitem_T *li;
15563 char_u *s = NULL;
15564 long_u len = p - start;
15565
15566 /* Finished a line. Remove CRs before NL. */
15567 if (readlen > 0 && !binary)
15568 {
15569 while (len > 0 && start[len - 1] == '\r')
15570 --len;
15571 /* removal may cross back to the "prev" string */
15572 if (len == 0)
15573 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15574 --prevlen;
15575 }
15576 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015577 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015578 else
15579 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015580 /* Change "prev" buffer to be the right size. This way
15581 * the bytes are only copied once, and very long lines are
15582 * allocated only once. */
15583 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015584 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015585 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015586 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015587 prev = NULL; /* the list will own the string */
15588 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015589 }
15590 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015591 if (s == NULL)
15592 {
15593 do_outofmem_msg((long_u) prevlen + len + 1);
15594 failed = TRUE;
15595 break;
15596 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015598 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 {
15600 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015601 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015602 break;
15603 }
15604 li->li_tv.v_type = VAR_STRING;
15605 li->li_tv.v_lock = 0;
15606 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015607 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015608
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015609 start = p + 1; /* step over newline */
15610 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015611 break;
15612 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015613 else if (*p == NUL)
15614 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015615#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015616 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15617 * when finding the BF and check the previous two bytes. */
15618 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015619 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015620 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15621 * + 1, these may be in the "prev" string. */
15622 char_u back1 = p >= buf + 1 ? p[-1]
15623 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15624 char_u back2 = p >= buf + 2 ? p[-2]
15625 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15626 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015627
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015628 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015629 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015630 char_u *dest = p - 2;
15631
15632 /* Usually a BOM is at the beginning of a file, and so at
15633 * the beginning of a line; then we can just step over it.
15634 */
15635 if (start == dest)
15636 start = p + 1;
15637 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015638 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015639 /* have to shuffle buf to close gap */
15640 int adjust_prevlen = 0;
15641
15642 if (dest < buf)
15643 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015644 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015645 dest = buf;
15646 }
15647 if (readlen > p - buf + 1)
15648 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15649 readlen -= 3 - adjust_prevlen;
15650 prevlen -= adjust_prevlen;
15651 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015652 }
15653 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015654 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015655#endif
15656 } /* for */
15657
15658 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15659 break;
15660 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015661 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015662 /* There's part of a line in buf, store it in "prev". */
15663 if (p - start + prevlen >= prevsize)
15664 {
15665 /* need bigger "prev" buffer */
15666 char_u *newprev;
15667
15668 /* A common use case is ordinary text files and "prev" gets a
15669 * fragment of a line, so the first allocation is made
15670 * small, to avoid repeatedly 'allocing' large and
15671 * 'reallocing' small. */
15672 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015673 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015674 else
15675 {
15676 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015677 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015678 prevsize = grow50pc > growmin ? grow50pc : growmin;
15679 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015680 newprev = prev == NULL ? alloc(prevsize)
15681 : vim_realloc(prev, prevsize);
15682 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015683 {
15684 do_outofmem_msg((long_u)prevsize);
15685 failed = TRUE;
15686 break;
15687 }
15688 prev = newprev;
15689 }
15690 /* Add the line part to end of "prev". */
15691 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015692 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015694 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015695
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015696 /*
15697 * For a negative line count use only the lines at the end of the file,
15698 * free the rest.
15699 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015700 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015701 while (cnt > -maxline)
15702 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015703 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015704 --cnt;
15705 }
15706
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015707 if (failed)
15708 {
15709 list_free(rettv->vval.v_list, TRUE);
15710 /* readfile doc says an empty list is returned on error */
15711 rettv->vval.v_list = list_alloc();
15712 }
15713
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015714 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015715 fclose(fd);
15716}
15717
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015718#if defined(FEAT_RELTIME)
15719static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15720
15721/*
15722 * Convert a List to proftime_T.
15723 * Return FAIL when there is something wrong.
15724 */
15725 static int
15726list2proftime(arg, tm)
15727 typval_T *arg;
15728 proftime_T *tm;
15729{
15730 long n1, n2;
15731 int error = FALSE;
15732
15733 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15734 || arg->vval.v_list->lv_len != 2)
15735 return FAIL;
15736 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15737 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15738# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015739 tm->HighPart = n1;
15740 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015741# else
15742 tm->tv_sec = n1;
15743 tm->tv_usec = n2;
15744# endif
15745 return error ? FAIL : OK;
15746}
15747#endif /* FEAT_RELTIME */
15748
15749/*
15750 * "reltime()" function
15751 */
15752 static void
15753f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015754 typval_T *argvars UNUSED;
15755 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015756{
15757#ifdef FEAT_RELTIME
15758 proftime_T res;
15759 proftime_T start;
15760
15761 if (argvars[0].v_type == VAR_UNKNOWN)
15762 {
15763 /* No arguments: get current time. */
15764 profile_start(&res);
15765 }
15766 else if (argvars[1].v_type == VAR_UNKNOWN)
15767 {
15768 if (list2proftime(&argvars[0], &res) == FAIL)
15769 return;
15770 profile_end(&res);
15771 }
15772 else
15773 {
15774 /* Two arguments: compute the difference. */
15775 if (list2proftime(&argvars[0], &start) == FAIL
15776 || list2proftime(&argvars[1], &res) == FAIL)
15777 return;
15778 profile_sub(&res, &start);
15779 }
15780
15781 if (rettv_list_alloc(rettv) == OK)
15782 {
15783 long n1, n2;
15784
15785# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015786 n1 = res.HighPart;
15787 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015788# else
15789 n1 = res.tv_sec;
15790 n2 = res.tv_usec;
15791# endif
15792 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15793 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15794 }
15795#endif
15796}
15797
15798/*
15799 * "reltimestr()" function
15800 */
15801 static void
15802f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015803 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015804 typval_T *rettv;
15805{
15806#ifdef FEAT_RELTIME
15807 proftime_T tm;
15808#endif
15809
15810 rettv->v_type = VAR_STRING;
15811 rettv->vval.v_string = NULL;
15812#ifdef FEAT_RELTIME
15813 if (list2proftime(&argvars[0], &tm) == OK)
15814 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15815#endif
15816}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015817
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15819static void make_connection __ARGS((void));
15820static int check_connection __ARGS((void));
15821
15822 static void
15823make_connection()
15824{
15825 if (X_DISPLAY == NULL
15826# ifdef FEAT_GUI
15827 && !gui.in_use
15828# endif
15829 )
15830 {
15831 x_force_connect = TRUE;
15832 setup_term_clip();
15833 x_force_connect = FALSE;
15834 }
15835}
15836
15837 static int
15838check_connection()
15839{
15840 make_connection();
15841 if (X_DISPLAY == NULL)
15842 {
15843 EMSG(_("E240: No connection to Vim server"));
15844 return FAIL;
15845 }
15846 return OK;
15847}
15848#endif
15849
15850#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015851static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852
15853 static void
15854remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015855 typval_T *argvars;
15856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015857 int expr;
15858{
15859 char_u *server_name;
15860 char_u *keys;
15861 char_u *r = NULL;
15862 char_u buf[NUMBUFLEN];
15863# ifdef WIN32
15864 HWND w;
15865# else
15866 Window w;
15867# endif
15868
15869 if (check_restricted() || check_secure())
15870 return;
15871
15872# ifdef FEAT_X11
15873 if (check_connection() == FAIL)
15874 return;
15875# endif
15876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 server_name = get_tv_string_chk(&argvars[0]);
15878 if (server_name == NULL)
15879 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015880 keys = get_tv_string_buf(&argvars[1], buf);
15881# ifdef WIN32
15882 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15883# else
15884 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15885 < 0)
15886# endif
15887 {
15888 if (r != NULL)
15889 EMSG(r); /* sending worked but evaluation failed */
15890 else
15891 EMSG2(_("E241: Unable to send to %s"), server_name);
15892 return;
15893 }
15894
15895 rettv->vval.v_string = r;
15896
15897 if (argvars[2].v_type != VAR_UNKNOWN)
15898 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015900 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015903 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 v.di_tv.v_type = VAR_STRING;
15905 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 idvar = get_tv_string_chk(&argvars[2]);
15907 if (idvar != NULL)
15908 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015910 }
15911}
15912#endif
15913
15914/*
15915 * "remote_expr()" function
15916 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917 static void
15918f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015920 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015921{
15922 rettv->v_type = VAR_STRING;
15923 rettv->vval.v_string = NULL;
15924#ifdef FEAT_CLIENTSERVER
15925 remote_common(argvars, rettv, TRUE);
15926#endif
15927}
15928
15929/*
15930 * "remote_foreground()" function
15931 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 static void
15933f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015934 typval_T *argvars UNUSED;
15935 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937#ifdef FEAT_CLIENTSERVER
15938# ifdef WIN32
15939 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015940 {
15941 char_u *server_name = get_tv_string_chk(&argvars[0]);
15942
15943 if (server_name != NULL)
15944 serverForeground(server_name);
15945 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946# else
15947 /* Send a foreground() expression to the server. */
15948 argvars[1].v_type = VAR_STRING;
15949 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15950 argvars[2].v_type = VAR_UNKNOWN;
15951 remote_common(argvars, rettv, TRUE);
15952 vim_free(argvars[1].vval.v_string);
15953# endif
15954#endif
15955}
15956
Bram Moolenaar0d660222005-01-07 21:51:51 +000015957 static void
15958f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015959 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961{
15962#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015963 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964 char_u *s = NULL;
15965# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015966 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015967# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015969
15970 if (check_restricted() || check_secure())
15971 {
15972 rettv->vval.v_number = -1;
15973 return;
15974 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 serverid = get_tv_string_chk(&argvars[0]);
15976 if (serverid == NULL)
15977 {
15978 rettv->vval.v_number = -1;
15979 return; /* type error; errmsg already given */
15980 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015982 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015983 if (n == 0)
15984 rettv->vval.v_number = -1;
15985 else
15986 {
15987 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15988 rettv->vval.v_number = (s != NULL);
15989 }
15990# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015991 if (check_connection() == FAIL)
15992 return;
15993
15994 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015995 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996# endif
15997
15998 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 char_u *retvar;
16001
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 v.di_tv.v_type = VAR_STRING;
16003 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016004 retvar = get_tv_string_chk(&argvars[1]);
16005 if (retvar != NULL)
16006 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016008 }
16009#else
16010 rettv->vval.v_number = -1;
16011#endif
16012}
16013
Bram Moolenaar0d660222005-01-07 21:51:51 +000016014 static void
16015f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016018{
16019 char_u *r = NULL;
16020
16021#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 char_u *serverid = get_tv_string_chk(&argvars[0]);
16023
16024 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025 {
16026# ifdef WIN32
16027 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016028 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016030 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031 if (n != 0)
16032 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16033 if (r == NULL)
16034# else
16035 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016036 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016037# endif
16038 EMSG(_("E277: Unable to read a server reply"));
16039 }
16040#endif
16041 rettv->v_type = VAR_STRING;
16042 rettv->vval.v_string = r;
16043}
16044
16045/*
16046 * "remote_send()" function
16047 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016048 static void
16049f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016050 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016052{
16053 rettv->v_type = VAR_STRING;
16054 rettv->vval.v_string = NULL;
16055#ifdef FEAT_CLIENTSERVER
16056 remote_common(argvars, rettv, FALSE);
16057#endif
16058}
16059
16060/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016061 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062 */
16063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016064f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016065 typval_T *argvars;
16066 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016067{
Bram Moolenaar33570922005-01-25 22:26:29 +000016068 list_T *l;
16069 listitem_T *item, *item2;
16070 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016071 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016072 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016074 dict_T *d;
16075 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016076 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016077
Bram Moolenaar8c711452005-01-14 21:53:12 +000016078 if (argvars[0].v_type == VAR_DICT)
16079 {
16080 if (argvars[2].v_type != VAR_UNKNOWN)
16081 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016082 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016083 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 key = get_tv_string_chk(&argvars[1]);
16086 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 di = dict_find(d, key, -1);
16089 if (di == NULL)
16090 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016091 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16092 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 {
16094 *rettv = di->di_tv;
16095 init_tv(&di->di_tv);
16096 dictitem_remove(d, di);
16097 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016098 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016099 }
16100 }
16101 else if (argvars[0].v_type != VAR_LIST)
16102 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016103 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016104 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016106 int error = FALSE;
16107
16108 idx = get_tv_number_chk(&argvars[1], &error);
16109 if (error)
16110 ; /* type error: do nothing, errmsg already given */
16111 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016112 EMSGN(_(e_listidx), idx);
16113 else
16114 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016115 if (argvars[2].v_type == VAR_UNKNOWN)
16116 {
16117 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016118 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016119 *rettv = item->li_tv;
16120 vim_free(item);
16121 }
16122 else
16123 {
16124 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016125 end = get_tv_number_chk(&argvars[2], &error);
16126 if (error)
16127 ; /* type error: do nothing */
16128 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016129 EMSGN(_(e_listidx), end);
16130 else
16131 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016132 int cnt = 0;
16133
16134 for (li = item; li != NULL; li = li->li_next)
16135 {
16136 ++cnt;
16137 if (li == item2)
16138 break;
16139 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016140 if (li == NULL) /* didn't find "item2" after "item" */
16141 EMSG(_(e_invrange));
16142 else
16143 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016144 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016145 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016146 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016147 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016148 l->lv_first = item;
16149 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016150 item->li_prev = NULL;
16151 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016152 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016153 }
16154 }
16155 }
16156 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016157 }
16158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159}
16160
16161/*
16162 * "rename({from}, {to})" function
16163 */
16164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 typval_T *argvars;
16167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168{
16169 char_u buf[NUMBUFLEN];
16170
16171 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016174 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16175 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176}
16177
16178/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016179 * "repeat()" function
16180 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016181 static void
16182f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *argvars;
16184 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016185{
16186 char_u *p;
16187 int n;
16188 int slen;
16189 int len;
16190 char_u *r;
16191 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016192
16193 n = get_tv_number(&argvars[1]);
16194 if (argvars[0].v_type == VAR_LIST)
16195 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016197 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016198 if (list_extend(rettv->vval.v_list,
16199 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016200 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016201 }
16202 else
16203 {
16204 p = get_tv_string(&argvars[0]);
16205 rettv->v_type = VAR_STRING;
16206 rettv->vval.v_string = NULL;
16207
16208 slen = (int)STRLEN(p);
16209 len = slen * n;
16210 if (len <= 0)
16211 return;
16212
16213 r = alloc(len + 1);
16214 if (r != NULL)
16215 {
16216 for (i = 0; i < n; i++)
16217 mch_memmove(r + i * slen, p, (size_t)slen);
16218 r[len] = NUL;
16219 }
16220
16221 rettv->vval.v_string = r;
16222 }
16223}
16224
16225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 * "resolve()" function
16227 */
16228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 typval_T *argvars;
16231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232{
16233 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016234#ifdef HAVE_READLINK
16235 char_u *buf = NULL;
16236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239#ifdef FEAT_SHORTCUT
16240 {
16241 char_u *v = NULL;
16242
16243 v = mch_resolve_shortcut(p);
16244 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016247 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 }
16249#else
16250# ifdef HAVE_READLINK
16251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 char_u *cpy;
16253 int len;
16254 char_u *remain = NULL;
16255 char_u *q;
16256 int is_relative_to_current = FALSE;
16257 int has_trailing_pathsep = FALSE;
16258 int limit = 100;
16259
16260 p = vim_strsave(p);
16261
16262 if (p[0] == '.' && (vim_ispathsep(p[1])
16263 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16264 is_relative_to_current = TRUE;
16265
16266 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016267 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016270 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272
16273 q = getnextcomp(p);
16274 if (*q != NUL)
16275 {
16276 /* Separate the first path component in "p", and keep the
16277 * remainder (beginning with the path separator). */
16278 remain = vim_strsave(q - 1);
16279 q[-1] = NUL;
16280 }
16281
Bram Moolenaard9462e32011-04-11 21:35:11 +020016282 buf = alloc(MAXPATHL + 1);
16283 if (buf == NULL)
16284 goto fail;
16285
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 for (;;)
16287 {
16288 for (;;)
16289 {
16290 len = readlink((char *)p, (char *)buf, MAXPATHL);
16291 if (len <= 0)
16292 break;
16293 buf[len] = NUL;
16294
16295 if (limit-- == 0)
16296 {
16297 vim_free(p);
16298 vim_free(remain);
16299 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 goto fail;
16302 }
16303
16304 /* Ensure that the result will have a trailing path separator
16305 * if the argument has one. */
16306 if (remain == NULL && has_trailing_pathsep)
16307 add_pathsep(buf);
16308
16309 /* Separate the first path component in the link value and
16310 * concatenate the remainders. */
16311 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16312 if (*q != NUL)
16313 {
16314 if (remain == NULL)
16315 remain = vim_strsave(q - 1);
16316 else
16317 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016318 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 if (cpy != NULL)
16320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 vim_free(remain);
16322 remain = cpy;
16323 }
16324 }
16325 q[-1] = NUL;
16326 }
16327
16328 q = gettail(p);
16329 if (q > p && *q == NUL)
16330 {
16331 /* Ignore trailing path separator. */
16332 q[-1] = NUL;
16333 q = gettail(p);
16334 }
16335 if (q > p && !mch_isFullName(buf))
16336 {
16337 /* symlink is relative to directory of argument */
16338 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16339 if (cpy != NULL)
16340 {
16341 STRCPY(cpy, p);
16342 STRCPY(gettail(cpy), buf);
16343 vim_free(p);
16344 p = cpy;
16345 }
16346 }
16347 else
16348 {
16349 vim_free(p);
16350 p = vim_strsave(buf);
16351 }
16352 }
16353
16354 if (remain == NULL)
16355 break;
16356
16357 /* Append the first path component of "remain" to "p". */
16358 q = getnextcomp(remain + 1);
16359 len = q - remain - (*q != NUL);
16360 cpy = vim_strnsave(p, STRLEN(p) + len);
16361 if (cpy != NULL)
16362 {
16363 STRNCAT(cpy, remain, len);
16364 vim_free(p);
16365 p = cpy;
16366 }
16367 /* Shorten "remain". */
16368 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016369 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 else
16371 {
16372 vim_free(remain);
16373 remain = NULL;
16374 }
16375 }
16376
16377 /* If the result is a relative path name, make it explicitly relative to
16378 * the current directory if and only if the argument had this form. */
16379 if (!vim_ispathsep(*p))
16380 {
16381 if (is_relative_to_current
16382 && *p != NUL
16383 && !(p[0] == '.'
16384 && (p[1] == NUL
16385 || vim_ispathsep(p[1])
16386 || (p[1] == '.'
16387 && (p[2] == NUL
16388 || vim_ispathsep(p[2]))))))
16389 {
16390 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016391 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 if (cpy != NULL)
16393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 vim_free(p);
16395 p = cpy;
16396 }
16397 }
16398 else if (!is_relative_to_current)
16399 {
16400 /* Strip leading "./". */
16401 q = p;
16402 while (q[0] == '.' && vim_ispathsep(q[1]))
16403 q += 2;
16404 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016405 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 }
16407 }
16408
16409 /* Ensure that the result will have no trailing path separator
16410 * if the argument had none. But keep "/" or "//". */
16411 if (!has_trailing_pathsep)
16412 {
16413 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016414 if (after_pathsep(p, q))
16415 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 }
16417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016418 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 }
16420# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422# endif
16423#endif
16424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426
16427#ifdef HAVE_READLINK
16428fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016429 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432}
16433
16434/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 */
16437 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 typval_T *argvars;
16440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441{
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 list_T *l;
16443 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 if (argvars[0].v_type != VAR_LIST)
16446 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016447 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016448 && !tv_check_lock(l->lv_lock,
16449 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 {
16451 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016452 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016453 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454 while (li != NULL)
16455 {
16456 ni = li->li_prev;
16457 list_append(l, li);
16458 li = ni;
16459 }
16460 rettv->vval.v_list = l;
16461 rettv->v_type = VAR_LIST;
16462 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016463 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465}
16466
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016467#define SP_NOMOVE 0x01 /* don't move cursor */
16468#define SP_REPEAT 0x02 /* repeat to find outer pair */
16469#define SP_RETCOUNT 0x04 /* return matchcount */
16470#define SP_SETPCMARK 0x08 /* set previous context mark */
16471#define SP_START 0x10 /* accept match at start position */
16472#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16473#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016474#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016475
Bram Moolenaar33570922005-01-25 22:26:29 +000016476static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477
16478/*
16479 * Get flags for a search function.
16480 * Possibly sets "p_ws".
16481 * Returns BACKWARD, FORWARD or zero (for an error).
16482 */
16483 static int
16484get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 int *flagsp;
16487{
16488 int dir = FORWARD;
16489 char_u *flags;
16490 char_u nbuf[NUMBUFLEN];
16491 int mask;
16492
16493 if (varp->v_type != VAR_UNKNOWN)
16494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 flags = get_tv_string_buf_chk(varp, nbuf);
16496 if (flags == NULL)
16497 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 while (*flags != NUL)
16499 {
16500 switch (*flags)
16501 {
16502 case 'b': dir = BACKWARD; break;
16503 case 'w': p_ws = TRUE; break;
16504 case 'W': p_ws = FALSE; break;
16505 default: mask = 0;
16506 if (flagsp != NULL)
16507 switch (*flags)
16508 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016509 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016510 case 'e': mask = SP_END; break;
16511 case 'm': mask = SP_RETCOUNT; break;
16512 case 'n': mask = SP_NOMOVE; break;
16513 case 'p': mask = SP_SUBPAT; break;
16514 case 'r': mask = SP_REPEAT; break;
16515 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016516 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 }
16518 if (mask == 0)
16519 {
16520 EMSG2(_(e_invarg2), flags);
16521 dir = 0;
16522 }
16523 else
16524 *flagsp |= mask;
16525 }
16526 if (dir == 0)
16527 break;
16528 ++flags;
16529 }
16530 }
16531 return dir;
16532}
16533
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016535 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016537 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016538search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016539 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016540 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016541 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016543 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 char_u *pat;
16545 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016546 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 int save_p_ws = p_ws;
16548 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016550 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016551 proftime_T tm;
16552#ifdef FEAT_RELTIME
16553 long time_limit = 0;
16554#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016555 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016556 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016558 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016559 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016560 if (dir == 0)
16561 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016562 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016563 if (flags & SP_START)
16564 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016565 if (flags & SP_END)
16566 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016567 if (flags & SP_COLUMN)
16568 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016569
Bram Moolenaar76929292008-01-06 19:07:36 +000016570 /* Optional arguments: line number to stop searching and timeout. */
16571 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016572 {
16573 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16574 if (lnum_stop < 0)
16575 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016576#ifdef FEAT_RELTIME
16577 if (argvars[3].v_type != VAR_UNKNOWN)
16578 {
16579 time_limit = get_tv_number_chk(&argvars[3], NULL);
16580 if (time_limit < 0)
16581 goto theend;
16582 }
16583#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016584 }
16585
Bram Moolenaar76929292008-01-06 19:07:36 +000016586#ifdef FEAT_RELTIME
16587 /* Set the time limit, if there is one. */
16588 profile_setlimit(time_limit, &tm);
16589#endif
16590
Bram Moolenaar231334e2005-07-25 20:46:57 +000016591 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016592 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016593 * Check to make sure only those flags are set.
16594 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16595 * flags cannot be set. Check for that condition also.
16596 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016597 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016598 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016600 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016601 goto theend;
16602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016604 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016605 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016606 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016607 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016609 if (flags & SP_SUBPAT)
16610 retval = subpatnum;
16611 else
16612 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016613 if (flags & SP_SETPCMARK)
16614 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016616 if (match_pos != NULL)
16617 {
16618 /* Store the match cursor position */
16619 match_pos->lnum = pos.lnum;
16620 match_pos->col = pos.col + 1;
16621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 /* "/$" will put the cursor after the end of the line, may need to
16623 * correct that here */
16624 check_cursor();
16625 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016626
16627 /* If 'n' flag is used: restore cursor position. */
16628 if (flags & SP_NOMOVE)
16629 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016630 else
16631 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016632theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016634
16635 return retval;
16636}
16637
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016638#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016639
16640/*
16641 * round() is not in C90, use ceil() or floor() instead.
16642 */
16643 float_T
16644vim_round(f)
16645 float_T f;
16646{
16647 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16648}
16649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016650/*
16651 * "round({float})" function
16652 */
16653 static void
16654f_round(argvars, rettv)
16655 typval_T *argvars;
16656 typval_T *rettv;
16657{
16658 float_T f;
16659
16660 rettv->v_type = VAR_FLOAT;
16661 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016662 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016663 else
16664 rettv->vval.v_float = 0.0;
16665}
16666#endif
16667
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016668/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016669 * "screenattr()" function
16670 */
16671 static void
16672f_screenattr(argvars, rettv)
16673 typval_T *argvars UNUSED;
16674 typval_T *rettv;
16675{
16676 int row;
16677 int col;
16678 int c;
16679
16680 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16681 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16682 if (row < 0 || row >= screen_Rows
16683 || col < 0 || col >= screen_Columns)
16684 c = -1;
16685 else
16686 c = ScreenAttrs[LineOffset[row] + col];
16687 rettv->vval.v_number = c;
16688}
16689
16690/*
16691 * "screenchar()" function
16692 */
16693 static void
16694f_screenchar(argvars, rettv)
16695 typval_T *argvars UNUSED;
16696 typval_T *rettv;
16697{
16698 int row;
16699 int col;
16700 int off;
16701 int c;
16702
16703 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16704 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16705 if (row < 0 || row >= screen_Rows
16706 || col < 0 || col >= screen_Columns)
16707 c = -1;
16708 else
16709 {
16710 off = LineOffset[row] + col;
16711#ifdef FEAT_MBYTE
16712 if (enc_utf8 && ScreenLinesUC[off] != 0)
16713 c = ScreenLinesUC[off];
16714 else
16715#endif
16716 c = ScreenLines[off];
16717 }
16718 rettv->vval.v_number = c;
16719}
16720
16721/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016722 * "screencol()" function
16723 *
16724 * First column is 1 to be consistent with virtcol().
16725 */
16726 static void
16727f_screencol(argvars, rettv)
16728 typval_T *argvars UNUSED;
16729 typval_T *rettv;
16730{
16731 rettv->vval.v_number = screen_screencol() + 1;
16732}
16733
16734/*
16735 * "screenrow()" function
16736 */
16737 static void
16738f_screenrow(argvars, rettv)
16739 typval_T *argvars UNUSED;
16740 typval_T *rettv;
16741{
16742 rettv->vval.v_number = screen_screenrow() + 1;
16743}
16744
16745/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016746 * "search()" function
16747 */
16748 static void
16749f_search(argvars, rettv)
16750 typval_T *argvars;
16751 typval_T *rettv;
16752{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016753 int flags = 0;
16754
16755 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016759 * "searchdecl()" function
16760 */
16761 static void
16762f_searchdecl(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
16766 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016767 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016768 int error = FALSE;
16769 char_u *name;
16770
16771 rettv->vval.v_number = 1; /* default: FAIL */
16772
16773 name = get_tv_string_chk(&argvars[0]);
16774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016775 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016776 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016777 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16778 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16779 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016780 if (!error && name != NULL)
16781 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016782 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016783}
16784
16785/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016786 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016788 static int
16789searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016790 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016791 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792{
16793 char_u *spat, *mpat, *epat;
16794 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 int dir;
16797 int flags = 0;
16798 char_u nbuf1[NUMBUFLEN];
16799 char_u nbuf2[NUMBUFLEN];
16800 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016801 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016802 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016803 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016806 spat = get_tv_string_chk(&argvars[0]);
16807 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16808 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16809 if (spat == NULL || mpat == NULL || epat == NULL)
16810 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 /* Handle the optional fourth argument: flags */
16813 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016814 if (dir == 0)
16815 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016816
16817 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016818 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16819 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016820 if ((flags & (SP_END | SP_SUBPAT)) != 0
16821 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016822 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016823 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016824 goto theend;
16825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016827 /* Using 'r' implies 'W', otherwise it doesn't work. */
16828 if (flags & SP_REPEAT)
16829 p_ws = FALSE;
16830
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016831 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016832 if (argvars[3].v_type == VAR_UNKNOWN
16833 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 skip = (char_u *)"";
16835 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016838 if (argvars[5].v_type != VAR_UNKNOWN)
16839 {
16840 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16841 if (lnum_stop < 0)
16842 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016843#ifdef FEAT_RELTIME
16844 if (argvars[6].v_type != VAR_UNKNOWN)
16845 {
16846 time_limit = get_tv_number_chk(&argvars[6], NULL);
16847 if (time_limit < 0)
16848 goto theend;
16849 }
16850#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 }
16852 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016853 if (skip == NULL)
16854 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016856 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016857 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016858
16859theend:
16860 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016861
16862 return retval;
16863}
16864
16865/*
16866 * "searchpair()" function
16867 */
16868 static void
16869f_searchpair(argvars, rettv)
16870 typval_T *argvars;
16871 typval_T *rettv;
16872{
16873 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16874}
16875
16876/*
16877 * "searchpairpos()" function
16878 */
16879 static void
16880f_searchpairpos(argvars, rettv)
16881 typval_T *argvars;
16882 typval_T *rettv;
16883{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016884 pos_T match_pos;
16885 int lnum = 0;
16886 int col = 0;
16887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016889 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016890
16891 if (searchpair_cmn(argvars, &match_pos) > 0)
16892 {
16893 lnum = match_pos.lnum;
16894 col = match_pos.col;
16895 }
16896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016897 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16898 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016899}
16900
16901/*
16902 * Search for a start/middle/end thing.
16903 * Used by searchpair(), see its documentation for the details.
16904 * Returns 0 or -1 for no match,
16905 */
16906 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016907do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16908 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016909 char_u *spat; /* start pattern */
16910 char_u *mpat; /* middle pattern */
16911 char_u *epat; /* end pattern */
16912 int dir; /* BACKWARD or FORWARD */
16913 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016914 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016915 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016916 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016917 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016918{
16919 char_u *save_cpo;
16920 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16921 long retval = 0;
16922 pos_T pos;
16923 pos_T firstpos;
16924 pos_T foundpos;
16925 pos_T save_cursor;
16926 pos_T save_pos;
16927 int n;
16928 int r;
16929 int nest = 1;
16930 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016931 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016932 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016933
16934 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16935 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016936 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016937
Bram Moolenaar76929292008-01-06 19:07:36 +000016938#ifdef FEAT_RELTIME
16939 /* Set the time limit, if there is one. */
16940 profile_setlimit(time_limit, &tm);
16941#endif
16942
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016943 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16944 * start/middle/end (pat3, for the top pair). */
16945 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16946 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16947 if (pat2 == NULL || pat3 == NULL)
16948 goto theend;
16949 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16950 if (*mpat == NUL)
16951 STRCPY(pat3, pat2);
16952 else
16953 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16954 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016955 if (flags & SP_START)
16956 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016957
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 save_cursor = curwin->w_cursor;
16959 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016960 clearpos(&firstpos);
16961 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 pat = pat3;
16963 for (;;)
16964 {
16965 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016966 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16968 /* didn't find it or found the first match again: FAIL */
16969 break;
16970
16971 if (firstpos.lnum == 0)
16972 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016973 if (equalpos(pos, foundpos))
16974 {
16975 /* Found the same position again. Can happen with a pattern that
16976 * has "\zs" at the end and searching backwards. Advance one
16977 * character and try again. */
16978 if (dir == BACKWARD)
16979 decl(&pos);
16980 else
16981 incl(&pos);
16982 }
16983 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016985 /* clear the start flag to avoid getting stuck here */
16986 options &= ~SEARCH_START;
16987
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 /* If the skip pattern matches, ignore this match. */
16989 if (*skip != NUL)
16990 {
16991 save_pos = curwin->w_cursor;
16992 curwin->w_cursor = pos;
16993 r = eval_to_bool(skip, &err, NULL, FALSE);
16994 curwin->w_cursor = save_pos;
16995 if (err)
16996 {
16997 /* Evaluating {skip} caused an error, break here. */
16998 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016999 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 break;
17001 }
17002 if (r)
17003 continue;
17004 }
17005
17006 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17007 {
17008 /* Found end when searching backwards or start when searching
17009 * forward: nested pair. */
17010 ++nest;
17011 pat = pat2; /* nested, don't search for middle */
17012 }
17013 else
17014 {
17015 /* Found end when searching forward or start when searching
17016 * backward: end of (nested) pair; or found middle in outer pair. */
17017 if (--nest == 1)
17018 pat = pat3; /* outer level, search for middle */
17019 }
17020
17021 if (nest == 0)
17022 {
17023 /* Found the match: return matchcount or line number. */
17024 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017025 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017027 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017028 if (flags & SP_SETPCMARK)
17029 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 curwin->w_cursor = pos;
17031 if (!(flags & SP_REPEAT))
17032 break;
17033 nest = 1; /* search for next unmatched */
17034 }
17035 }
17036
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017037 if (match_pos != NULL)
17038 {
17039 /* Store the match cursor position */
17040 match_pos->lnum = curwin->w_cursor.lnum;
17041 match_pos->col = curwin->w_cursor.col + 1;
17042 }
17043
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017045 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 curwin->w_cursor = save_cursor;
17047
17048theend:
17049 vim_free(pat2);
17050 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017051 if (p_cpo == empty_option)
17052 p_cpo = save_cpo;
17053 else
17054 /* Darn, evaluating the {skip} expression changed the value. */
17055 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017056
17057 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017060/*
17061 * "searchpos()" function
17062 */
17063 static void
17064f_searchpos(argvars, rettv)
17065 typval_T *argvars;
17066 typval_T *rettv;
17067{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017068 pos_T match_pos;
17069 int lnum = 0;
17070 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017071 int n;
17072 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017075 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017076
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017077 n = search_cmn(argvars, &match_pos, &flags);
17078 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017079 {
17080 lnum = match_pos.lnum;
17081 col = match_pos.col;
17082 }
17083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017084 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17085 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017086 if (flags & SP_SUBPAT)
17087 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088}
17089
17090
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 static void
17092f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017093 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096#ifdef FEAT_CLIENTSERVER
17097 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017098 char_u *server = get_tv_string_chk(&argvars[0]);
17099 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017102 if (server == NULL || reply == NULL)
17103 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 if (check_restricted() || check_secure())
17105 return;
17106# ifdef FEAT_X11
17107 if (check_connection() == FAIL)
17108 return;
17109# endif
17110
17111 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 EMSG(_("E258: Unable to send to client"));
17114 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 rettv->vval.v_number = 0;
17117#else
17118 rettv->vval.v_number = -1;
17119#endif
17120}
17121
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 static void
17123f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017125 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126{
17127 char_u *r = NULL;
17128
17129#ifdef FEAT_CLIENTSERVER
17130# ifdef WIN32
17131 r = serverGetVimNames();
17132# else
17133 make_connection();
17134 if (X_DISPLAY != NULL)
17135 r = serverGetVimNames(X_DISPLAY);
17136# endif
17137#endif
17138 rettv->v_type = VAR_STRING;
17139 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140}
17141
17142/*
17143 * "setbufvar()" function
17144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017146f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017148 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149{
17150 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 char_u nbuf[NUMBUFLEN];
17155
17156 if (check_restricted() || check_secure())
17157 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017158 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17159 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017160 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 varp = &argvars[2];
17162
17163 if (buf != NULL && varname != NULL && varp != NULL)
17164 {
17165 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
17168 if (*varname == '&')
17169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 long numval;
17171 char_u *strval;
17172 int error = FALSE;
17173
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 numval = get_tv_number_chk(varp, &error);
17176 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 if (!error && strval != NULL)
17178 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 }
17180 else
17181 {
17182 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17183 if (bufvarname != NULL)
17184 {
17185 STRCPY(bufvarname, "b:");
17186 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017187 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 vim_free(bufvarname);
17189 }
17190 }
17191
17192 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195}
17196
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017197 static void
17198f_setcharsearch(argvars, rettv)
17199 typval_T *argvars;
17200 typval_T *rettv UNUSED;
17201{
17202 dict_T *d;
17203 dictitem_T *di;
17204 char_u *csearch;
17205
17206 if (argvars[0].v_type != VAR_DICT)
17207 {
17208 EMSG(_(e_dictreq));
17209 return;
17210 }
17211
17212 if ((d = argvars[0].vval.v_dict) != NULL)
17213 {
17214 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17215 if (csearch != NULL)
17216 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017217#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017218 if (enc_utf8)
17219 {
17220 int pcc[MAX_MCO];
17221 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017222
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017223 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17224 }
17225 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017226#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017227 set_last_csearch(PTR2CHAR(csearch),
17228 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017229 }
17230
17231 di = dict_find(d, (char_u *)"forward", -1);
17232 if (di != NULL)
17233 set_csearch_direction(get_tv_number(&di->di_tv)
17234 ? FORWARD : BACKWARD);
17235
17236 di = dict_find(d, (char_u *)"until", -1);
17237 if (di != NULL)
17238 set_csearch_until(!!get_tv_number(&di->di_tv));
17239 }
17240}
17241
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242/*
17243 * "setcmdpos()" function
17244 */
17245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017246f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 typval_T *argvars;
17248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 int pos = (int)get_tv_number(&argvars[0]) - 1;
17251
17252 if (pos >= 0)
17253 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254}
17255
17256/*
17257 * "setline()" function
17258 */
17259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017260f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 typval_T *argvars;
17262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263{
17264 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017265 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017266 list_T *l = NULL;
17267 listitem_T *li = NULL;
17268 long added = 0;
17269 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017271 lnum = get_tv_lnum(&argvars[0]);
17272 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017274 l = argvars[1].vval.v_list;
17275 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017277 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017278 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017279
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017280 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017281 for (;;)
17282 {
17283 if (l != NULL)
17284 {
17285 /* list argument, get next string */
17286 if (li == NULL)
17287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017288 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017289 li = li->li_next;
17290 }
17291
17292 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017294 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017295
17296 /* When coming here from Insert mode, sync undo, so that this can be
17297 * undone separately from what was previously inserted. */
17298 if (u_sync_once == 2)
17299 {
17300 u_sync_once = 1; /* notify that u_sync() was called */
17301 u_sync(TRUE);
17302 }
17303
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017304 if (lnum <= curbuf->b_ml.ml_line_count)
17305 {
17306 /* existing line, replace it */
17307 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17308 {
17309 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017310 if (lnum == curwin->w_cursor.lnum)
17311 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017312 rettv->vval.v_number = 0; /* OK */
17313 }
17314 }
17315 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17316 {
17317 /* lnum is one past the last line, append the line */
17318 ++added;
17319 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17320 rettv->vval.v_number = 0; /* OK */
17321 }
17322
17323 if (l == NULL) /* only one string argument */
17324 break;
17325 ++lnum;
17326 }
17327
17328 if (added > 0)
17329 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330}
17331
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017332static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17333
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017335 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017336 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017337 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017338set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017339 win_T *wp UNUSED;
17340 typval_T *list_arg UNUSED;
17341 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017342 typval_T *rettv;
17343{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017344#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017345 char_u *act;
17346 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017347#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017348
Bram Moolenaar2641f772005-03-25 21:58:17 +000017349 rettv->vval.v_number = -1;
17350
17351#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017352 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017353 EMSG(_(e_listreq));
17354 else
17355 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017356 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017357
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017358 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017359 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017360 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017361 if (act == NULL)
17362 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017363 if (*act == 'a' || *act == 'r')
17364 action = *act;
17365 }
17366
Bram Moolenaar81484f42012-12-05 15:16:47 +010017367 if (l != NULL && set_errorlist(wp, l, action,
17368 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017369 rettv->vval.v_number = 0;
17370 }
17371#endif
17372}
17373
17374/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017375 * "setloclist()" function
17376 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017377 static void
17378f_setloclist(argvars, rettv)
17379 typval_T *argvars;
17380 typval_T *rettv;
17381{
17382 win_T *win;
17383
17384 rettv->vval.v_number = -1;
17385
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017386 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017387 if (win != NULL)
17388 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17389}
17390
17391/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017392 * "setmatches()" function
17393 */
17394 static void
17395f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017396 typval_T *argvars UNUSED;
17397 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017398{
17399#ifdef FEAT_SEARCH_EXTRA
17400 list_T *l;
17401 listitem_T *li;
17402 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017403 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017404
17405 rettv->vval.v_number = -1;
17406 if (argvars[0].v_type != VAR_LIST)
17407 {
17408 EMSG(_(e_listreq));
17409 return;
17410 }
17411 if ((l = argvars[0].vval.v_list) != NULL)
17412 {
17413
17414 /* To some extent make sure that we are dealing with a list from
17415 * "getmatches()". */
17416 li = l->lv_first;
17417 while (li != NULL)
17418 {
17419 if (li->li_tv.v_type != VAR_DICT
17420 || (d = li->li_tv.vval.v_dict) == NULL)
17421 {
17422 EMSG(_(e_invarg));
17423 return;
17424 }
17425 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017426 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17427 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017428 && dict_find(d, (char_u *)"priority", -1) != NULL
17429 && dict_find(d, (char_u *)"id", -1) != NULL))
17430 {
17431 EMSG(_(e_invarg));
17432 return;
17433 }
17434 li = li->li_next;
17435 }
17436
17437 clear_matches(curwin);
17438 li = l->lv_first;
17439 while (li != NULL)
17440 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017441 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017442 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017443 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017444 char_u *group;
17445 int priority;
17446 int id;
17447 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017448
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017449 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017450 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17451 {
17452 if (s == NULL)
17453 {
17454 s = list_alloc();
17455 if (s == NULL)
17456 return;
17457 }
17458
17459 /* match from matchaddpos() */
17460 for (i = 1; i < 9; i++)
17461 {
17462 sprintf((char *)buf, (char *)"pos%d", i);
17463 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17464 {
17465 if (di->di_tv.v_type != VAR_LIST)
17466 return;
17467
17468 list_append_tv(s, &di->di_tv);
17469 s->lv_refcount++;
17470 }
17471 else
17472 break;
17473 }
17474 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017475
17476 group = get_dict_string(d, (char_u *)"group", FALSE);
17477 priority = (int)get_dict_number(d, (char_u *)"priority");
17478 id = (int)get_dict_number(d, (char_u *)"id");
17479 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17480 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17481 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017482 if (i == 0)
17483 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017484 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017485 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017486 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017487 }
17488 else
17489 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017490 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017491 list_unref(s);
17492 s = NULL;
17493 }
17494
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017495 li = li->li_next;
17496 }
17497 rettv->vval.v_number = 0;
17498 }
17499#endif
17500}
17501
17502/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017503 * "setpos()" function
17504 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017505 static void
17506f_setpos(argvars, rettv)
17507 typval_T *argvars;
17508 typval_T *rettv;
17509{
17510 pos_T pos;
17511 int fnum;
17512 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017513 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017514
Bram Moolenaar08250432008-02-13 11:42:46 +000017515 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017516 name = get_tv_string_chk(argvars);
17517 if (name != NULL)
17518 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017519 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017520 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017521 if (--pos.col < 0)
17522 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017523 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017524 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017525 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017526 if (fnum == curbuf->b_fnum)
17527 {
17528 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017529 if (curswant >= 0)
17530 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017531 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017532 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017533 }
17534 else
17535 EMSG(_(e_invarg));
17536 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017537 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17538 {
17539 /* set mark */
17540 if (setmark_pos(name[1], &pos, fnum) == OK)
17541 rettv->vval.v_number = 0;
17542 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017543 else
17544 EMSG(_(e_invarg));
17545 }
17546 }
17547}
17548
17549/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017550 * "setqflist()" function
17551 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017552 static void
17553f_setqflist(argvars, rettv)
17554 typval_T *argvars;
17555 typval_T *rettv;
17556{
17557 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17558}
17559
17560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 * "setreg()" function
17562 */
17563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017564f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017565 typval_T *argvars;
17566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567{
17568 int regname;
17569 char_u *strregname;
17570 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017571 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 int append;
17573 char_u yank_type;
17574 long block_len;
17575
17576 block_len = -1;
17577 yank_type = MAUTO;
17578 append = FALSE;
17579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017580 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017581 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017583 if (strregname == NULL)
17584 return; /* type error; errmsg already given */
17585 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 if (regname == 0 || regname == '@')
17587 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017591 stropt = get_tv_string_chk(&argvars[2]);
17592 if (stropt == NULL)
17593 return; /* type error */
17594 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 switch (*stropt)
17596 {
17597 case 'a': case 'A': /* append */
17598 append = TRUE;
17599 break;
17600 case 'v': case 'c': /* character-wise selection */
17601 yank_type = MCHAR;
17602 break;
17603 case 'V': case 'l': /* line-wise selection */
17604 yank_type = MLINE;
17605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 case 'b': case Ctrl_V: /* block-wise selection */
17607 yank_type = MBLOCK;
17608 if (VIM_ISDIGIT(stropt[1]))
17609 {
17610 ++stropt;
17611 block_len = getdigits(&stropt) - 1;
17612 --stropt;
17613 }
17614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 }
17616 }
17617
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017618 if (argvars[1].v_type == VAR_LIST)
17619 {
17620 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017621 char_u **allocval;
17622 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017623 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017624 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017625 int len = argvars[1].vval.v_list->lv_len;
17626 listitem_T *li;
17627
Bram Moolenaar7d647822014-04-05 21:28:56 +020017628 /* First half: use for pointers to result lines; second half: use for
17629 * pointers to allocated copies. */
17630 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017631 if (lstval == NULL)
17632 return;
17633 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017634 allocval = lstval + len + 2;
17635 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017636
17637 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17638 li = li->li_next)
17639 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017640 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017641 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017642 goto free_lstval;
17643 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017644 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017645 /* Need to make a copy, next get_tv_string_buf_chk() will
17646 * overwrite the string. */
17647 strval = vim_strsave(buf);
17648 if (strval == NULL)
17649 goto free_lstval;
17650 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017651 }
17652 *curval++ = strval;
17653 }
17654 *curval++ = NULL;
17655
17656 write_reg_contents_lst(regname, lstval, -1,
17657 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017658free_lstval:
17659 while (curallocval > allocval)
17660 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017661 vim_free(lstval);
17662 }
17663 else
17664 {
17665 strval = get_tv_string_chk(&argvars[1]);
17666 if (strval == NULL)
17667 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017668 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672}
17673
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017674/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017675 * "settabvar()" function
17676 */
17677 static void
17678f_settabvar(argvars, rettv)
17679 typval_T *argvars;
17680 typval_T *rettv;
17681{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017682#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017683 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017684 tabpage_T *tp;
17685#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017686 char_u *varname, *tabvarname;
17687 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017688
17689 rettv->vval.v_number = 0;
17690
17691 if (check_restricted() || check_secure())
17692 return;
17693
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017694#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017695 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017696#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017697 varname = get_tv_string_chk(&argvars[1]);
17698 varp = &argvars[2];
17699
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017700 if (varname != NULL && varp != NULL
17701#ifdef FEAT_WINDOWS
17702 && tp != NULL
17703#endif
17704 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017705 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017706#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017707 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017708 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017709#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017710
17711 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17712 if (tabvarname != NULL)
17713 {
17714 STRCPY(tabvarname, "t:");
17715 STRCPY(tabvarname + 2, varname);
17716 set_var(tabvarname, varp, TRUE);
17717 vim_free(tabvarname);
17718 }
17719
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017720#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017721 /* Restore current tabpage */
17722 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017723 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017724#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017725 }
17726}
17727
17728/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017729 * "settabwinvar()" function
17730 */
17731 static void
17732f_settabwinvar(argvars, rettv)
17733 typval_T *argvars;
17734 typval_T *rettv;
17735{
17736 setwinvar(argvars, rettv, 1);
17737}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
17739/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017740 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 typval_T *argvars;
17745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017747 setwinvar(argvars, rettv, 0);
17748}
17749
17750/*
17751 * "setwinvar()" and "settabwinvar()" functions
17752 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017753
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017754 static void
17755setwinvar(argvars, rettv, off)
17756 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017757 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017758 int off;
17759{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 win_T *win;
17761#ifdef FEAT_WINDOWS
17762 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017763 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017764 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765#endif
17766 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017769 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770
17771 if (check_restricted() || check_secure())
17772 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017773
17774#ifdef FEAT_WINDOWS
17775 if (off == 1)
17776 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17777 else
17778 tp = curtab;
17779#endif
17780 win = find_win_by_nr(&argvars[off], tp);
17781 varname = get_tv_string_chk(&argvars[off + 1]);
17782 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783
17784 if (win != NULL && varname != NULL && varp != NULL)
17785 {
17786#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017787 need_switch_win = !(tp == curtab && win == curwin);
17788 if (!need_switch_win
17789 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017792 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017794 long numval;
17795 char_u *strval;
17796 int error = FALSE;
17797
17798 ++varname;
17799 numval = get_tv_number_chk(varp, &error);
17800 strval = get_tv_string_buf_chk(varp, nbuf);
17801 if (!error && strval != NULL)
17802 set_option_value(varname, numval, strval, OPT_LOCAL);
17803 }
17804 else
17805 {
17806 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17807 if (winvarname != NULL)
17808 {
17809 STRCPY(winvarname, "w:");
17810 STRCPY(winvarname + 2, varname);
17811 set_var(winvarname, varp, TRUE);
17812 vim_free(winvarname);
17813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 }
17815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017817 if (need_switch_win)
17818 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819#endif
17820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821}
17822
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017823#ifdef FEAT_CRYPT
17824/*
17825 * "sha256({string})" function
17826 */
17827 static void
17828f_sha256(argvars, rettv)
17829 typval_T *argvars;
17830 typval_T *rettv;
17831{
17832 char_u *p;
17833
17834 p = get_tv_string(&argvars[0]);
17835 rettv->vval.v_string = vim_strsave(
17836 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17837 rettv->v_type = VAR_STRING;
17838}
17839#endif /* FEAT_CRYPT */
17840
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017842 * "shellescape({string})" function
17843 */
17844 static void
17845f_shellescape(argvars, rettv)
17846 typval_T *argvars;
17847 typval_T *rettv;
17848{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017849 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017850 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017851 rettv->v_type = VAR_STRING;
17852}
17853
17854/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017855 * shiftwidth() function
17856 */
17857 static void
17858f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017859 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017860 typval_T *rettv;
17861{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017862 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017863}
17864
17865/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 */
17868 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017870 typval_T *argvars;
17871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017873 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874
Bram Moolenaar0d660222005-01-07 21:51:51 +000017875 p = get_tv_string(&argvars[0]);
17876 rettv->vval.v_string = vim_strsave(p);
17877 simplify_filename(rettv->vval.v_string); /* simplify in place */
17878 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879}
17880
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017881#ifdef FEAT_FLOAT
17882/*
17883 * "sin()" function
17884 */
17885 static void
17886f_sin(argvars, rettv)
17887 typval_T *argvars;
17888 typval_T *rettv;
17889{
17890 float_T f;
17891
17892 rettv->v_type = VAR_FLOAT;
17893 if (get_float_arg(argvars, &f) == OK)
17894 rettv->vval.v_float = sin(f);
17895 else
17896 rettv->vval.v_float = 0.0;
17897}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017898
17899/*
17900 * "sinh()" function
17901 */
17902 static void
17903f_sinh(argvars, rettv)
17904 typval_T *argvars;
17905 typval_T *rettv;
17906{
17907 float_T f;
17908
17909 rettv->v_type = VAR_FLOAT;
17910 if (get_float_arg(argvars, &f) == OK)
17911 rettv->vval.v_float = sinh(f);
17912 else
17913 rettv->vval.v_float = 0.0;
17914}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017915#endif
17916
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917static int
17918#ifdef __BORLANDC__
17919 _RTLENTRYF
17920#endif
17921 item_compare __ARGS((const void *s1, const void *s2));
17922static int
17923#ifdef __BORLANDC__
17924 _RTLENTRYF
17925#endif
17926 item_compare2 __ARGS((const void *s1, const void *s2));
17927
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017928/* struct used in the array that's given to qsort() */
17929typedef struct
17930{
17931 listitem_T *item;
17932 int idx;
17933} sortItem_T;
17934
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017936static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017937static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017939static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017940static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017941static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017942static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017943#define ITEM_COMPARE_FAIL 999
17944
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017946 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 static int
17949#ifdef __BORLANDC__
17950_RTLENTRYF
17951#endif
17952item_compare(s1, s2)
17953 const void *s1;
17954 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017956 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017957 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017958 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017959 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 int res;
17961 char_u numbuf1[NUMBUFLEN];
17962 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017964 si1 = (sortItem_T *)s1;
17965 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017966 tv1 = &si1->item->li_tv;
17967 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017968
17969 if (item_compare_numbers)
17970 {
17971 long v1 = get_tv_number(tv1);
17972 long v2 = get_tv_number(tv2);
17973
17974 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17975 }
17976
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017977 /* tv2string() puts quotes around a string and allocates memory. Don't do
17978 * that for string variables. Use a single quote when comparing with a
17979 * non-string to do what the docs promise. */
17980 if (tv1->v_type == VAR_STRING)
17981 {
17982 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17983 p1 = (char_u *)"'";
17984 else
17985 p1 = tv1->vval.v_string;
17986 }
17987 else
17988 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17989 if (tv2->v_type == VAR_STRING)
17990 {
17991 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17992 p2 = (char_u *)"'";
17993 else
17994 p2 = tv2->vval.v_string;
17995 }
17996 else
17997 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017998 if (p1 == NULL)
17999 p1 = (char_u *)"";
18000 if (p2 == NULL)
18001 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018002 if (!item_compare_numeric)
18003 {
18004 if (item_compare_ic)
18005 res = STRICMP(p1, p2);
18006 else
18007 res = STRCMP(p1, p2);
18008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018010 {
18011 double n1, n2;
18012 n1 = strtod((char *)p1, (char **)&p1);
18013 n2 = strtod((char *)p2, (char **)&p2);
18014 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18015 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018016
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018017 /* When the result would be zero, compare the item indexes. Makes the
18018 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018019 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018020 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018021
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 vim_free(tofree1);
18023 vim_free(tofree2);
18024 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025}
18026
18027 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018028#ifdef __BORLANDC__
18029_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031item_compare2(s1, s2)
18032 const void *s1;
18033 const void *s2;
18034{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018035 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018037 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018038 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018041 /* shortcut after failure in previous call; compare all items equal */
18042 if (item_compare_func_err)
18043 return 0;
18044
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018045 si1 = (sortItem_T *)s1;
18046 si2 = (sortItem_T *)s2;
18047
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018048 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018049 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018050 copy_tv(&si1->item->li_tv, &argv[0]);
18051 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052
18053 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018054 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018055 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18056 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018057 clear_tv(&argv[0]);
18058 clear_tv(&argv[1]);
18059
18060 if (res == FAIL)
18061 res = ITEM_COMPARE_FAIL;
18062 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018063 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18064 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018065 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018066 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018067
18068 /* When the result would be zero, compare the pointers themselves. Makes
18069 * the sort stable. */
18070 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018071 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018072
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073 return res;
18074}
18075
18076/*
18077 * "sort({list})" function
18078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018080do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018081 typval_T *argvars;
18082 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018083 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084{
Bram Moolenaar33570922005-01-25 22:26:29 +000018085 list_T *l;
18086 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018087 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 long len;
18089 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018092 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 else
18094 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018095 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018096 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018097 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18098 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 return;
18100 rettv->vval.v_list = l;
18101 rettv->v_type = VAR_LIST;
18102 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 len = list_len(l);
18105 if (len <= 1)
18106 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018109 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018110 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018111 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018112 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113 if (argvars[1].v_type != VAR_UNKNOWN)
18114 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018115 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018116 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018117 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018118 else
18119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018120 int error = FALSE;
18121
18122 i = get_tv_number_chk(&argvars[1], &error);
18123 if (error)
18124 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018125 if (i == 1)
18126 item_compare_ic = TRUE;
18127 else
18128 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018129 if (item_compare_func != NULL)
18130 {
18131 if (STRCMP(item_compare_func, "n") == 0)
18132 {
18133 item_compare_func = NULL;
18134 item_compare_numeric = TRUE;
18135 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018136 else if (STRCMP(item_compare_func, "N") == 0)
18137 {
18138 item_compare_func = NULL;
18139 item_compare_numbers = TRUE;
18140 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018141 else if (STRCMP(item_compare_func, "i") == 0)
18142 {
18143 item_compare_func = NULL;
18144 item_compare_ic = TRUE;
18145 }
18146 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018147 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018148
18149 if (argvars[2].v_type != VAR_UNKNOWN)
18150 {
18151 /* optional third argument: {dict} */
18152 if (argvars[2].v_type != VAR_DICT)
18153 {
18154 EMSG(_(e_dictreq));
18155 return;
18156 }
18157 item_compare_selfdict = argvars[2].vval.v_dict;
18158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018162 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163 if (ptrs == NULL)
18164 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
Bram Moolenaar327aa022014-03-25 18:24:23 +010018166 i = 0;
18167 if (sort)
18168 {
18169 /* sort(): ptrs will be the list to sort */
18170 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018171 {
18172 ptrs[i].item = li;
18173 ptrs[i].idx = i;
18174 ++i;
18175 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018176
18177 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018178 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018179 /* test the compare function */
18180 if (item_compare_func != NULL
18181 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018183 EMSG(_("E702: Sort compare function failed"));
18184 else
18185 {
18186 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018187 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018188 item_compare_func == NULL ? item_compare : item_compare2);
18189
18190 if (!item_compare_func_err)
18191 {
18192 /* Clear the List and append the items in sorted order. */
18193 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18194 l->lv_len = 0;
18195 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018196 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018197 }
18198 }
18199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018202 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18203
18204 /* f_uniq(): ptrs will be a stack of items to remove */
18205 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018206 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018207 item_compare_func_ptr = item_compare_func
18208 ? item_compare2 : item_compare;
18209
18210 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18211 li = li->li_next)
18212 {
18213 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18214 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018215 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018216 if (item_compare_func_err)
18217 {
18218 EMSG(_("E882: Uniq compare function failed"));
18219 break;
18220 }
18221 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 if (!item_compare_func_err)
18224 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018225 while (--i >= 0)
18226 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018227 li = ptrs[i].item->li_next;
18228 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018229 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018230 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018231 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018232 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018233 list_fix_watch(l, li);
18234 listitem_free(li);
18235 l->lv_len--;
18236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238 }
18239
18240 vim_free(ptrs);
18241 }
18242}
18243
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018244/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018245 * "sort({list})" function
18246 */
18247 static void
18248f_sort(argvars, rettv)
18249 typval_T *argvars;
18250 typval_T *rettv;
18251{
18252 do_sort_uniq(argvars, rettv, TRUE);
18253}
18254
18255/*
18256 * "uniq({list})" function
18257 */
18258 static void
18259f_uniq(argvars, rettv)
18260 typval_T *argvars;
18261 typval_T *rettv;
18262{
18263 do_sort_uniq(argvars, rettv, FALSE);
18264}
18265
18266/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018267 * "soundfold({word})" function
18268 */
18269 static void
18270f_soundfold(argvars, rettv)
18271 typval_T *argvars;
18272 typval_T *rettv;
18273{
18274 char_u *s;
18275
18276 rettv->v_type = VAR_STRING;
18277 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018278#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018279 rettv->vval.v_string = eval_soundfold(s);
18280#else
18281 rettv->vval.v_string = vim_strsave(s);
18282#endif
18283}
18284
18285/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018286 * "spellbadword()" function
18287 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018288 static void
18289f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018290 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018291 typval_T *rettv;
18292{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018293 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018294 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018295 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018297 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018298 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018299
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018300#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018301 if (argvars[0].v_type == VAR_UNKNOWN)
18302 {
18303 /* Find the start and length of the badly spelled word. */
18304 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18305 if (len != 0)
18306 word = ml_get_cursor();
18307 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018308 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018309 {
18310 char_u *str = get_tv_string_chk(&argvars[0]);
18311 int capcol = -1;
18312
18313 if (str != NULL)
18314 {
18315 /* Check the argument for spelling. */
18316 while (*str != NUL)
18317 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018318 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018319 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018320 {
18321 word = str;
18322 break;
18323 }
18324 str += len;
18325 }
18326 }
18327 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018328#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018330 list_append_string(rettv->vval.v_list, word, len);
18331 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018332 attr == HLF_SPB ? "bad" :
18333 attr == HLF_SPR ? "rare" :
18334 attr == HLF_SPL ? "local" :
18335 attr == HLF_SPC ? "caps" :
18336 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018337}
18338
18339/*
18340 * "spellsuggest()" function
18341 */
18342 static void
18343f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018344 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018345 typval_T *rettv;
18346{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018347#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018348 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018349 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018350 int maxcount;
18351 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018352 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018353 listitem_T *li;
18354 int need_capital = FALSE;
18355#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018357 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018358 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018359
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018360#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018361 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018362 {
18363 str = get_tv_string(&argvars[0]);
18364 if (argvars[1].v_type != VAR_UNKNOWN)
18365 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018366 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018367 if (maxcount <= 0)
18368 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018369 if (argvars[2].v_type != VAR_UNKNOWN)
18370 {
18371 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18372 if (typeerr)
18373 return;
18374 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018375 }
18376 else
18377 maxcount = 25;
18378
Bram Moolenaar4770d092006-01-12 23:22:24 +000018379 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018380
18381 for (i = 0; i < ga.ga_len; ++i)
18382 {
18383 str = ((char_u **)ga.ga_data)[i];
18384
18385 li = listitem_alloc();
18386 if (li == NULL)
18387 vim_free(str);
18388 else
18389 {
18390 li->li_tv.v_type = VAR_STRING;
18391 li->li_tv.v_lock = 0;
18392 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018393 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018394 }
18395 }
18396 ga_clear(&ga);
18397 }
18398#endif
18399}
18400
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018402f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018403 typval_T *argvars;
18404 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018405{
18406 char_u *str;
18407 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018408 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018409 regmatch_T regmatch;
18410 char_u patbuf[NUMBUFLEN];
18411 char_u *save_cpo;
18412 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018414 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018415 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416
18417 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18418 save_cpo = p_cpo;
18419 p_cpo = (char_u *)"";
18420
18421 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018422 if (argvars[1].v_type != VAR_UNKNOWN)
18423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018424 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18425 if (pat == NULL)
18426 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018428 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018429 }
18430 if (pat == NULL || *pat == NUL)
18431 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018435 if (typeerr)
18436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18439 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018442 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018443 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018444 if (*str == NUL)
18445 match = FALSE; /* empty item at the end */
18446 else
18447 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 if (match)
18449 end = regmatch.startp[0];
18450 else
18451 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018452 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18453 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018455 if (list_append_string(rettv->vval.v_list, str,
18456 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458 }
18459 if (!match)
18460 break;
18461 /* Advance to just after the match. */
18462 if (regmatch.endp[0] > str)
18463 col = 0;
18464 else
18465 {
18466 /* Don't get stuck at the same match. */
18467#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018468 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469#else
18470 col = 1;
18471#endif
18472 }
18473 str = regmatch.endp[0];
18474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475
Bram Moolenaar473de612013-06-08 18:19:48 +020018476 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480}
18481
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018482#ifdef FEAT_FLOAT
18483/*
18484 * "sqrt()" function
18485 */
18486 static void
18487f_sqrt(argvars, rettv)
18488 typval_T *argvars;
18489 typval_T *rettv;
18490{
18491 float_T f;
18492
18493 rettv->v_type = VAR_FLOAT;
18494 if (get_float_arg(argvars, &f) == OK)
18495 rettv->vval.v_float = sqrt(f);
18496 else
18497 rettv->vval.v_float = 0.0;
18498}
18499
18500/*
18501 * "str2float()" function
18502 */
18503 static void
18504f_str2float(argvars, rettv)
18505 typval_T *argvars;
18506 typval_T *rettv;
18507{
18508 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18509
18510 if (*p == '+')
18511 p = skipwhite(p + 1);
18512 (void)string2float(p, &rettv->vval.v_float);
18513 rettv->v_type = VAR_FLOAT;
18514}
18515#endif
18516
Bram Moolenaar2c932302006-03-18 21:42:09 +000018517/*
18518 * "str2nr()" function
18519 */
18520 static void
18521f_str2nr(argvars, rettv)
18522 typval_T *argvars;
18523 typval_T *rettv;
18524{
18525 int base = 10;
18526 char_u *p;
18527 long n;
18528
18529 if (argvars[1].v_type != VAR_UNKNOWN)
18530 {
18531 base = get_tv_number(&argvars[1]);
18532 if (base != 8 && base != 10 && base != 16)
18533 {
18534 EMSG(_(e_invarg));
18535 return;
18536 }
18537 }
18538
18539 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018540 if (*p == '+')
18541 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018542 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018543 rettv->vval.v_number = n;
18544}
18545
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546#ifdef HAVE_STRFTIME
18547/*
18548 * "strftime({format}[, {time}])" function
18549 */
18550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018552 typval_T *argvars;
18553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554{
18555 char_u result_buf[256];
18556 struct tm *curtime;
18557 time_t seconds;
18558 char_u *p;
18559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018563 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 seconds = time(NULL);
18565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 curtime = localtime(&seconds);
18568 /* MSVC returns NULL for an invalid value of seconds. */
18569 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 else
18572 {
18573# ifdef FEAT_MBYTE
18574 vimconv_T conv;
18575 char_u *enc;
18576
18577 conv.vc_type = CONV_NONE;
18578 enc = enc_locale();
18579 convert_setup(&conv, p_enc, enc);
18580 if (conv.vc_type != CONV_NONE)
18581 p = string_convert(&conv, p, NULL);
18582# endif
18583 if (p != NULL)
18584 (void)strftime((char *)result_buf, sizeof(result_buf),
18585 (char *)p, curtime);
18586 else
18587 result_buf[0] = NUL;
18588
18589# ifdef FEAT_MBYTE
18590 if (conv.vc_type != CONV_NONE)
18591 vim_free(p);
18592 convert_setup(&conv, enc, p_enc);
18593 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 else
18596# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598
18599# ifdef FEAT_MBYTE
18600 /* Release conversion descriptors */
18601 convert_setup(&conv, NULL, NULL);
18602 vim_free(enc);
18603# endif
18604 }
18605}
18606#endif
18607
18608/*
18609 * "stridx()" function
18610 */
18611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018612f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 typval_T *argvars;
18614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615{
18616 char_u buf[NUMBUFLEN];
18617 char_u *needle;
18618 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018621 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 needle = get_tv_string_chk(&argvars[1]);
18624 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018626 if (needle == NULL || haystack == NULL)
18627 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628
Bram Moolenaar33570922005-01-25 22:26:29 +000018629 if (argvars[2].v_type != VAR_UNKNOWN)
18630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 int error = FALSE;
18632
18633 start_idx = get_tv_number_chk(&argvars[2], &error);
18634 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018636 if (start_idx >= 0)
18637 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018638 }
18639
18640 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18641 if (pos != NULL)
18642 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643}
18644
18645/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018646 * "string()" function
18647 */
18648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018650 typval_T *argvars;
18651 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018652{
18653 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018654 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018657 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018658 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018659 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661}
18662
18663/*
18664 * "strlen()" function
18665 */
18666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *argvars;
18669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018671 rettv->vval.v_number = (varnumber_T)(STRLEN(
18672 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673}
18674
18675/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018676 * "strchars()" function
18677 */
18678 static void
18679f_strchars(argvars, rettv)
18680 typval_T *argvars;
18681 typval_T *rettv;
18682{
18683 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018684 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018685#ifdef FEAT_MBYTE
18686 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018687 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018688#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018689
18690 if (argvars[1].v_type != VAR_UNKNOWN)
18691 skipcc = get_tv_number_chk(&argvars[1], NULL);
18692 if (skipcc < 0 || skipcc > 1)
18693 EMSG(_(e_invarg));
18694 else
18695 {
18696#ifdef FEAT_MBYTE
18697 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18698 while (*s != NUL)
18699 {
18700 func_mb_ptr2char_adv(&s);
18701 ++len;
18702 }
18703 rettv->vval.v_number = len;
18704#else
18705 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18706#endif
18707 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018708}
18709
18710/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018711 * "strdisplaywidth()" function
18712 */
18713 static void
18714f_strdisplaywidth(argvars, rettv)
18715 typval_T *argvars;
18716 typval_T *rettv;
18717{
18718 char_u *s = get_tv_string(&argvars[0]);
18719 int col = 0;
18720
18721 if (argvars[1].v_type != VAR_UNKNOWN)
18722 col = get_tv_number(&argvars[1]);
18723
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018724 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018725}
18726
18727/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018728 * "strwidth()" function
18729 */
18730 static void
18731f_strwidth(argvars, rettv)
18732 typval_T *argvars;
18733 typval_T *rettv;
18734{
18735 char_u *s = get_tv_string(&argvars[0]);
18736
18737 rettv->vval.v_number = (varnumber_T)(
18738#ifdef FEAT_MBYTE
18739 mb_string2cells(s, -1)
18740#else
18741 STRLEN(s)
18742#endif
18743 );
18744}
18745
18746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 * "strpart()" function
18748 */
18749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 typval_T *argvars;
18752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753{
18754 char_u *p;
18755 int n;
18756 int len;
18757 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018758 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 slen = (int)STRLEN(p);
18762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 n = get_tv_number_chk(&argvars[1], &error);
18764 if (error)
18765 len = 0;
18766 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 else
18769 len = slen - n; /* default len: all bytes that are available. */
18770
18771 /*
18772 * Only return the overlap between the specified part and the actual
18773 * string.
18774 */
18775 if (n < 0)
18776 {
18777 len += n;
18778 n = 0;
18779 }
18780 else if (n > slen)
18781 n = slen;
18782 if (len < 0)
18783 len = 0;
18784 else if (n + len > slen)
18785 len = slen - n;
18786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787 rettv->v_type = VAR_STRING;
18788 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018792 * "strridx()" function
18793 */
18794 static void
18795f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018796 typval_T *argvars;
18797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018798{
18799 char_u buf[NUMBUFLEN];
18800 char_u *needle;
18801 char_u *haystack;
18802 char_u *rest;
18803 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018804 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018806 needle = get_tv_string_chk(&argvars[1]);
18807 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808
18809 rettv->vval.v_number = -1;
18810 if (needle == NULL || haystack == NULL)
18811 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018812
18813 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018814 if (argvars[2].v_type != VAR_UNKNOWN)
18815 {
18816 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018817 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018818 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018819 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018820 }
18821 else
18822 end_idx = haystack_len;
18823
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018825 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018826 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018827 lastmatch = haystack + end_idx;
18828 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018829 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018830 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018831 for (rest = haystack; *rest != '\0'; ++rest)
18832 {
18833 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018834 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018835 break;
18836 lastmatch = rest;
18837 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018838 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018839
18840 if (lastmatch == NULL)
18841 rettv->vval.v_number = -1;
18842 else
18843 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18844}
18845
18846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 * "strtrans()" function
18848 */
18849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018850f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 typval_T *argvars;
18852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018854 rettv->v_type = VAR_STRING;
18855 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856}
18857
18858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018859 * "submatch()" function
18860 */
18861 static void
18862f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018863 typval_T *argvars;
18864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865{
Bram Moolenaar41571762014-04-02 19:00:58 +020018866 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018867 int no;
18868 int retList = 0;
18869
18870 no = (int)get_tv_number_chk(&argvars[0], &error);
18871 if (error)
18872 return;
18873 error = FALSE;
18874 if (argvars[1].v_type != VAR_UNKNOWN)
18875 retList = get_tv_number_chk(&argvars[1], &error);
18876 if (error)
18877 return;
18878
18879 if (retList == 0)
18880 {
18881 rettv->v_type = VAR_STRING;
18882 rettv->vval.v_string = reg_submatch(no);
18883 }
18884 else
18885 {
18886 rettv->v_type = VAR_LIST;
18887 rettv->vval.v_list = reg_submatch_list(no);
18888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889}
18890
18891/*
18892 * "substitute()" function
18893 */
18894 static void
18895f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 typval_T *argvars;
18897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018898{
18899 char_u patbuf[NUMBUFLEN];
18900 char_u subbuf[NUMBUFLEN];
18901 char_u flagsbuf[NUMBUFLEN];
18902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018903 char_u *str = get_tv_string_chk(&argvars[0]);
18904 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18905 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18906 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18907
Bram Moolenaar0d660222005-01-07 21:51:51 +000018908 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018909 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18910 rettv->vval.v_string = NULL;
18911 else
18912 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018913}
18914
18915/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018916 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018920 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
18923 int id = 0;
18924#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018925 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 long col;
18927 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018928 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 lnum = get_tv_lnum(argvars); /* -1 on type error */
18931 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18932 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018934 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018935 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018936 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937#endif
18938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018939 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940}
18941
18942/*
18943 * "synIDattr(id, what [, mode])" function
18944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018946f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018947 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949{
18950 char_u *p = NULL;
18951#ifdef FEAT_SYN_HL
18952 int id;
18953 char_u *what;
18954 char_u *mode;
18955 char_u modebuf[NUMBUFLEN];
18956 int modec;
18957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018958 id = get_tv_number(&argvars[0]);
18959 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018964 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 modec = 0; /* replace invalid with current */
18966 }
18967 else
18968 {
18969#ifdef FEAT_GUI
18970 if (gui.in_use)
18971 modec = 'g';
18972 else
18973#endif
18974 if (t_colors > 1)
18975 modec = 'c';
18976 else
18977 modec = 't';
18978 }
18979
18980
18981 switch (TOLOWER_ASC(what[0]))
18982 {
18983 case 'b':
18984 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18985 p = highlight_color(id, what, modec);
18986 else /* bold */
18987 p = highlight_has_attr(id, HL_BOLD, modec);
18988 break;
18989
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018990 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 p = highlight_color(id, what, modec);
18992 break;
18993
18994 case 'i':
18995 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18996 p = highlight_has_attr(id, HL_INVERSE, modec);
18997 else /* italic */
18998 p = highlight_has_attr(id, HL_ITALIC, modec);
18999 break;
19000
19001 case 'n': /* name */
19002 p = get_highlight_name(NULL, id - 1);
19003 break;
19004
19005 case 'r': /* reverse */
19006 p = highlight_has_attr(id, HL_INVERSE, modec);
19007 break;
19008
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019009 case 's':
19010 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19011 p = highlight_color(id, what, modec);
19012 else /* standout */
19013 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 break;
19015
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019016 case 'u':
19017 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19018 /* underline */
19019 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19020 else
19021 /* undercurl */
19022 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 break;
19024 }
19025
19026 if (p != NULL)
19027 p = vim_strsave(p);
19028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 rettv->v_type = VAR_STRING;
19030 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031}
19032
19033/*
19034 * "synIDtrans(id)" function
19035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019037f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040{
19041 int id;
19042
19043#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
19046 if (id > 0)
19047 id = syn_get_final_id(id);
19048 else
19049#endif
19050 id = 0;
19051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019052 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053}
19054
19055/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019056 * "synconcealed(lnum, col)" function
19057 */
19058 static void
19059f_synconcealed(argvars, rettv)
19060 typval_T *argvars UNUSED;
19061 typval_T *rettv;
19062{
19063#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19064 long lnum;
19065 long col;
19066 int syntax_flags = 0;
19067 int cchar;
19068 int matchid = 0;
19069 char_u str[NUMBUFLEN];
19070#endif
19071
19072 rettv->v_type = VAR_LIST;
19073 rettv->vval.v_list = NULL;
19074
19075#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19076 lnum = get_tv_lnum(argvars); /* -1 on type error */
19077 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19078
19079 vim_memset(str, NUL, sizeof(str));
19080
19081 if (rettv_list_alloc(rettv) != FAIL)
19082 {
19083 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19084 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19085 && curwin->w_p_cole > 0)
19086 {
19087 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19088 syntax_flags = get_syntax_info(&matchid);
19089
19090 /* get the conceal character */
19091 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19092 {
19093 cchar = syn_get_sub_char();
19094 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19095 cchar = lcs_conceal;
19096 if (cchar != NUL)
19097 {
19098# ifdef FEAT_MBYTE
19099 if (has_mbyte)
19100 (*mb_char2bytes)(cchar, str);
19101 else
19102# endif
19103 str[0] = cchar;
19104 }
19105 }
19106 }
19107
19108 list_append_number(rettv->vval.v_list,
19109 (syntax_flags & HL_CONCEAL) != 0);
19110 /* -1 to auto-determine strlen */
19111 list_append_string(rettv->vval.v_list, str, -1);
19112 list_append_number(rettv->vval.v_list, matchid);
19113 }
19114#endif
19115}
19116
19117/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019118 * "synstack(lnum, col)" function
19119 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019120 static void
19121f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019122 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019123 typval_T *rettv;
19124{
19125#ifdef FEAT_SYN_HL
19126 long lnum;
19127 long col;
19128 int i;
19129 int id;
19130#endif
19131
19132 rettv->v_type = VAR_LIST;
19133 rettv->vval.v_list = NULL;
19134
19135#ifdef FEAT_SYN_HL
19136 lnum = get_tv_lnum(argvars); /* -1 on type error */
19137 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19138
19139 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019140 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019141 && rettv_list_alloc(rettv) != FAIL)
19142 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019143 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019144 for (i = 0; ; ++i)
19145 {
19146 id = syn_get_stack_item(i);
19147 if (id < 0)
19148 break;
19149 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19150 break;
19151 }
19152 }
19153#endif
19154}
19155
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019157get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 typval_T *argvars;
19159 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019160 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019162 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019164 char_u *infile = NULL;
19165 char_u buf[NUMBUFLEN];
19166 int err = FALSE;
19167 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019168 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019169 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019171 rettv->v_type = VAR_STRING;
19172 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019173 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019174 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019175
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019177 {
19178 /*
19179 * Write the string to a temp file, to be used for input of the shell
19180 * command.
19181 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019182 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019183 {
19184 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019185 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019186 }
19187
19188 fd = mch_fopen((char *)infile, WRITEBIN);
19189 if (fd == NULL)
19190 {
19191 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019192 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019193 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019194 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019195 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019196 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19197 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019198 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019199 else
19200 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019201 size_t len;
19202
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019203 p = get_tv_string_buf_chk(&argvars[1], buf);
19204 if (p == NULL)
19205 {
19206 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019207 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019208 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019209 len = STRLEN(p);
19210 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019211 err = TRUE;
19212 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019213 if (fclose(fd) != 0)
19214 err = TRUE;
19215 if (err)
19216 {
19217 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019218 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019219 }
19220 }
19221
Bram Moolenaar52a72462014-08-29 15:53:52 +020019222 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19223 * echoes typeahead, that messes up the display. */
19224 if (!msg_silent)
19225 flags += SHELL_COOKED;
19226
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019227 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019229 int len;
19230 listitem_T *li;
19231 char_u *s = NULL;
19232 char_u *start;
19233 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019234 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
Bram Moolenaar52a72462014-08-29 15:53:52 +020019236 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019237 if (res == NULL)
19238 goto errret;
19239
19240 list = list_alloc();
19241 if (list == NULL)
19242 goto errret;
19243
19244 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019246 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019247 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019248 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019249 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019250
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019251 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019252 if (s == NULL)
19253 goto errret;
19254
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019255 for (p = s; start < end; ++p, ++start)
19256 *p = *start == NUL ? NL : *start;
19257 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019258
19259 li = listitem_alloc();
19260 if (li == NULL)
19261 {
19262 vim_free(s);
19263 goto errret;
19264 }
19265 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019266 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019267 li->li_tv.vval.v_string = s;
19268 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019270
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019271 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019272 rettv->v_type = VAR_LIST;
19273 rettv->vval.v_list = list;
19274 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019276 else
19277 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019278 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019279#ifdef USE_CR
19280 /* translate <CR> into <NL> */
19281 if (res != NULL)
19282 {
19283 char_u *s;
19284
19285 for (s = res; *s; ++s)
19286 {
19287 if (*s == CAR)
19288 *s = NL;
19289 }
19290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291#else
19292# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293 /* translate <CR><NL> into <NL> */
19294 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019296 char_u *s, *d;
19297
19298 d = res;
19299 for (s = res; *s; ++s)
19300 {
19301 if (s[0] == CAR && s[1] == NL)
19302 ++s;
19303 *d++ = *s;
19304 }
19305 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307# endif
19308#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019309 rettv->vval.v_string = res;
19310 res = NULL;
19311 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019312
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019313errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019314 if (infile != NULL)
19315 {
19316 mch_remove(infile);
19317 vim_free(infile);
19318 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 if (res != NULL)
19320 vim_free(res);
19321 if (list != NULL)
19322 list_free(list, TRUE);
19323}
19324
19325/*
19326 * "system()" function
19327 */
19328 static void
19329f_system(argvars, rettv)
19330 typval_T *argvars;
19331 typval_T *rettv;
19332{
19333 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19334}
19335
19336/*
19337 * "systemlist()" function
19338 */
19339 static void
19340f_systemlist(argvars, rettv)
19341 typval_T *argvars;
19342 typval_T *rettv;
19343{
19344 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345}
19346
19347/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019348 * "tabpagebuflist()" function
19349 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019350 static void
19351f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019352 typval_T *argvars UNUSED;
19353 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019354{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019355#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019356 tabpage_T *tp;
19357 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019358
19359 if (argvars[0].v_type == VAR_UNKNOWN)
19360 wp = firstwin;
19361 else
19362 {
19363 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19364 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019365 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019366 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019367 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019368 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019369 for (; wp != NULL; wp = wp->w_next)
19370 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019371 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019372 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019373 }
19374#endif
19375}
19376
19377
19378/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019379 * "tabpagenr()" function
19380 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019381 static void
19382f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019383 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019384 typval_T *rettv;
19385{
19386 int nr = 1;
19387#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019388 char_u *arg;
19389
19390 if (argvars[0].v_type != VAR_UNKNOWN)
19391 {
19392 arg = get_tv_string_chk(&argvars[0]);
19393 nr = 0;
19394 if (arg != NULL)
19395 {
19396 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019397 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019398 else
19399 EMSG2(_(e_invexpr2), arg);
19400 }
19401 }
19402 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019403 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019404#endif
19405 rettv->vval.v_number = nr;
19406}
19407
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019408
19409#ifdef FEAT_WINDOWS
19410static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19411
19412/*
19413 * Common code for tabpagewinnr() and winnr().
19414 */
19415 static int
19416get_winnr(tp, argvar)
19417 tabpage_T *tp;
19418 typval_T *argvar;
19419{
19420 win_T *twin;
19421 int nr = 1;
19422 win_T *wp;
19423 char_u *arg;
19424
19425 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19426 if (argvar->v_type != VAR_UNKNOWN)
19427 {
19428 arg = get_tv_string_chk(argvar);
19429 if (arg == NULL)
19430 nr = 0; /* type error; errmsg already given */
19431 else if (STRCMP(arg, "$") == 0)
19432 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19433 else if (STRCMP(arg, "#") == 0)
19434 {
19435 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19436 if (twin == NULL)
19437 nr = 0;
19438 }
19439 else
19440 {
19441 EMSG2(_(e_invexpr2), arg);
19442 nr = 0;
19443 }
19444 }
19445
19446 if (nr > 0)
19447 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19448 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019449 {
19450 if (wp == NULL)
19451 {
19452 /* didn't find it in this tabpage */
19453 nr = 0;
19454 break;
19455 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019456 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019457 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019458 return nr;
19459}
19460#endif
19461
19462/*
19463 * "tabpagewinnr()" function
19464 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019465 static void
19466f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019467 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019468 typval_T *rettv;
19469{
19470 int nr = 1;
19471#ifdef FEAT_WINDOWS
19472 tabpage_T *tp;
19473
19474 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19475 if (tp == NULL)
19476 nr = 0;
19477 else
19478 nr = get_winnr(tp, &argvars[1]);
19479#endif
19480 rettv->vval.v_number = nr;
19481}
19482
19483
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019484/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019485 * "tagfiles()" function
19486 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019487 static void
19488f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019489 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019490 typval_T *rettv;
19491{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019492 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019493 tagname_T tn;
19494 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019495
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019496 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019497 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019498 fname = alloc(MAXPATHL);
19499 if (fname == NULL)
19500 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019502 for (first = TRUE; ; first = FALSE)
19503 if (get_tagfname(&tn, first, fname) == FAIL
19504 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019505 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019506 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019507 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019508}
19509
19510/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019511 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019512 */
19513 static void
19514f_taglist(argvars, rettv)
19515 typval_T *argvars;
19516 typval_T *rettv;
19517{
19518 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019519
19520 tag_pattern = get_tv_string(&argvars[0]);
19521
19522 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019523 if (*tag_pattern == NUL)
19524 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019525
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019526 if (rettv_list_alloc(rettv) == OK)
19527 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019528}
19529
19530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 * "tempname()" function
19532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019534f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019535 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537{
19538 static int x = 'A';
19539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019540 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019541 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542
19543 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19544 * names. Skip 'I' and 'O', they are used for shell redirection. */
19545 do
19546 {
19547 if (x == 'Z')
19548 x = '0';
19549 else if (x == '9')
19550 x = 'A';
19551 else
19552 {
19553#ifdef EBCDIC
19554 if (x == 'I')
19555 x = 'J';
19556 else if (x == 'R')
19557 x = 'S';
19558 else
19559#endif
19560 ++x;
19561 }
19562 } while (x == 'I' || x == 'O');
19563}
19564
19565/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019566 * "test(list)" function: Just checking the walls...
19567 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019568 static void
19569f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019570 typval_T *argvars UNUSED;
19571 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019572{
19573 /* Used for unit testing. Change the code below to your liking. */
19574#if 0
19575 listitem_T *li;
19576 list_T *l;
19577 char_u *bad, *good;
19578
19579 if (argvars[0].v_type != VAR_LIST)
19580 return;
19581 l = argvars[0].vval.v_list;
19582 if (l == NULL)
19583 return;
19584 li = l->lv_first;
19585 if (li == NULL)
19586 return;
19587 bad = get_tv_string(&li->li_tv);
19588 li = li->li_next;
19589 if (li == NULL)
19590 return;
19591 good = get_tv_string(&li->li_tv);
19592 rettv->vval.v_number = test_edit_score(bad, good);
19593#endif
19594}
19595
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019596#ifdef FEAT_FLOAT
19597/*
19598 * "tan()" function
19599 */
19600 static void
19601f_tan(argvars, rettv)
19602 typval_T *argvars;
19603 typval_T *rettv;
19604{
19605 float_T f;
19606
19607 rettv->v_type = VAR_FLOAT;
19608 if (get_float_arg(argvars, &f) == OK)
19609 rettv->vval.v_float = tan(f);
19610 else
19611 rettv->vval.v_float = 0.0;
19612}
19613
19614/*
19615 * "tanh()" function
19616 */
19617 static void
19618f_tanh(argvars, rettv)
19619 typval_T *argvars;
19620 typval_T *rettv;
19621{
19622 float_T f;
19623
19624 rettv->v_type = VAR_FLOAT;
19625 if (get_float_arg(argvars, &f) == OK)
19626 rettv->vval.v_float = tanh(f);
19627 else
19628 rettv->vval.v_float = 0.0;
19629}
19630#endif
19631
Bram Moolenaard52d9742005-08-21 22:20:28 +000019632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 * "tolower(string)" function
19634 */
19635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019636f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 typval_T *argvars;
19638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639{
19640 char_u *p;
19641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019642 p = vim_strsave(get_tv_string(&argvars[0]));
19643 rettv->v_type = VAR_STRING;
19644 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645
19646 if (p != NULL)
19647 while (*p != NUL)
19648 {
19649#ifdef FEAT_MBYTE
19650 int l;
19651
19652 if (enc_utf8)
19653 {
19654 int c, lc;
19655
19656 c = utf_ptr2char(p);
19657 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019658 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 /* TODO: reallocate string when byte count changes. */
19660 if (utf_char2len(lc) == l)
19661 utf_char2bytes(lc, p);
19662 p += l;
19663 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019664 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 p += l; /* skip multi-byte character */
19666 else
19667#endif
19668 {
19669 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19670 ++p;
19671 }
19672 }
19673}
19674
19675/*
19676 * "toupper(string)" function
19677 */
19678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019679f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 typval_T *argvars;
19681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019684 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685}
19686
19687/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019688 * "tr(string, fromstr, tostr)" function
19689 */
19690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 typval_T *argvars;
19693 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019694{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019695 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019696 char_u *fromstr;
19697 char_u *tostr;
19698 char_u *p;
19699#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019700 int inlen;
19701 int fromlen;
19702 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019703 int idx;
19704 char_u *cpstr;
19705 int cplen;
19706 int first = TRUE;
19707#endif
19708 char_u buf[NUMBUFLEN];
19709 char_u buf2[NUMBUFLEN];
19710 garray_T ga;
19711
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019712 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019713 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19714 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019715
19716 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->v_type = VAR_STRING;
19718 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019719 if (fromstr == NULL || tostr == NULL)
19720 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019721 ga_init2(&ga, (int)sizeof(char), 80);
19722
19723#ifdef FEAT_MBYTE
19724 if (!has_mbyte)
19725#endif
19726 /* not multi-byte: fromstr and tostr must be the same length */
19727 if (STRLEN(fromstr) != STRLEN(tostr))
19728 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019729#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019730error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019731#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019732 EMSG2(_(e_invarg2), fromstr);
19733 ga_clear(&ga);
19734 return;
19735 }
19736
19737 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019738 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019739 {
19740#ifdef FEAT_MBYTE
19741 if (has_mbyte)
19742 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019743 inlen = (*mb_ptr2len)(in_str);
19744 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019745 cplen = inlen;
19746 idx = 0;
19747 for (p = fromstr; *p != NUL; p += fromlen)
19748 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019749 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019750 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019751 {
19752 for (p = tostr; *p != NUL; p += tolen)
19753 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019754 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019755 if (idx-- == 0)
19756 {
19757 cplen = tolen;
19758 cpstr = p;
19759 break;
19760 }
19761 }
19762 if (*p == NUL) /* tostr is shorter than fromstr */
19763 goto error;
19764 break;
19765 }
19766 ++idx;
19767 }
19768
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019769 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019770 {
19771 /* Check that fromstr and tostr have the same number of
19772 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019773 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019774 first = FALSE;
19775 for (p = tostr; *p != NUL; p += tolen)
19776 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019777 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019778 --idx;
19779 }
19780 if (idx != 0)
19781 goto error;
19782 }
19783
Bram Moolenaarcde88542015-08-11 19:14:00 +020019784 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019785 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019786 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019787
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019788 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019789 }
19790 else
19791#endif
19792 {
19793 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019794 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019795 if (p != NULL)
19796 ga_append(&ga, tostr[p - fromstr]);
19797 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019798 ga_append(&ga, *in_str);
19799 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019800 }
19801 }
19802
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019803 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019804 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019805 ga_append(&ga, NUL);
19806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019808}
19809
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019810#ifdef FEAT_FLOAT
19811/*
19812 * "trunc({float})" function
19813 */
19814 static void
19815f_trunc(argvars, rettv)
19816 typval_T *argvars;
19817 typval_T *rettv;
19818{
19819 float_T f;
19820
19821 rettv->v_type = VAR_FLOAT;
19822 if (get_float_arg(argvars, &f) == OK)
19823 /* trunc() is not in C90, use floor() or ceil() instead. */
19824 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19825 else
19826 rettv->vval.v_float = 0.0;
19827}
19828#endif
19829
Bram Moolenaar8299df92004-07-10 09:47:34 +000019830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 * "type(expr)" function
19832 */
19833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 typval_T *argvars;
19836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019838 int n;
19839
19840 switch (argvars[0].v_type)
19841 {
19842 case VAR_NUMBER: n = 0; break;
19843 case VAR_STRING: n = 1; break;
19844 case VAR_FUNC: n = 2; break;
19845 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019846 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019847#ifdef FEAT_FLOAT
19848 case VAR_FLOAT: n = 5; break;
19849#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019850 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19851 }
19852 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853}
19854
19855/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019856 * "undofile(name)" function
19857 */
19858 static void
19859f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019860 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019861 typval_T *rettv;
19862{
19863 rettv->v_type = VAR_STRING;
19864#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019865 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019866 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019867
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019868 if (*fname == NUL)
19869 {
19870 /* If there is no file name there will be no undo file. */
19871 rettv->vval.v_string = NULL;
19872 }
19873 else
19874 {
19875 char_u *ffname = FullName_save(fname, FALSE);
19876
19877 if (ffname != NULL)
19878 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19879 vim_free(ffname);
19880 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019881 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019882#else
19883 rettv->vval.v_string = NULL;
19884#endif
19885}
19886
19887/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019888 * "undotree()" function
19889 */
19890 static void
19891f_undotree(argvars, rettv)
19892 typval_T *argvars UNUSED;
19893 typval_T *rettv;
19894{
19895 if (rettv_dict_alloc(rettv) == OK)
19896 {
19897 dict_T *dict = rettv->vval.v_dict;
19898 list_T *list;
19899
Bram Moolenaar730cde92010-06-27 05:18:54 +020019900 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019901 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019902 dict_add_nr_str(dict, "save_last",
19903 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019904 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19905 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019906 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019907
19908 list = list_alloc();
19909 if (list != NULL)
19910 {
19911 u_eval_tree(curbuf->b_u_oldhead, list);
19912 dict_add_list(dict, "entries", list);
19913 }
19914 }
19915}
19916
19917/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019918 * "values(dict)" function
19919 */
19920 static void
19921f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 typval_T *argvars;
19923 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019924{
19925 dict_list(argvars, rettv, 1);
19926}
19927
19928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 * "virtcol(string)" function
19930 */
19931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019933 typval_T *argvars;
19934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935{
19936 colnr_T vcol = 0;
19937 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019938 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019940 fp = var2fpos(&argvars[0], FALSE, &fnum);
19941 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19942 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 {
19944 getvvcol(curwin, fp, NULL, NULL, &vcol);
19945 ++vcol;
19946 }
19947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949}
19950
19951/*
19952 * "visualmode()" function
19953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019956 typval_T *argvars UNUSED;
19957 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 char_u str[2];
19960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 str[0] = curbuf->b_visual_mode_eval;
19963 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965
19966 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019967 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969}
19970
19971/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019972 * "wildmenumode()" function
19973 */
19974 static void
19975f_wildmenumode(argvars, rettv)
19976 typval_T *argvars UNUSED;
19977 typval_T *rettv UNUSED;
19978{
19979#ifdef FEAT_WILDMENU
19980 if (wild_menu_showing)
19981 rettv->vval.v_number = 1;
19982#endif
19983}
19984
19985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 * "winbufnr(nr)" function
19987 */
19988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019989f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 typval_T *argvars;
19991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992{
19993 win_T *wp;
19994
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019995 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000}
20001
20002/*
20003 * "wincol()" function
20004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020007 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009{
20010 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020011 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012}
20013
20014/*
20015 * "winheight(nr)" function
20016 */
20017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 typval_T *argvars;
20020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021{
20022 win_T *wp;
20023
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020024 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020028 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029}
20030
20031/*
20032 * "winline()" function
20033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020036 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038{
20039 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041}
20042
20043/*
20044 * "winnr()" function
20045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050{
20051 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020052
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020054 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057}
20058
20059/*
20060 * "winrestcmd()" function
20061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
20067#ifdef FEAT_WINDOWS
20068 win_T *wp;
20069 int winnr = 1;
20070 garray_T ga;
20071 char_u buf[50];
20072
20073 ga_init2(&ga, (int)sizeof(char), 70);
20074 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20075 {
20076 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20077 ga_concat(&ga, buf);
20078# ifdef FEAT_VERTSPLIT
20079 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20080 ga_concat(&ga, buf);
20081# endif
20082 ++winnr;
20083 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020084 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091}
20092
20093/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020094 * "winrestview()" function
20095 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020096 static void
20097f_winrestview(argvars, rettv)
20098 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020099 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020100{
20101 dict_T *dict;
20102
20103 if (argvars[0].v_type != VAR_DICT
20104 || (dict = argvars[0].vval.v_dict) == NULL)
20105 EMSG(_(e_invarg));
20106 else
20107 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020108 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20109 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20110 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20111 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020112#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020113 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20114 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020115#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020116 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20117 {
20118 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20119 curwin->w_set_curswant = FALSE;
20120 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020121
Bram Moolenaar82c25852014-05-28 16:47:16 +020020122 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20123 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020124#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020125 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20126 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020127#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020128 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20129 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20130 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20131 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020132
20133 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020134 win_new_height(curwin, curwin->w_height);
20135# ifdef FEAT_VERTSPLIT
20136 win_new_width(curwin, W_WIDTH(curwin));
20137# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020138 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020139
Bram Moolenaarb851a962014-10-31 15:45:52 +010020140 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020141 curwin->w_topline = 1;
20142 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20143 curwin->w_topline = curbuf->b_ml.ml_line_count;
20144#ifdef FEAT_DIFF
20145 check_topfill(curwin, TRUE);
20146#endif
20147 }
20148}
20149
20150/*
20151 * "winsaveview()" function
20152 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020153 static void
20154f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020155 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020156 typval_T *rettv;
20157{
20158 dict_T *dict;
20159
Bram Moolenaara800b422010-06-27 01:15:55 +020020160 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020161 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020162 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020163
20164 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20165 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20166#ifdef FEAT_VIRTUALEDIT
20167 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20168#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020169 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020170 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20171
20172 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20173#ifdef FEAT_DIFF
20174 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20175#endif
20176 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20177 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20178}
20179
20180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 * "winwidth(nr)" function
20182 */
20183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 typval_T *argvars;
20186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187{
20188 win_T *wp;
20189
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020190 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 else
20194#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020195 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198#endif
20199}
20200
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020202 * Write list of strings to file
20203 */
20204 static int
20205write_list(fd, list, binary)
20206 FILE *fd;
20207 list_T *list;
20208 int binary;
20209{
20210 listitem_T *li;
20211 int c;
20212 int ret = OK;
20213 char_u *s;
20214
20215 for (li = list->lv_first; li != NULL; li = li->li_next)
20216 {
20217 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20218 {
20219 if (*s == '\n')
20220 c = putc(NUL, fd);
20221 else
20222 c = putc(*s, fd);
20223 if (c == EOF)
20224 {
20225 ret = FAIL;
20226 break;
20227 }
20228 }
20229 if (!binary || li->li_next != NULL)
20230 if (putc('\n', fd) == EOF)
20231 {
20232 ret = FAIL;
20233 break;
20234 }
20235 if (ret == FAIL)
20236 {
20237 EMSG(_(e_write));
20238 break;
20239 }
20240 }
20241 return ret;
20242}
20243
20244/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020245 * "writefile()" function
20246 */
20247 static void
20248f_writefile(argvars, rettv)
20249 typval_T *argvars;
20250 typval_T *rettv;
20251{
20252 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020253 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020254 char_u *fname;
20255 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020256 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020257
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020258 if (check_restricted() || check_secure())
20259 return;
20260
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020261 if (argvars[0].v_type != VAR_LIST)
20262 {
20263 EMSG2(_(e_listarg), "writefile()");
20264 return;
20265 }
20266 if (argvars[0].vval.v_list == NULL)
20267 return;
20268
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020269 if (argvars[2].v_type != VAR_UNKNOWN)
20270 {
20271 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20272 binary = TRUE;
20273 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20274 append = TRUE;
20275 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020276
20277 /* Always open the file in binary mode, library functions have a mind of
20278 * their own about CR-LF conversion. */
20279 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020280 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20281 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020282 {
20283 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20284 ret = -1;
20285 }
20286 else
20287 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020288 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20289 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020290 fclose(fd);
20291 }
20292
20293 rettv->vval.v_number = ret;
20294}
20295
20296/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020297 * "xor(expr, expr)" function
20298 */
20299 static void
20300f_xor(argvars, rettv)
20301 typval_T *argvars;
20302 typval_T *rettv;
20303{
20304 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20305 ^ get_tv_number_chk(&argvars[1], NULL);
20306}
20307
20308
20309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020311 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 */
20313 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020314var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020316 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020317 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020319 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020321 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322
Bram Moolenaara5525202006-03-02 22:52:09 +000020323 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020324 if (varp->v_type == VAR_LIST)
20325 {
20326 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020327 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020328 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020329 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020330
20331 l = varp->vval.v_list;
20332 if (l == NULL)
20333 return NULL;
20334
20335 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020336 pos.lnum = list_find_nr(l, 0L, &error);
20337 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020338 return NULL; /* invalid line number */
20339
20340 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020341 pos.col = list_find_nr(l, 1L, &error);
20342 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020343 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020344 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020345
20346 /* We accept "$" for the column number: last column. */
20347 li = list_find(l, 1L);
20348 if (li != NULL && li->li_tv.v_type == VAR_STRING
20349 && li->li_tv.vval.v_string != NULL
20350 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20351 pos.col = len + 1;
20352
Bram Moolenaara5525202006-03-02 22:52:09 +000020353 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020354 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020355 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020356 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020357
Bram Moolenaara5525202006-03-02 22:52:09 +000020358#ifdef FEAT_VIRTUALEDIT
20359 /* Get the virtual offset. Defaults to zero. */
20360 pos.coladd = list_find_nr(l, 2L, &error);
20361 if (error)
20362 pos.coladd = 0;
20363#endif
20364
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020365 return &pos;
20366 }
20367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020368 name = get_tv_string_chk(varp);
20369 if (name == NULL)
20370 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020371 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020373 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20374 {
20375 if (VIsual_active)
20376 return &VIsual;
20377 return &curwin->w_cursor;
20378 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020379 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020381 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20383 return NULL;
20384 return pp;
20385 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020386
20387#ifdef FEAT_VIRTUALEDIT
20388 pos.coladd = 0;
20389#endif
20390
Bram Moolenaar477933c2007-07-17 14:32:23 +000020391 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020392 {
20393 pos.col = 0;
20394 if (name[1] == '0') /* "w0": first visible line */
20395 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020396 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020397 pos.lnum = curwin->w_topline;
20398 return &pos;
20399 }
20400 else if (name[1] == '$') /* "w$": last visible line */
20401 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020402 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020403 pos.lnum = curwin->w_botline - 1;
20404 return &pos;
20405 }
20406 }
20407 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020409 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
20411 pos.lnum = curbuf->b_ml.ml_line_count;
20412 pos.col = 0;
20413 }
20414 else
20415 {
20416 pos.lnum = curwin->w_cursor.lnum;
20417 pos.col = (colnr_T)STRLEN(ml_get_curline());
20418 }
20419 return &pos;
20420 }
20421 return NULL;
20422}
20423
20424/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020425 * Convert list in "arg" into a position and optional file number.
20426 * When "fnump" is NULL there is no file number, only 3 items.
20427 * Note that the column is passed on as-is, the caller may want to decrement
20428 * it to use 1 for the first column.
20429 * Return FAIL when conversion is not possible, doesn't check the position for
20430 * validity.
20431 */
20432 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020433list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020434 typval_T *arg;
20435 pos_T *posp;
20436 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020437 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020438{
20439 list_T *l = arg->vval.v_list;
20440 long i = 0;
20441 long n;
20442
Bram Moolenaar493c1782014-05-28 14:34:46 +020020443 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20444 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020445 if (arg->v_type != VAR_LIST
20446 || l == NULL
20447 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020448 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020449 return FAIL;
20450
20451 if (fnump != NULL)
20452 {
20453 n = list_find_nr(l, i++, NULL); /* fnum */
20454 if (n < 0)
20455 return FAIL;
20456 if (n == 0)
20457 n = curbuf->b_fnum; /* current buffer */
20458 *fnump = n;
20459 }
20460
20461 n = list_find_nr(l, i++, NULL); /* lnum */
20462 if (n < 0)
20463 return FAIL;
20464 posp->lnum = n;
20465
20466 n = list_find_nr(l, i++, NULL); /* col */
20467 if (n < 0)
20468 return FAIL;
20469 posp->col = n;
20470
20471#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020472 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020473 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020474 posp->coladd = 0;
20475 else
20476 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020477#endif
20478
Bram Moolenaar493c1782014-05-28 14:34:46 +020020479 if (curswantp != NULL)
20480 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20481
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020482 return OK;
20483}
20484
20485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 * Get the length of an environment variable name.
20487 * Advance "arg" to the first character after the name.
20488 * Return 0 for error.
20489 */
20490 static int
20491get_env_len(arg)
20492 char_u **arg;
20493{
20494 char_u *p;
20495 int len;
20496
20497 for (p = *arg; vim_isIDc(*p); ++p)
20498 ;
20499 if (p == *arg) /* no name found */
20500 return 0;
20501
20502 len = (int)(p - *arg);
20503 *arg = p;
20504 return len;
20505}
20506
20507/*
20508 * Get the length of the name of a function or internal variable.
20509 * "arg" is advanced to the first non-white character after the name.
20510 * Return 0 if something is wrong.
20511 */
20512 static int
20513get_id_len(arg)
20514 char_u **arg;
20515{
20516 char_u *p;
20517 int len;
20518
20519 /* Find the end of the name. */
20520 for (p = *arg; eval_isnamec(*p); ++p)
20521 ;
20522 if (p == *arg) /* no name found */
20523 return 0;
20524
20525 len = (int)(p - *arg);
20526 *arg = skipwhite(p);
20527
20528 return len;
20529}
20530
20531/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020532 * Get the length of the name of a variable or function.
20533 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020535 * Return -1 if curly braces expansion failed.
20536 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 * If the name contains 'magic' {}'s, expand them and return the
20538 * expanded name in an allocated string via 'alias' - caller must free.
20539 */
20540 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020541get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 char_u **arg;
20543 char_u **alias;
20544 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020545 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546{
20547 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 char_u *p;
20549 char_u *expr_start;
20550 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551
20552 *alias = NULL; /* default to no alias */
20553
20554 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20555 && (*arg)[2] == (int)KE_SNR)
20556 {
20557 /* hard coded <SNR>, already translated */
20558 *arg += 3;
20559 return get_id_len(arg) + 3;
20560 }
20561 len = eval_fname_script(*arg);
20562 if (len > 0)
20563 {
20564 /* literal "<SID>", "s:" or "<SNR>" */
20565 *arg += len;
20566 }
20567
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020569 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020571 p = find_name_end(*arg, &expr_start, &expr_end,
20572 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 if (expr_start != NULL)
20574 {
20575 char_u *temp_string;
20576
20577 if (!evaluate)
20578 {
20579 len += (int)(p - *arg);
20580 *arg = skipwhite(p);
20581 return len;
20582 }
20583
20584 /*
20585 * Include any <SID> etc in the expanded string:
20586 * Thus the -len here.
20587 */
20588 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20589 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020590 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 *alias = temp_string;
20592 *arg = skipwhite(p);
20593 return (int)STRLEN(temp_string);
20594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595
20596 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020597 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 EMSG2(_(e_invexpr2), *arg);
20599
20600 return len;
20601}
20602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020603/*
20604 * Find the end of a variable or function name, taking care of magic braces.
20605 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20606 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020607 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020608 * Return a pointer to just after the name. Equal to "arg" if there is no
20609 * valid name.
20610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020612find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 char_u *arg;
20614 char_u **expr_start;
20615 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020616 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 int mb_nest = 0;
20619 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 char_u *p;
20621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020622 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 *expr_start = NULL;
20625 *expr_end = NULL;
20626 }
20627
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020628 /* Quick check for valid starting character. */
20629 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20630 return arg;
20631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 for (p = arg; *p != NUL
20633 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020634 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020635 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020637 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020638 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020639 if (*p == '\'')
20640 {
20641 /* skip over 'string' to avoid counting [ and ] inside it. */
20642 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20643 ;
20644 if (*p == NUL)
20645 break;
20646 }
20647 else if (*p == '"')
20648 {
20649 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20650 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20651 if (*p == '\\' && p[1] != NUL)
20652 ++p;
20653 if (*p == NUL)
20654 break;
20655 }
20656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020659 if (*p == '[')
20660 ++br_nest;
20661 else if (*p == ']')
20662 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020665 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020667 if (*p == '{')
20668 {
20669 mb_nest++;
20670 if (expr_start != NULL && *expr_start == NULL)
20671 *expr_start = p;
20672 }
20673 else if (*p == '}')
20674 {
20675 mb_nest--;
20676 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20677 *expr_end = p;
20678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 }
20681
20682 return p;
20683}
20684
20685/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020686 * Expands out the 'magic' {}'s in a variable/function name.
20687 * Note that this can call itself recursively, to deal with
20688 * constructs like foo{bar}{baz}{bam}
20689 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20690 * "in_start" ^
20691 * "expr_start" ^
20692 * "expr_end" ^
20693 * "in_end" ^
20694 *
20695 * Returns a new allocated string, which the caller must free.
20696 * Returns NULL for failure.
20697 */
20698 static char_u *
20699make_expanded_name(in_start, expr_start, expr_end, in_end)
20700 char_u *in_start;
20701 char_u *expr_start;
20702 char_u *expr_end;
20703 char_u *in_end;
20704{
20705 char_u c1;
20706 char_u *retval = NULL;
20707 char_u *temp_result;
20708 char_u *nextcmd = NULL;
20709
20710 if (expr_end == NULL || in_end == NULL)
20711 return NULL;
20712 *expr_start = NUL;
20713 *expr_end = NUL;
20714 c1 = *in_end;
20715 *in_end = NUL;
20716
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020717 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020718 if (temp_result != NULL && nextcmd == NULL)
20719 {
20720 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20721 + (in_end - expr_end) + 1));
20722 if (retval != NULL)
20723 {
20724 STRCPY(retval, in_start);
20725 STRCAT(retval, temp_result);
20726 STRCAT(retval, expr_end + 1);
20727 }
20728 }
20729 vim_free(temp_result);
20730
20731 *in_end = c1; /* put char back for error messages */
20732 *expr_start = '{';
20733 *expr_end = '}';
20734
20735 if (retval != NULL)
20736 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020737 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020738 if (expr_start != NULL)
20739 {
20740 /* Further expansion! */
20741 temp_result = make_expanded_name(retval, expr_start,
20742 expr_end, temp_result);
20743 vim_free(retval);
20744 retval = temp_result;
20745 }
20746 }
20747
20748 return retval;
20749}
20750
20751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020753 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 */
20755 static int
20756eval_isnamec(c)
20757 int c;
20758{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020759 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20760}
20761
20762/*
20763 * Return TRUE if character "c" can be used as the first character in a
20764 * variable or function name (excluding '{' and '}').
20765 */
20766 static int
20767eval_isnamec1(c)
20768 int c;
20769{
20770 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771}
20772
20773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 * Set number v: variable to "val".
20775 */
20776 void
20777set_vim_var_nr(idx, val)
20778 int idx;
20779 long val;
20780{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020781 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782}
20783
20784/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020785 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 */
20787 long
20788get_vim_var_nr(idx)
20789 int idx;
20790{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020791 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020794/*
20795 * Get string v: variable value. Uses a static buffer, can only be used once.
20796 */
20797 char_u *
20798get_vim_var_str(idx)
20799 int idx;
20800{
20801 return get_tv_string(&vimvars[idx].vv_tv);
20802}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020803
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020805 * Get List v: variable value. Caller must take care of reference count when
20806 * needed.
20807 */
20808 list_T *
20809get_vim_var_list(idx)
20810 int idx;
20811{
20812 return vimvars[idx].vv_list;
20813}
20814
20815/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020816 * Set v:char to character "c".
20817 */
20818 void
20819set_vim_var_char(c)
20820 int c;
20821{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020822 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020823
20824#ifdef FEAT_MBYTE
20825 if (has_mbyte)
20826 buf[(*mb_char2bytes)(c, buf)] = NUL;
20827 else
20828#endif
20829 {
20830 buf[0] = c;
20831 buf[1] = NUL;
20832 }
20833 set_vim_var_string(VV_CHAR, buf, -1);
20834}
20835
20836/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020837 * Set v:count to "count" and v:count1 to "count1".
20838 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 */
20840 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020841set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 long count;
20843 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020844 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020846 if (set_prevcount)
20847 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020848 vimvars[VV_COUNT].vv_nr = count;
20849 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850}
20851
20852/*
20853 * Set string v: variable to a copy of "val".
20854 */
20855 void
20856set_vim_var_string(idx, val, len)
20857 int idx;
20858 char_u *val;
20859 int len; /* length of "val" to use or -1 (whole string) */
20860{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020861 /* Need to do this (at least) once, since we can't initialize a union.
20862 * Will always be invoked when "v:progname" is set. */
20863 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20864
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020867 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020869 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020871 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872}
20873
20874/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020875 * Set List v: variable to "val".
20876 */
20877 void
20878set_vim_var_list(idx, val)
20879 int idx;
20880 list_T *val;
20881{
20882 list_unref(vimvars[idx].vv_list);
20883 vimvars[idx].vv_list = val;
20884 if (val != NULL)
20885 ++val->lv_refcount;
20886}
20887
20888/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020889 * Set Dictionary v: variable to "val".
20890 */
20891 void
20892set_vim_var_dict(idx, val)
20893 int idx;
20894 dict_T *val;
20895{
20896 int todo;
20897 hashitem_T *hi;
20898
20899 dict_unref(vimvars[idx].vv_dict);
20900 vimvars[idx].vv_dict = val;
20901 if (val != NULL)
20902 {
20903 ++val->dv_refcount;
20904
20905 /* Set readonly */
20906 todo = (int)val->dv_hashtab.ht_used;
20907 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20908 {
20909 if (HASHITEM_EMPTY(hi))
20910 continue;
20911 --todo;
20912 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20913 }
20914 }
20915}
20916
20917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 * Set v:register if needed.
20919 */
20920 void
20921set_reg_var(c)
20922 int c;
20923{
20924 char_u regname;
20925
20926 if (c == 0 || c == ' ')
20927 regname = '"';
20928 else
20929 regname = c;
20930 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020931 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 set_vim_var_string(VV_REG, &regname, 1);
20933}
20934
20935/*
20936 * Get or set v:exception. If "oldval" == NULL, return the current value.
20937 * Otherwise, restore the value to "oldval" and return NULL.
20938 * Must always be called in pairs to save and restore v:exception! Does not
20939 * take care of memory allocations.
20940 */
20941 char_u *
20942v_exception(oldval)
20943 char_u *oldval;
20944{
20945 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020946 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947
Bram Moolenaare9a41262005-01-15 22:18:47 +000020948 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 return NULL;
20950}
20951
20952/*
20953 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20954 * Otherwise, restore the value to "oldval" and return NULL.
20955 * Must always be called in pairs to save and restore v:throwpoint! Does not
20956 * take care of memory allocations.
20957 */
20958 char_u *
20959v_throwpoint(oldval)
20960 char_u *oldval;
20961{
20962 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020963 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964
Bram Moolenaare9a41262005-01-15 22:18:47 +000020965 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 return NULL;
20967}
20968
20969#if defined(FEAT_AUTOCMD) || defined(PROTO)
20970/*
20971 * Set v:cmdarg.
20972 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20973 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20974 * Must always be called in pairs!
20975 */
20976 char_u *
20977set_cmdarg(eap, oldarg)
20978 exarg_T *eap;
20979 char_u *oldarg;
20980{
20981 char_u *oldval;
20982 char_u *newval;
20983 unsigned len;
20984
Bram Moolenaare9a41262005-01-15 22:18:47 +000020985 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020986 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020988 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020989 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020990 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 }
20992
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020993 if (eap->force_bin == FORCE_BIN)
20994 len = 6;
20995 else if (eap->force_bin == FORCE_NOBIN)
20996 len = 8;
20997 else
20998 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020999
21000 if (eap->read_edit)
21001 len += 7;
21002
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021003 if (eap->force_ff != 0)
21004 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21005# ifdef FEAT_MBYTE
21006 if (eap->force_enc != 0)
21007 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021008 if (eap->bad_char != 0)
21009 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021010# endif
21011
21012 newval = alloc(len + 1);
21013 if (newval == NULL)
21014 return NULL;
21015
21016 if (eap->force_bin == FORCE_BIN)
21017 sprintf((char *)newval, " ++bin");
21018 else if (eap->force_bin == FORCE_NOBIN)
21019 sprintf((char *)newval, " ++nobin");
21020 else
21021 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021022
21023 if (eap->read_edit)
21024 STRCAT(newval, " ++edit");
21025
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021026 if (eap->force_ff != 0)
21027 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21028 eap->cmd + eap->force_ff);
21029# ifdef FEAT_MBYTE
21030 if (eap->force_enc != 0)
21031 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21032 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021033 if (eap->bad_char == BAD_KEEP)
21034 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21035 else if (eap->bad_char == BAD_DROP)
21036 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21037 else if (eap->bad_char != 0)
21038 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021039# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021040 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021041 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042}
21043#endif
21044
21045/*
21046 * Get the value of internal variable "name".
21047 * Return OK or FAIL.
21048 */
21049 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021050get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 char_u *name;
21052 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021054 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021055 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021056 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057{
21058 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021059 typval_T *tv = NULL;
21060 typval_T atv;
21061 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063
21064 /* truncate the name, so that we can use strcmp() */
21065 cc = name[len];
21066 name[len] = NUL;
21067
21068 /*
21069 * Check for "b:changedtick".
21070 */
21071 if (STRCMP(name, "b:changedtick") == 0)
21072 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021073 atv.v_type = VAR_NUMBER;
21074 atv.vval.v_number = curbuf->b_changedtick;
21075 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 }
21077
21078 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 * Check for user-defined variables.
21080 */
21081 else
21082 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021083 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021086 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021087 if (dip != NULL)
21088 *dip = v;
21089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 }
21091
Bram Moolenaare9a41262005-01-15 22:18:47 +000021092 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021094 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021095 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 ret = FAIL;
21097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100
21101 name[len] = cc;
21102
21103 return ret;
21104}
21105
21106/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021107 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21108 * Also handle function call with Funcref variable: func(expr)
21109 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21110 */
21111 static int
21112handle_subscript(arg, rettv, evaluate, verbose)
21113 char_u **arg;
21114 typval_T *rettv;
21115 int evaluate; /* do more than finding the end */
21116 int verbose; /* give error messages */
21117{
21118 int ret = OK;
21119 dict_T *selfdict = NULL;
21120 char_u *s;
21121 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021122 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021123
21124 while (ret == OK
21125 && (**arg == '['
21126 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021127 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021128 && !vim_iswhite(*(*arg - 1)))
21129 {
21130 if (**arg == '(')
21131 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021132 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021133 if (evaluate)
21134 {
21135 functv = *rettv;
21136 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021137
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021138 /* Invoke the function. Recursive! */
21139 s = functv.vval.v_string;
21140 }
21141 else
21142 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021143 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021144 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21145 &len, evaluate, selfdict);
21146
21147 /* Clear the funcref afterwards, so that deleting it while
21148 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021149 if (evaluate)
21150 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021151
21152 /* Stop the expression evaluation when immediately aborting on
21153 * error, or when an interrupt occurred or an exception was thrown
21154 * but not caught. */
21155 if (aborting())
21156 {
21157 if (ret == OK)
21158 clear_tv(rettv);
21159 ret = FAIL;
21160 }
21161 dict_unref(selfdict);
21162 selfdict = NULL;
21163 }
21164 else /* **arg == '[' || **arg == '.' */
21165 {
21166 dict_unref(selfdict);
21167 if (rettv->v_type == VAR_DICT)
21168 {
21169 selfdict = rettv->vval.v_dict;
21170 if (selfdict != NULL)
21171 ++selfdict->dv_refcount;
21172 }
21173 else
21174 selfdict = NULL;
21175 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21176 {
21177 clear_tv(rettv);
21178 ret = FAIL;
21179 }
21180 }
21181 }
21182 dict_unref(selfdict);
21183 return ret;
21184}
21185
21186/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021187 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021188 * value).
21189 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021190 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021192{
Bram Moolenaar33570922005-01-25 22:26:29 +000021193 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021194}
21195
21196/*
21197 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 * The string "s" must have been allocated, it is consumed.
21199 * Return NULL for out of memory, the variable otherwise.
21200 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021202alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 char_u *s;
21204{
Bram Moolenaar33570922005-01-25 22:26:29 +000021205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021207 rettv = alloc_tv();
21208 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021210 rettv->v_type = VAR_STRING;
21211 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 }
21213 else
21214 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021215 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216}
21217
21218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021219 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021221 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021222free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021223 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224{
21225 if (varp != NULL)
21226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021227 switch (varp->v_type)
21228 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021229 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021230 func_unref(varp->vval.v_string);
21231 /*FALLTHROUGH*/
21232 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233 vim_free(varp->vval.v_string);
21234 break;
21235 case VAR_LIST:
21236 list_unref(varp->vval.v_list);
21237 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 case VAR_DICT:
21239 dict_unref(varp->vval.v_dict);
21240 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021241 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021242#ifdef FEAT_FLOAT
21243 case VAR_FLOAT:
21244#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021245 case VAR_UNKNOWN:
21246 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021247 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021248 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021249 break;
21250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 vim_free(varp);
21252 }
21253}
21254
21255/*
21256 * Free the memory for a variable value and set the value to NULL or 0.
21257 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021258 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021260 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261{
21262 if (varp != NULL)
21263 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021264 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021266 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021267 func_unref(varp->vval.v_string);
21268 /*FALLTHROUGH*/
21269 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021270 vim_free(varp->vval.v_string);
21271 varp->vval.v_string = NULL;
21272 break;
21273 case VAR_LIST:
21274 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021275 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021276 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021277 case VAR_DICT:
21278 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021279 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021280 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021281 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021282 varp->vval.v_number = 0;
21283 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284#ifdef FEAT_FLOAT
21285 case VAR_FLOAT:
21286 varp->vval.v_float = 0.0;
21287 break;
21288#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021289 case VAR_UNKNOWN:
21290 break;
21291 default:
21292 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021294 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 }
21296}
21297
21298/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021299 * Set the value of a variable to NULL without freeing items.
21300 */
21301 static void
21302init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304{
21305 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021306 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021307}
21308
21309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 * Get the number value of a variable.
21311 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021312 * For incompatible types, return 0.
21313 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21314 * caller of incompatible types: it sets *denote to TRUE if "denote"
21315 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 */
21317 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021318get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021319 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021321 int error = FALSE;
21322
21323 return get_tv_number_chk(varp, &error); /* return 0L on error */
21324}
21325
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021326 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021327get_tv_number_chk(varp, denote)
21328 typval_T *varp;
21329 int *denote;
21330{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021331 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021335 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021336 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021337#ifdef FEAT_FLOAT
21338 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021339 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021340 break;
21341#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021342 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021343 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021344 break;
21345 case VAR_STRING:
21346 if (varp->vval.v_string != NULL)
21347 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021348 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021349 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021350 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021351 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021352 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021353 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021354 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021355 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021356 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021357 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021360 if (denote == NULL) /* useful for values that must be unsigned */
21361 n = -1;
21362 else
21363 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021364 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365}
21366
21367/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021368 * Get the lnum from the first argument.
21369 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021370 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 */
21372 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021373get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021374 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375{
Bram Moolenaar33570922005-01-25 22:26:29 +000021376 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 linenr_T lnum;
21378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021379 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 if (lnum == 0) /* no valid number, try using line() */
21381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021382 rettv.v_type = VAR_NUMBER;
21383 f_line(argvars, &rettv);
21384 lnum = rettv.vval.v_number;
21385 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 }
21387 return lnum;
21388}
21389
21390/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021391 * Get the lnum from the first argument.
21392 * Also accepts "$", then "buf" is used.
21393 * Returns 0 on error.
21394 */
21395 static linenr_T
21396get_tv_lnum_buf(argvars, buf)
21397 typval_T *argvars;
21398 buf_T *buf;
21399{
21400 if (argvars[0].v_type == VAR_STRING
21401 && argvars[0].vval.v_string != NULL
21402 && argvars[0].vval.v_string[0] == '$'
21403 && buf != NULL)
21404 return buf->b_ml.ml_line_count;
21405 return get_tv_number_chk(&argvars[0], NULL);
21406}
21407
21408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 * Get the string value of a variable.
21410 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021411 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21412 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 * If the String variable has never been set, return an empty string.
21414 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021415 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21416 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 */
21418 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021420 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421{
21422 static char_u mybuf[NUMBUFLEN];
21423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021424 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425}
21426
21427 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021428get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021429 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021432 char_u *res = get_tv_string_buf_chk(varp, buf);
21433
21434 return res != NULL ? res : (char_u *)"";
21435}
21436
Bram Moolenaar7d647822014-04-05 21:28:56 +020021437/*
21438 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21439 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021440 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021441get_tv_string_chk(varp)
21442 typval_T *varp;
21443{
21444 static char_u mybuf[NUMBUFLEN];
21445
21446 return get_tv_string_buf_chk(varp, mybuf);
21447}
21448
21449 static char_u *
21450get_tv_string_buf_chk(varp, buf)
21451 typval_T *varp;
21452 char_u *buf;
21453{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021454 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021456 case VAR_NUMBER:
21457 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21458 return buf;
21459 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021460 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021461 break;
21462 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021463 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021464 break;
21465 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021466 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021468#ifdef FEAT_FLOAT
21469 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021470 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021471 break;
21472#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021473 case VAR_STRING:
21474 if (varp->vval.v_string != NULL)
21475 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021476 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021477 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021481 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482}
21483
21484/*
21485 * Find variable "name" in the list of variables.
21486 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021487 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021488 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021492find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021495 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499
Bram Moolenaara7043832005-01-21 11:56:39 +000021500 ht = find_var_ht(name, &varname);
21501 if (htp != NULL)
21502 *htp = ht;
21503 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021505 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506}
21507
21508/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021509 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021510 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021513find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021514 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021515 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021516 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021517 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021518{
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 hashitem_T *hi;
21520
21521 if (*varname == NUL)
21522 {
21523 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021524 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021526 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021527 case 'g': return &globvars_var;
21528 case 'v': return &vimvars_var;
21529 case 'b': return &curbuf->b_bufvar;
21530 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021531#ifdef FEAT_WINDOWS
21532 case 't': return &curtab->tp_winvar;
21533#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021534 case 'l': return current_funccal == NULL
21535 ? NULL : &current_funccal->l_vars_var;
21536 case 'a': return current_funccal == NULL
21537 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021538 }
21539 return NULL;
21540 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021541
21542 hi = hash_find(ht, varname);
21543 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021544 {
21545 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021546 * worked find the variable again. Don't auto-load a script if it was
21547 * loaded already, otherwise it would be loaded every time when
21548 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021549 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021550 {
21551 /* Note: script_autoload() may make "hi" invalid. It must either
21552 * be obtained again or not used. */
21553 if (!script_autoload(varname, FALSE) || aborting())
21554 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021555 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021556 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021557 if (HASHITEM_EMPTY(hi))
21558 return NULL;
21559 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021561}
21562
21563/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021565 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021566 * Set "varname" to the start of name without ':'.
21567 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021569find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 char_u *name;
21571 char_u **varname;
21572{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021573 hashitem_T *hi;
21574
Bram Moolenaar73627d02015-08-11 15:46:09 +020021575 if (name[0] == NUL)
21576 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 if (name[1] != ':')
21578 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021579 /* The name must not start with a colon or #. */
21580 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 return NULL;
21582 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021583
21584 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021585 hi = hash_find(&compat_hashtab, name);
21586 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021587 return &compat_hashtab;
21588
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 return &globvarht; /* global variable */
21591 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 }
21593 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021594 if (*name == 'g') /* global variable */
21595 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021596 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21597 */
21598 if (vim_strchr(name + 2, ':') != NULL
21599 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021602 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021604 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021605#ifdef FEAT_WINDOWS
21606 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021607 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021608#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021609 if (*name == 'v') /* v: variable */
21610 return &vimvarht;
21611 if (*name == 'a' && current_funccal != NULL) /* function argument */
21612 return &current_funccal->l_avars.dv_hashtab;
21613 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21614 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 if (*name == 's' /* script variable */
21616 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21617 return &SCRIPT_VARS(current_SID);
21618 return NULL;
21619}
21620
21621/*
21622 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021623 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 * Returns NULL when it doesn't exist.
21625 */
21626 char_u *
21627get_var_value(name)
21628 char_u *name;
21629{
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021632 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 if (v == NULL)
21634 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636}
21637
21638/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 * sourcing this script and when executing functions defined in the script.
21641 */
21642 void
21643new_script_vars(id)
21644 scid_T id;
21645{
Bram Moolenaara7043832005-01-21 11:56:39 +000021646 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021647 hashtab_T *ht;
21648 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021649
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021652 /* Re-allocating ga_data means that an ht_array pointing to
21653 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021655 for (i = 1; i <= ga_scripts.ga_len; ++i)
21656 {
21657 ht = &SCRIPT_VARS(i);
21658 if (ht->ht_mask == HT_INIT_SIZE - 1)
21659 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021660 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021662 }
21663
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 while (ga_scripts.ga_len < id)
21665 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021666 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021667 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021668 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 }
21671 }
21672}
21673
21674/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21676 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 */
21678 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021679init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 dict_T *dict;
21681 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021682 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683{
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021685 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021686 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021687 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021688 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 dict_var->di_tv.vval.v_dict = dict;
21690 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021691 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021692 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21693 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694}
21695
21696/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021697 * Unreference a dictionary initialized by init_var_dict().
21698 */
21699 void
21700unref_var_dict(dict)
21701 dict_T *dict;
21702{
21703 /* Now the dict needs to be freed if no one else is using it, go back to
21704 * normal reference counting. */
21705 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21706 dict_unref(dict);
21707}
21708
21709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 * Frees all allocated variables and the value they contain.
21712 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 */
21714 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021715vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 hashtab_T *ht;
21717{
21718 vars_clear_ext(ht, TRUE);
21719}
21720
21721/*
21722 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21723 */
21724 static void
21725vars_clear_ext(ht, free_val)
21726 hashtab_T *ht;
21727 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728{
Bram Moolenaara7043832005-01-21 11:56:39 +000021729 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 hashitem_T *hi;
21731 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021734 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021735 for (hi = ht->ht_array; todo > 0; ++hi)
21736 {
21737 if (!HASHITEM_EMPTY(hi))
21738 {
21739 --todo;
21740
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021742 * ht_array might change then. hash_clear() takes care of it
21743 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 v = HI2DI(hi);
21745 if (free_val)
21746 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021747 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021749 }
21750 }
21751 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021752 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753}
21754
Bram Moolenaara7043832005-01-21 11:56:39 +000021755/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 * Delete a variable from hashtab "ht" at item "hi".
21757 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021760delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021761 hashtab_T *ht;
21762 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763{
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021765
21766 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 clear_tv(&di->di_tv);
21768 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769}
21770
21771/*
21772 * List the value of one internal variable.
21773 */
21774 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021775list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021776 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021778 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021780 char_u *tofree;
21781 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021782 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021783
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021784 current_copyID += COPYID_INC;
21785 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021787 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789}
21790
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021792list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 char_u *prefix;
21794 char_u *name;
21795 int type;
21796 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021797 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798{
Bram Moolenaar31859182007-08-14 20:41:13 +000021799 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21800 msg_start();
21801 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 if (name != NULL) /* "a:" vars don't have a name stored */
21803 msg_puts(name);
21804 msg_putchar(' ');
21805 msg_advance(22);
21806 if (type == VAR_NUMBER)
21807 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021808 else if (type == VAR_FUNC)
21809 msg_putchar('*');
21810 else if (type == VAR_LIST)
21811 {
21812 msg_putchar('[');
21813 if (*string == '[')
21814 ++string;
21815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021816 else if (type == VAR_DICT)
21817 {
21818 msg_putchar('{');
21819 if (*string == '{')
21820 ++string;
21821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 else
21823 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021824
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021826
21827 if (type == VAR_FUNC)
21828 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021829 if (*first)
21830 {
21831 msg_clr_eos();
21832 *first = FALSE;
21833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834}
21835
21836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021837 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 * If the variable already exists, the value is updated.
21839 * Otherwise the variable is created.
21840 */
21841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021842set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021844 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021845 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846{
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021851 ht = find_var_ht(name, &varname);
21852 if (ht == NULL || *varname == NUL)
21853 {
21854 EMSG2(_(e_illvar), name);
21855 return;
21856 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021857 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021858
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021859 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21860 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021861
Bram Moolenaar33570922005-01-25 22:26:29 +000021862 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021865 if (var_check_ro(v->di_flags, name, FALSE)
21866 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 return;
21868 if (v->di_tv.v_type != tv->v_type
21869 && !((v->di_tv.v_type == VAR_STRING
21870 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021871 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021872 || tv->v_type == VAR_NUMBER))
21873#ifdef FEAT_FLOAT
21874 && !((v->di_tv.v_type == VAR_NUMBER
21875 || v->di_tv.v_type == VAR_FLOAT)
21876 && (tv->v_type == VAR_NUMBER
21877 || tv->v_type == VAR_FLOAT))
21878#endif
21879 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021881 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 return;
21883 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021884
21885 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021886 * Handle setting internal v: variables separately where needed to
21887 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 */
21889 if (ht == &vimvarht)
21890 {
21891 if (v->di_tv.v_type == VAR_STRING)
21892 {
21893 vim_free(v->di_tv.vval.v_string);
21894 if (copy || tv->v_type != VAR_STRING)
21895 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21896 else
21897 {
21898 /* Take over the string to avoid an extra alloc/free. */
21899 v->di_tv.vval.v_string = tv->vval.v_string;
21900 tv->vval.v_string = NULL;
21901 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021902 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021904 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021907 if (STRCMP(varname, "searchforward") == 0)
21908 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021909#ifdef FEAT_SEARCH_EXTRA
21910 else if (STRCMP(varname, "hlsearch") == 0)
21911 {
21912 no_hlsearch = !v->di_tv.vval.v_number;
21913 redraw_all_later(SOME_VALID);
21914 }
21915#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021916 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021917 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021918 else if (v->di_tv.v_type != tv->v_type)
21919 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021920 }
21921
21922 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 }
21924 else /* add a new variable */
21925 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021926 /* Can't add "v:" variable. */
21927 if (ht == &vimvarht)
21928 {
21929 EMSG2(_(e_illvar), name);
21930 return;
21931 }
21932
Bram Moolenaar92124a32005-06-17 22:03:40 +000021933 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021934 if (!valid_varname(varname))
21935 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021936
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021937 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21938 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021939 if (v == NULL)
21940 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021944 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 return;
21946 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021947 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021949
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021950 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021952 else
21953 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021954 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021955 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021956 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958}
21959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021961 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 * Also give an error message.
21963 */
21964 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021965var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 int flags;
21967 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021968 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021969{
21970 if (flags & DI_FLAGS_RO)
21971 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021972 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 return TRUE;
21974 }
21975 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021977 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 return TRUE;
21979 }
21980 return FALSE;
21981}
21982
21983/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021984 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21985 * Also give an error message.
21986 */
21987 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021988var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021989 int flags;
21990 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021991 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021992{
21993 if (flags & DI_FLAGS_FIX)
21994 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021995 EMSG2(_("E795: Cannot delete variable %s"),
21996 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021997 return TRUE;
21998 }
21999 return FALSE;
22000}
22001
22002/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022003 * Check if a funcref is assigned to a valid variable name.
22004 * Return TRUE and give an error if not.
22005 */
22006 static int
22007var_check_func_name(name, new_var)
22008 char_u *name; /* points to start of variable name */
22009 int new_var; /* TRUE when creating the variable */
22010{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022011 /* Allow for w: b: s: and t:. */
22012 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022013 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22014 ? name[2] : name[0]))
22015 {
22016 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22017 name);
22018 return TRUE;
22019 }
22020 /* Don't allow hiding a function. When "v" is not NULL we might be
22021 * assigning another function to the same var, the type is checked
22022 * below. */
22023 if (new_var && function_exists(name))
22024 {
22025 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22026 name);
22027 return TRUE;
22028 }
22029 return FALSE;
22030}
22031
22032/*
22033 * Check if a variable name is valid.
22034 * Return FALSE and give an error if not.
22035 */
22036 static int
22037valid_varname(varname)
22038 char_u *varname;
22039{
22040 char_u *p;
22041
22042 for (p = varname; *p != NUL; ++p)
22043 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22044 && *p != AUTOLOAD_CHAR)
22045 {
22046 EMSG2(_(e_illvar), varname);
22047 return FALSE;
22048 }
22049 return TRUE;
22050}
22051
22052/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022053 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022054 * Also give an error message, using "name" or _("name") when use_gettext is
22055 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022056 */
22057 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022058tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022059 int lock;
22060 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022061 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022062{
22063 if (lock & VAR_LOCKED)
22064 {
22065 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022066 name == NULL ? (char_u *)_("Unknown")
22067 : use_gettext ? (char_u *)_(name)
22068 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022069 return TRUE;
22070 }
22071 if (lock & VAR_FIXED)
22072 {
22073 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022074 name == NULL ? (char_u *)_("Unknown")
22075 : use_gettext ? (char_u *)_(name)
22076 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022077 return TRUE;
22078 }
22079 return FALSE;
22080}
22081
22082/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022085 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022086 * It is OK for "from" and "to" to point to the same item. This is used to
22087 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022088 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022089 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022090copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 typval_T *from;
22092 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022094 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022095 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096 switch (from->v_type)
22097 {
22098 case VAR_NUMBER:
22099 to->vval.v_number = from->vval.v_number;
22100 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022101#ifdef FEAT_FLOAT
22102 case VAR_FLOAT:
22103 to->vval.v_float = from->vval.v_float;
22104 break;
22105#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 case VAR_STRING:
22107 case VAR_FUNC:
22108 if (from->vval.v_string == NULL)
22109 to->vval.v_string = NULL;
22110 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022111 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022112 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022113 if (from->v_type == VAR_FUNC)
22114 func_ref(to->vval.v_string);
22115 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022116 break;
22117 case VAR_LIST:
22118 if (from->vval.v_list == NULL)
22119 to->vval.v_list = NULL;
22120 else
22121 {
22122 to->vval.v_list = from->vval.v_list;
22123 ++to->vval.v_list->lv_refcount;
22124 }
22125 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022126 case VAR_DICT:
22127 if (from->vval.v_dict == NULL)
22128 to->vval.v_dict = NULL;
22129 else
22130 {
22131 to->vval.v_dict = from->vval.v_dict;
22132 ++to->vval.v_dict->dv_refcount;
22133 }
22134 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022137 break;
22138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139}
22140
22141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022142 * Make a copy of an item.
22143 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022144 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22145 * reference to an already copied list/dict can be used.
22146 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022147 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022148 static int
22149item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022150 typval_T *from;
22151 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022152 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022153 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022154{
22155 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022156 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022157
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022159 {
22160 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022161 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022162 }
22163 ++recurse;
22164
22165 switch (from->v_type)
22166 {
22167 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022168#ifdef FEAT_FLOAT
22169 case VAR_FLOAT:
22170#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022171 case VAR_STRING:
22172 case VAR_FUNC:
22173 copy_tv(from, to);
22174 break;
22175 case VAR_LIST:
22176 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022177 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022178 if (from->vval.v_list == NULL)
22179 to->vval.v_list = NULL;
22180 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22181 {
22182 /* use the copy made earlier */
22183 to->vval.v_list = from->vval.v_list->lv_copylist;
22184 ++to->vval.v_list->lv_refcount;
22185 }
22186 else
22187 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22188 if (to->vval.v_list == NULL)
22189 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022190 break;
22191 case VAR_DICT:
22192 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022193 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022194 if (from->vval.v_dict == NULL)
22195 to->vval.v_dict = NULL;
22196 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22197 {
22198 /* use the copy made earlier */
22199 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22200 ++to->vval.v_dict->dv_refcount;
22201 }
22202 else
22203 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22204 if (to->vval.v_dict == NULL)
22205 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022206 break;
22207 default:
22208 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022209 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022210 }
22211 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022212 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022213}
22214
22215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 * ":echo expr1 ..." print each argument separated with a space, add a
22217 * newline at the end.
22218 * ":echon expr1 ..." print each argument plain.
22219 */
22220 void
22221ex_echo(eap)
22222 exarg_T *eap;
22223{
22224 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022226 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 char_u *p;
22228 int needclr = TRUE;
22229 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022230 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231
22232 if (eap->skip)
22233 ++emsg_skip;
22234 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22235 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022236 /* If eval1() causes an error message the text from the command may
22237 * still need to be cleared. E.g., "echo 22,44". */
22238 need_clr_eos = needclr;
22239
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 {
22243 /*
22244 * Report the invalid expression unless the expression evaluation
22245 * has been cancelled due to an aborting error, an interrupt, or an
22246 * exception.
22247 */
22248 if (!aborting())
22249 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022250 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 break;
22252 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022253 need_clr_eos = FALSE;
22254
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 if (!eap->skip)
22256 {
22257 if (atstart)
22258 {
22259 atstart = FALSE;
22260 /* Call msg_start() after eval1(), evaluating the expression
22261 * may cause a message to appear. */
22262 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022263 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022264 /* Mark the saved text as finishing the line, so that what
22265 * follows is displayed on a new line when scrolling back
22266 * at the more prompt. */
22267 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 }
22271 else if (eap->cmdidx == CMD_echo)
22272 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022273 current_copyID += COPYID_INC;
22274 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022275 if (p != NULL)
22276 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022278 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022280 if (*p != TAB && needclr)
22281 {
22282 /* remove any text still there from the command */
22283 msg_clr_eos();
22284 needclr = FALSE;
22285 }
22286 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 }
22288 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022289 {
22290#ifdef FEAT_MBYTE
22291 if (has_mbyte)
22292 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022293 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022294
22295 (void)msg_outtrans_len_attr(p, i, echo_attr);
22296 p += i - 1;
22297 }
22298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 arg = skipwhite(arg);
22307 }
22308 eap->nextcmd = check_nextcmd(arg);
22309
22310 if (eap->skip)
22311 --emsg_skip;
22312 else
22313 {
22314 /* remove text that may still be there from the command */
22315 if (needclr)
22316 msg_clr_eos();
22317 if (eap->cmdidx == CMD_echo)
22318 msg_end();
22319 }
22320}
22321
22322/*
22323 * ":echohl {name}".
22324 */
22325 void
22326ex_echohl(eap)
22327 exarg_T *eap;
22328{
22329 int id;
22330
22331 id = syn_name2id(eap->arg);
22332 if (id == 0)
22333 echo_attr = 0;
22334 else
22335 echo_attr = syn_id2attr(id);
22336}
22337
22338/*
22339 * ":execute expr1 ..." execute the result of an expression.
22340 * ":echomsg expr1 ..." Print a message
22341 * ":echoerr expr1 ..." Print an error
22342 * Each gets spaces around each argument and a newline at the end for
22343 * echo commands
22344 */
22345 void
22346ex_execute(eap)
22347 exarg_T *eap;
22348{
22349 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 int ret = OK;
22352 char_u *p;
22353 garray_T ga;
22354 int len;
22355 int save_did_emsg;
22356
22357 ga_init2(&ga, 1, 80);
22358
22359 if (eap->skip)
22360 ++emsg_skip;
22361 while (*arg != NUL && *arg != '|' && *arg != '\n')
22362 {
22363 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022364 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
22366 /*
22367 * Report the invalid expression unless the expression evaluation
22368 * has been cancelled due to an aborting error, an interrupt, or an
22369 * exception.
22370 */
22371 if (!aborting())
22372 EMSG2(_(e_invexpr2), p);
22373 ret = FAIL;
22374 break;
22375 }
22376
22377 if (!eap->skip)
22378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022379 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 len = (int)STRLEN(p);
22381 if (ga_grow(&ga, len + 2) == FAIL)
22382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022383 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 ret = FAIL;
22385 break;
22386 }
22387 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 ga.ga_len += len;
22391 }
22392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 arg = skipwhite(arg);
22395 }
22396
22397 if (ret != FAIL && ga.ga_data != NULL)
22398 {
22399 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022402 out_flush();
22403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 else if (eap->cmdidx == CMD_echoerr)
22405 {
22406 /* We don't want to abort following commands, restore did_emsg. */
22407 save_did_emsg = did_emsg;
22408 EMSG((char_u *)ga.ga_data);
22409 if (!force_abort)
22410 did_emsg = save_did_emsg;
22411 }
22412 else if (eap->cmdidx == CMD_execute)
22413 do_cmdline((char_u *)ga.ga_data,
22414 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22415 }
22416
22417 ga_clear(&ga);
22418
22419 if (eap->skip)
22420 --emsg_skip;
22421
22422 eap->nextcmd = check_nextcmd(arg);
22423}
22424
22425/*
22426 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22427 * "arg" points to the "&" or '+' when called, to "option" when returning.
22428 * Returns NULL when no option name found. Otherwise pointer to the char
22429 * after the option name.
22430 */
22431 static char_u *
22432find_option_end(arg, opt_flags)
22433 char_u **arg;
22434 int *opt_flags;
22435{
22436 char_u *p = *arg;
22437
22438 ++p;
22439 if (*p == 'g' && p[1] == ':')
22440 {
22441 *opt_flags = OPT_GLOBAL;
22442 p += 2;
22443 }
22444 else if (*p == 'l' && p[1] == ':')
22445 {
22446 *opt_flags = OPT_LOCAL;
22447 p += 2;
22448 }
22449 else
22450 *opt_flags = 0;
22451
22452 if (!ASCII_ISALPHA(*p))
22453 return NULL;
22454 *arg = p;
22455
22456 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22457 p += 4; /* termcap option */
22458 else
22459 while (ASCII_ISALPHA(*p))
22460 ++p;
22461 return p;
22462}
22463
22464/*
22465 * ":function"
22466 */
22467 void
22468ex_function(eap)
22469 exarg_T *eap;
22470{
22471 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022472 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 int j;
22474 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022476 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 char_u *name = NULL;
22478 char_u *p;
22479 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022480 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 garray_T newargs;
22482 garray_T newlines;
22483 int varargs = FALSE;
22484 int mustend = FALSE;
22485 int flags = 0;
22486 ufunc_T *fp;
22487 int indent;
22488 int nesting;
22489 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 dictitem_T *v;
22491 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492 static int func_nr = 0; /* number for nameless function */
22493 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494 hashtab_T *ht;
22495 int todo;
22496 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022497 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498
22499 /*
22500 * ":function" without argument: list functions.
22501 */
22502 if (ends_excmd(*eap->arg))
22503 {
22504 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022505 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022506 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022507 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022508 {
22509 if (!HASHITEM_EMPTY(hi))
22510 {
22511 --todo;
22512 fp = HI2UF(hi);
22513 if (!isdigit(*fp->uf_name))
22514 list_func_head(fp, FALSE);
22515 }
22516 }
22517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 eap->nextcmd = check_nextcmd(eap->arg);
22519 return;
22520 }
22521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022522 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022523 * ":function /pat": list functions matching pattern.
22524 */
22525 if (*eap->arg == '/')
22526 {
22527 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22528 if (!eap->skip)
22529 {
22530 regmatch_T regmatch;
22531
22532 c = *p;
22533 *p = NUL;
22534 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22535 *p = c;
22536 if (regmatch.regprog != NULL)
22537 {
22538 regmatch.rm_ic = p_ic;
22539
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022540 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022541 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22542 {
22543 if (!HASHITEM_EMPTY(hi))
22544 {
22545 --todo;
22546 fp = HI2UF(hi);
22547 if (!isdigit(*fp->uf_name)
22548 && vim_regexec(&regmatch, fp->uf_name, 0))
22549 list_func_head(fp, FALSE);
22550 }
22551 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022552 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022553 }
22554 }
22555 if (*p == '/')
22556 ++p;
22557 eap->nextcmd = check_nextcmd(p);
22558 return;
22559 }
22560
22561 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022562 * Get the function name. There are these situations:
22563 * func normal function name
22564 * "name" == func, "fudi.fd_dict" == NULL
22565 * dict.func new dictionary entry
22566 * "name" == NULL, "fudi.fd_dict" set,
22567 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22568 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022569 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022570 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22571 * dict.func existing dict entry that's not a Funcref
22572 * "name" == NULL, "fudi.fd_dict" set,
22573 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022574 * s:func script-local function name
22575 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022578 name = trans_function_name(&p, eap->skip, 0, &fudi);
22579 paren = (vim_strchr(p, '(') != NULL);
22580 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
22582 /*
22583 * Return on an invalid expression in braces, unless the expression
22584 * evaluation has been cancelled due to an aborting error, an
22585 * interrupt, or an exception.
22586 */
22587 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022589 if (!eap->skip && fudi.fd_newkey != NULL)
22590 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022591 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 else
22595 eap->skip = TRUE;
22596 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022597
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 /* An error in a function call during evaluation of an expression in magic
22599 * braces should not cause the function not to be defined. */
22600 saved_did_emsg = did_emsg;
22601 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602
22603 /*
22604 * ":function func" with only function name: list function.
22605 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022606 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 {
22608 if (!ends_excmd(*skipwhite(p)))
22609 {
22610 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
22613 eap->nextcmd = check_nextcmd(p);
22614 if (eap->nextcmd != NULL)
22615 *p = NUL;
22616 if (!eap->skip && !got_int)
22617 {
22618 fp = find_func(name);
22619 if (fp != NULL)
22620 {
22621 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022622 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022624 if (FUNCLINE(fp, j) == NULL)
22625 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 msg_putchar('\n');
22627 msg_outnum((long)(j + 1));
22628 if (j < 9)
22629 msg_putchar(' ');
22630 if (j < 99)
22631 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022632 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 out_flush(); /* show a line at a time */
22634 ui_breakcheck();
22635 }
22636 if (!got_int)
22637 {
22638 msg_putchar('\n');
22639 msg_puts((char_u *)" endfunction");
22640 }
22641 }
22642 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022643 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 }
22647
22648 /*
22649 * ":function name(arg1, arg2)" Define function.
22650 */
22651 p = skipwhite(p);
22652 if (*p != '(')
22653 {
22654 if (!eap->skip)
22655 {
22656 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022657 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 }
22659 /* attempt to continue by skipping some text */
22660 if (vim_strchr(p, '(') != NULL)
22661 p = vim_strchr(p, '(');
22662 }
22663 p = skipwhite(p + 1);
22664
22665 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22666 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22667
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022668 if (!eap->skip)
22669 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022670 /* Check the name of the function. Unless it's a dictionary function
22671 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022672 if (name != NULL)
22673 arg = name;
22674 else
22675 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022676 if (arg != NULL && (fudi.fd_di == NULL
22677 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022678 {
22679 if (*arg == K_SPECIAL)
22680 j = 3;
22681 else
22682 j = 0;
22683 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22684 : eval_isnamec(arg[j])))
22685 ++j;
22686 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022687 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022688 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022689 /* Disallow using the g: dict. */
22690 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22691 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022692 }
22693
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 /*
22695 * Isolate the arguments: "arg1, arg2, ...)"
22696 */
22697 while (*p != ')')
22698 {
22699 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22700 {
22701 varargs = TRUE;
22702 p += 3;
22703 mustend = TRUE;
22704 }
22705 else
22706 {
22707 arg = p;
22708 while (ASCII_ISALNUM(*p) || *p == '_')
22709 ++p;
22710 if (arg == p || isdigit(*arg)
22711 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22712 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22713 {
22714 if (!eap->skip)
22715 EMSG2(_("E125: Illegal argument: %s"), arg);
22716 break;
22717 }
22718 if (ga_grow(&newargs, 1) == FAIL)
22719 goto erret;
22720 c = *p;
22721 *p = NUL;
22722 arg = vim_strsave(arg);
22723 if (arg == NULL)
22724 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022725
22726 /* Check for duplicate argument name. */
22727 for (i = 0; i < newargs.ga_len; ++i)
22728 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22729 {
22730 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022731 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022732 goto erret;
22733 }
22734
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22736 *p = c;
22737 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 if (*p == ',')
22739 ++p;
22740 else
22741 mustend = TRUE;
22742 }
22743 p = skipwhite(p);
22744 if (mustend && *p != ')')
22745 {
22746 if (!eap->skip)
22747 EMSG2(_(e_invarg2), eap->arg);
22748 break;
22749 }
22750 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022751 if (*p != ')')
22752 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 ++p; /* skip the ')' */
22754
Bram Moolenaare9a41262005-01-15 22:18:47 +000022755 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 for (;;)
22757 {
22758 p = skipwhite(p);
22759 if (STRNCMP(p, "range", 5) == 0)
22760 {
22761 flags |= FC_RANGE;
22762 p += 5;
22763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022764 else if (STRNCMP(p, "dict", 4) == 0)
22765 {
22766 flags |= FC_DICT;
22767 p += 4;
22768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 else if (STRNCMP(p, "abort", 5) == 0)
22770 {
22771 flags |= FC_ABORT;
22772 p += 5;
22773 }
22774 else
22775 break;
22776 }
22777
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022778 /* When there is a line break use what follows for the function body.
22779 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22780 if (*p == '\n')
22781 line_arg = p + 1;
22782 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 EMSG(_(e_trailing));
22784
22785 /*
22786 * Read the body of the function, until ":endfunction" is found.
22787 */
22788 if (KeyTyped)
22789 {
22790 /* Check if the function already exists, don't let the user type the
22791 * whole function before telling him it doesn't work! For a script we
22792 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022793 if (!eap->skip && !eap->forceit)
22794 {
22795 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22796 EMSG(_(e_funcdict));
22797 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022798 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022801 if (!eap->skip && did_emsg)
22802 goto erret;
22803
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 msg_putchar('\n'); /* don't overwrite the function name */
22805 cmdline_row = msg_row;
22806 }
22807
22808 indent = 2;
22809 nesting = 0;
22810 for (;;)
22811 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022812 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022813 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022814 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022815 saved_wait_return = FALSE;
22816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022818 sourcing_lnum_off = sourcing_lnum;
22819
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022820 if (line_arg != NULL)
22821 {
22822 /* Use eap->arg, split up in parts by line breaks. */
22823 theline = line_arg;
22824 p = vim_strchr(theline, '\n');
22825 if (p == NULL)
22826 line_arg += STRLEN(line_arg);
22827 else
22828 {
22829 *p = NUL;
22830 line_arg = p + 1;
22831 }
22832 }
22833 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 theline = getcmdline(':', 0L, indent);
22835 else
22836 theline = eap->getline(':', eap->cookie, indent);
22837 if (KeyTyped)
22838 lines_left = Rows - 1;
22839 if (theline == NULL)
22840 {
22841 EMSG(_("E126: Missing :endfunction"));
22842 goto erret;
22843 }
22844
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022845 /* Detect line continuation: sourcing_lnum increased more than one. */
22846 if (sourcing_lnum > sourcing_lnum_off + 1)
22847 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22848 else
22849 sourcing_lnum_off = 0;
22850
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 if (skip_until != NULL)
22852 {
22853 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22854 * don't check for ":endfunc". */
22855 if (STRCMP(theline, skip_until) == 0)
22856 {
22857 vim_free(skip_until);
22858 skip_until = NULL;
22859 }
22860 }
22861 else
22862 {
22863 /* skip ':' and blanks*/
22864 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22865 ;
22866
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022867 /* Check for "endfunction". */
22868 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022870 if (line_arg == NULL)
22871 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 break;
22873 }
22874
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022875 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 * at "end". */
22877 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22878 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022879 else if (STRNCMP(p, "if", 2) == 0
22880 || STRNCMP(p, "wh", 2) == 0
22881 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 || STRNCMP(p, "try", 3) == 0)
22883 indent += 2;
22884
22885 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022886 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022888 if (*p == '!')
22889 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022891 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22892 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022894 ++nesting;
22895 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 }
22897 }
22898
22899 /* Check for ":append" or ":insert". */
22900 p = skip_range(p, NULL);
22901 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22902 || (p[0] == 'i'
22903 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22904 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22905 skip_until = vim_strsave((char_u *)".");
22906
22907 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22908 arg = skipwhite(skiptowhite(p));
22909 if (arg[0] == '<' && arg[1] =='<'
22910 && ((p[0] == 'p' && p[1] == 'y'
22911 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22912 || (p[0] == 'p' && p[1] == 'e'
22913 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22914 || (p[0] == 't' && p[1] == 'c'
22915 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022916 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22917 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22919 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022920 || (p[0] == 'm' && p[1] == 'z'
22921 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 ))
22923 {
22924 /* ":python <<" continues until a dot, like ":append" */
22925 p = skipwhite(arg + 2);
22926 if (*p == NUL)
22927 skip_until = vim_strsave((char_u *)".");
22928 else
22929 skip_until = vim_strsave(p);
22930 }
22931 }
22932
22933 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022934 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022935 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022936 if (line_arg == NULL)
22937 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022939 }
22940
22941 /* Copy the line to newly allocated memory. get_one_sourceline()
22942 * allocates 250 bytes per line, this saves 80% on average. The cost
22943 * is an extra alloc/free. */
22944 p = vim_strsave(theline);
22945 if (p != NULL)
22946 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022947 if (line_arg == NULL)
22948 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022949 theline = p;
22950 }
22951
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022952 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22953
22954 /* Add NULL lines for continuation lines, so that the line count is
22955 * equal to the index in the growarray. */
22956 while (sourcing_lnum_off-- > 0)
22957 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022958
22959 /* Check for end of eap->arg. */
22960 if (line_arg != NULL && *line_arg == NUL)
22961 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 }
22963
22964 /* Don't define the function when skipping commands or when an error was
22965 * detected. */
22966 if (eap->skip || did_emsg)
22967 goto erret;
22968
22969 /*
22970 * If there are no errors, add the function
22971 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022972 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022973 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022974 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022976 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022977 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022978 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 goto erret;
22980 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 fp = find_func(name);
22983 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022985 if (!eap->forceit)
22986 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022987 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022988 goto erret;
22989 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022990 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022991 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022992 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022993 name);
22994 goto erret;
22995 }
22996 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022997 ga_clear_strings(&(fp->uf_args));
22998 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022999 vim_free(name);
23000 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 }
23003 else
23004 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023005 char numbuf[20];
23006
23007 fp = NULL;
23008 if (fudi.fd_newkey == NULL && !eap->forceit)
23009 {
23010 EMSG(_(e_funcdict));
23011 goto erret;
23012 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023013 if (fudi.fd_di == NULL)
23014 {
23015 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023016 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023017 goto erret;
23018 }
23019 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023020 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023021 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023022
23023 /* Give the function a sequential number. Can only be used with a
23024 * Funcref! */
23025 vim_free(name);
23026 sprintf(numbuf, "%d", ++func_nr);
23027 name = vim_strsave((char_u *)numbuf);
23028 if (name == NULL)
23029 goto erret;
23030 }
23031
23032 if (fp == NULL)
23033 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023034 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023035 {
23036 int slen, plen;
23037 char_u *scriptname;
23038
23039 /* Check that the autoload name matches the script name. */
23040 j = FAIL;
23041 if (sourcing_name != NULL)
23042 {
23043 scriptname = autoload_name(name);
23044 if (scriptname != NULL)
23045 {
23046 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023047 plen = (int)STRLEN(p);
23048 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023049 if (slen > plen && fnamecmp(p,
23050 sourcing_name + slen - plen) == 0)
23051 j = OK;
23052 vim_free(scriptname);
23053 }
23054 }
23055 if (j == FAIL)
23056 {
23057 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23058 goto erret;
23059 }
23060 }
23061
23062 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 if (fp == NULL)
23064 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023065
23066 if (fudi.fd_dict != NULL)
23067 {
23068 if (fudi.fd_di == NULL)
23069 {
23070 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023071 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023072 if (fudi.fd_di == NULL)
23073 {
23074 vim_free(fp);
23075 goto erret;
23076 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023077 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23078 {
23079 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023080 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023081 goto erret;
23082 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023083 }
23084 else
23085 /* overwrite existing dict entry */
23086 clear_tv(&fudi.fd_di->di_tv);
23087 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023088 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023089 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023090 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023091
23092 /* behave like "dict" was used */
23093 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023094 }
23095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023097 STRCPY(fp->uf_name, name);
23098 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023100 fp->uf_args = newargs;
23101 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023102#ifdef FEAT_PROFILE
23103 fp->uf_tml_count = NULL;
23104 fp->uf_tml_total = NULL;
23105 fp->uf_tml_self = NULL;
23106 fp->uf_profiling = FALSE;
23107 if (prof_def_func())
23108 func_do_profile(fp);
23109#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023110 fp->uf_varargs = varargs;
23111 fp->uf_flags = flags;
23112 fp->uf_calls = 0;
23113 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023114 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
23116erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 ga_clear_strings(&newargs);
23118 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119ret_free:
23120 vim_free(skip_until);
23121 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023124 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125}
23126
23127/*
23128 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023129 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023131 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023132 * TFN_INT: internal function name OK
23133 * TFN_QUIET: be quiet
23134 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 * Advances "pp" to just after the function name (if no error).
23136 */
23137 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 char_u **pp;
23140 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023141 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023144 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 char_u *start;
23146 char_u *end;
23147 int lead;
23148 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023150 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023151
23152 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023153 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023155
23156 /* Check for hard coded <SNR>: already translated function ID (from a user
23157 * command). */
23158 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23159 && (*pp)[2] == (int)KE_SNR)
23160 {
23161 *pp += 3;
23162 len = get_id_len(pp) + 3;
23163 return vim_strnsave(start, len);
23164 }
23165
23166 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23167 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023169 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023171
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023172 /* Note that TFN_ flags use the same values as GLV_ flags. */
23173 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023174 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023175 if (end == start)
23176 {
23177 if (!skip)
23178 EMSG(_("E129: Function name required"));
23179 goto theend;
23180 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023181 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023182 {
23183 /*
23184 * Report an invalid expression in braces, unless the expression
23185 * evaluation has been cancelled due to an aborting error, an
23186 * interrupt, or an exception.
23187 */
23188 if (!aborting())
23189 {
23190 if (end != NULL)
23191 EMSG2(_(e_invarg2), start);
23192 }
23193 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023194 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023195 goto theend;
23196 }
23197
23198 if (lv.ll_tv != NULL)
23199 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023200 if (fdp != NULL)
23201 {
23202 fdp->fd_dict = lv.ll_dict;
23203 fdp->fd_newkey = lv.ll_newkey;
23204 lv.ll_newkey = NULL;
23205 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023206 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023207 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23208 {
23209 name = vim_strsave(lv.ll_tv->vval.v_string);
23210 *pp = end;
23211 }
23212 else
23213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023214 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23215 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023216 EMSG(_(e_funcref));
23217 else
23218 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023219 name = NULL;
23220 }
23221 goto theend;
23222 }
23223
23224 if (lv.ll_name == NULL)
23225 {
23226 /* Error found, but continue after the function name. */
23227 *pp = end;
23228 goto theend;
23229 }
23230
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023231 /* Check if the name is a Funcref. If so, use the value. */
23232 if (lv.ll_exp_name != NULL)
23233 {
23234 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023235 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023236 if (name == lv.ll_exp_name)
23237 name = NULL;
23238 }
23239 else
23240 {
23241 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023242 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023243 if (name == *pp)
23244 name = NULL;
23245 }
23246 if (name != NULL)
23247 {
23248 name = vim_strsave(name);
23249 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023250 if (STRNCMP(name, "<SNR>", 5) == 0)
23251 {
23252 /* Change "<SNR>" to the byte sequence. */
23253 name[0] = K_SPECIAL;
23254 name[1] = KS_EXTRA;
23255 name[2] = (int)KE_SNR;
23256 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23257 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023258 goto theend;
23259 }
23260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023261 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023262 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023263 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023264 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23265 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23266 {
23267 /* When there was "s:" already or the name expanded to get a
23268 * leading "s:" then remove it. */
23269 lv.ll_name += 2;
23270 len -= 2;
23271 lead = 2;
23272 }
23273 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023274 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023275 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023276 /* skip over "s:" and "g:" */
23277 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 lv.ll_name += 2;
23279 len = (int)(end - lv.ll_name);
23280 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023281
23282 /*
23283 * Copy the function name to allocated memory.
23284 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23285 * Accept <SNR>123_name() outside a script.
23286 */
23287 if (skip)
23288 lead = 0; /* do nothing */
23289 else if (lead > 0)
23290 {
23291 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023292 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23293 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023294 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023295 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023296 if (current_SID <= 0)
23297 {
23298 EMSG(_(e_usingsid));
23299 goto theend;
23300 }
23301 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23302 lead += (int)STRLEN(sid_buf);
23303 }
23304 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023305 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023306 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023307 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023308 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023309 goto theend;
23310 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023311 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023312 {
23313 char_u *cp = vim_strchr(lv.ll_name, ':');
23314
23315 if (cp != NULL && cp < end)
23316 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023317 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023318 goto theend;
23319 }
23320 }
23321
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023322 name = alloc((unsigned)(len + lead + 1));
23323 if (name != NULL)
23324 {
23325 if (lead > 0)
23326 {
23327 name[0] = K_SPECIAL;
23328 name[1] = KS_EXTRA;
23329 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023330 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023331 STRCPY(name + 3, sid_buf);
23332 }
23333 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023334 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023335 }
23336 *pp = end;
23337
23338theend:
23339 clear_lval(&lv);
23340 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341}
23342
23343/*
23344 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23345 * Return 2 if "p" starts with "s:".
23346 * Return 0 otherwise.
23347 */
23348 static int
23349eval_fname_script(p)
23350 char_u *p;
23351{
23352 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23353 || STRNICMP(p + 1, "SNR>", 4) == 0))
23354 return 5;
23355 if (p[0] == 's' && p[1] == ':')
23356 return 2;
23357 return 0;
23358}
23359
23360/*
23361 * Return TRUE if "p" starts with "<SID>" or "s:".
23362 * Only works if eval_fname_script() returned non-zero for "p"!
23363 */
23364 static int
23365eval_fname_sid(p)
23366 char_u *p;
23367{
23368 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23369}
23370
23371/*
23372 * List the head of the function: "name(arg1, arg2)".
23373 */
23374 static void
23375list_func_head(fp, indent)
23376 ufunc_T *fp;
23377 int indent;
23378{
23379 int j;
23380
23381 msg_start();
23382 if (indent)
23383 MSG_PUTS(" ");
23384 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023385 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 {
23387 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023388 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 }
23390 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023391 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023393 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 {
23395 if (j)
23396 MSG_PUTS(", ");
23397 msg_puts(FUNCARG(fp, j));
23398 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023399 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 {
23401 if (j)
23402 MSG_PUTS(", ");
23403 MSG_PUTS("...");
23404 }
23405 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023406 if (fp->uf_flags & FC_ABORT)
23407 MSG_PUTS(" abort");
23408 if (fp->uf_flags & FC_RANGE)
23409 MSG_PUTS(" range");
23410 if (fp->uf_flags & FC_DICT)
23411 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023412 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023413 if (p_verbose > 0)
23414 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415}
23416
23417/*
23418 * Find a function by name, return pointer to it in ufuncs.
23419 * Return NULL for unknown function.
23420 */
23421 static ufunc_T *
23422find_func(name)
23423 char_u *name;
23424{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023425 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 hi = hash_find(&func_hashtab, name);
23428 if (!HASHITEM_EMPTY(hi))
23429 return HI2UF(hi);
23430 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431}
23432
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023433#if defined(EXITFREE) || defined(PROTO)
23434 void
23435free_all_functions()
23436{
23437 hashitem_T *hi;
23438
23439 /* Need to start all over every time, because func_free() may change the
23440 * hash table. */
23441 while (func_hashtab.ht_used > 0)
23442 for (hi = func_hashtab.ht_array; ; ++hi)
23443 if (!HASHITEM_EMPTY(hi))
23444 {
23445 func_free(HI2UF(hi));
23446 break;
23447 }
23448}
23449#endif
23450
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023451 int
23452translated_function_exists(name)
23453 char_u *name;
23454{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023455 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023456 return find_internal_func(name) >= 0;
23457 return find_func(name) != NULL;
23458}
23459
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023460/*
23461 * Return TRUE if a function "name" exists.
23462 */
23463 static int
23464function_exists(name)
23465 char_u *name;
23466{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023467 char_u *nm = name;
23468 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023469 int n = FALSE;
23470
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023471 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23472 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023473 nm = skipwhite(nm);
23474
23475 /* Only accept "funcname", "funcname ", "funcname (..." and
23476 * "funcname(...", not "funcname!...". */
23477 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023478 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023479 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023480 return n;
23481}
23482
Bram Moolenaara1544c02013-05-30 12:35:52 +020023483 char_u *
23484get_expanded_name(name, check)
23485 char_u *name;
23486 int check;
23487{
23488 char_u *nm = name;
23489 char_u *p;
23490
23491 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23492
23493 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023494 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023495 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023496
Bram Moolenaara1544c02013-05-30 12:35:52 +020023497 vim_free(p);
23498 return NULL;
23499}
23500
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023501/*
23502 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023503 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23504 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023505 */
23506 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023507builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023508 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023509 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023510{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023511 char_u *p;
23512
23513 if (!ASCII_ISLOWER(name[0]))
23514 return FALSE;
23515 p = vim_strchr(name, AUTOLOAD_CHAR);
23516 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023517}
23518
Bram Moolenaar05159a02005-02-26 23:04:13 +000023519#if defined(FEAT_PROFILE) || defined(PROTO)
23520/*
23521 * Start profiling function "fp".
23522 */
23523 static void
23524func_do_profile(fp)
23525 ufunc_T *fp;
23526{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023527 int len = fp->uf_lines.ga_len;
23528
23529 if (len == 0)
23530 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023531 fp->uf_tm_count = 0;
23532 profile_zero(&fp->uf_tm_self);
23533 profile_zero(&fp->uf_tm_total);
23534 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023535 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023536 if (fp->uf_tml_total == NULL)
23537 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023538 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023539 if (fp->uf_tml_self == NULL)
23540 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023541 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023542 fp->uf_tml_idx = -1;
23543 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23544 || fp->uf_tml_self == NULL)
23545 return; /* out of memory */
23546
23547 fp->uf_profiling = TRUE;
23548}
23549
23550/*
23551 * Dump the profiling results for all functions in file "fd".
23552 */
23553 void
23554func_dump_profile(fd)
23555 FILE *fd;
23556{
23557 hashitem_T *hi;
23558 int todo;
23559 ufunc_T *fp;
23560 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023561 ufunc_T **sorttab;
23562 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023563
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023564 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023565 if (todo == 0)
23566 return; /* nothing to dump */
23567
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023568 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023569
Bram Moolenaar05159a02005-02-26 23:04:13 +000023570 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23571 {
23572 if (!HASHITEM_EMPTY(hi))
23573 {
23574 --todo;
23575 fp = HI2UF(hi);
23576 if (fp->uf_profiling)
23577 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023578 if (sorttab != NULL)
23579 sorttab[st_len++] = fp;
23580
Bram Moolenaar05159a02005-02-26 23:04:13 +000023581 if (fp->uf_name[0] == K_SPECIAL)
23582 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23583 else
23584 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23585 if (fp->uf_tm_count == 1)
23586 fprintf(fd, "Called 1 time\n");
23587 else
23588 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23589 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23590 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23591 fprintf(fd, "\n");
23592 fprintf(fd, "count total (s) self (s)\n");
23593
23594 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23595 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023596 if (FUNCLINE(fp, i) == NULL)
23597 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023598 prof_func_line(fd, fp->uf_tml_count[i],
23599 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023600 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23601 }
23602 fprintf(fd, "\n");
23603 }
23604 }
23605 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023606
23607 if (sorttab != NULL && st_len > 0)
23608 {
23609 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23610 prof_total_cmp);
23611 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23612 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23613 prof_self_cmp);
23614 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23615 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023616
23617 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023618}
Bram Moolenaar73830342005-02-28 22:48:19 +000023619
23620 static void
23621prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23622 FILE *fd;
23623 ufunc_T **sorttab;
23624 int st_len;
23625 char *title;
23626 int prefer_self; /* when equal print only self time */
23627{
23628 int i;
23629 ufunc_T *fp;
23630
23631 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23632 fprintf(fd, "count total (s) self (s) function\n");
23633 for (i = 0; i < 20 && i < st_len; ++i)
23634 {
23635 fp = sorttab[i];
23636 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23637 prefer_self);
23638 if (fp->uf_name[0] == K_SPECIAL)
23639 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23640 else
23641 fprintf(fd, " %s()\n", fp->uf_name);
23642 }
23643 fprintf(fd, "\n");
23644}
23645
23646/*
23647 * Print the count and times for one function or function line.
23648 */
23649 static void
23650prof_func_line(fd, count, total, self, prefer_self)
23651 FILE *fd;
23652 int count;
23653 proftime_T *total;
23654 proftime_T *self;
23655 int prefer_self; /* when equal print only self time */
23656{
23657 if (count > 0)
23658 {
23659 fprintf(fd, "%5d ", count);
23660 if (prefer_self && profile_equal(total, self))
23661 fprintf(fd, " ");
23662 else
23663 fprintf(fd, "%s ", profile_msg(total));
23664 if (!prefer_self && profile_equal(total, self))
23665 fprintf(fd, " ");
23666 else
23667 fprintf(fd, "%s ", profile_msg(self));
23668 }
23669 else
23670 fprintf(fd, " ");
23671}
23672
23673/*
23674 * Compare function for total time sorting.
23675 */
23676 static int
23677#ifdef __BORLANDC__
23678_RTLENTRYF
23679#endif
23680prof_total_cmp(s1, s2)
23681 const void *s1;
23682 const void *s2;
23683{
23684 ufunc_T *p1, *p2;
23685
23686 p1 = *(ufunc_T **)s1;
23687 p2 = *(ufunc_T **)s2;
23688 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23689}
23690
23691/*
23692 * Compare function for self time sorting.
23693 */
23694 static int
23695#ifdef __BORLANDC__
23696_RTLENTRYF
23697#endif
23698prof_self_cmp(s1, s2)
23699 const void *s1;
23700 const void *s2;
23701{
23702 ufunc_T *p1, *p2;
23703
23704 p1 = *(ufunc_T **)s1;
23705 p2 = *(ufunc_T **)s2;
23706 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23707}
23708
Bram Moolenaar05159a02005-02-26 23:04:13 +000023709#endif
23710
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023711/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023712 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023713 * Return TRUE if a package was loaded.
23714 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023715 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023716script_autoload(name, reload)
23717 char_u *name;
23718 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023719{
23720 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023721 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023722 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023723 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023724
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023725 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023726 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023727 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023728 return FALSE;
23729
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023730 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023731
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023732 /* Find the name in the list of previously loaded package names. Skip
23733 * "autoload/", it's always the same. */
23734 for (i = 0; i < ga_loaded.ga_len; ++i)
23735 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23736 break;
23737 if (!reload && i < ga_loaded.ga_len)
23738 ret = FALSE; /* was loaded already */
23739 else
23740 {
23741 /* Remember the name if it wasn't loaded already. */
23742 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23743 {
23744 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23745 tofree = NULL;
23746 }
23747
23748 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023749 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023750 ret = TRUE;
23751 }
23752
23753 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 return ret;
23755}
23756
23757/*
23758 * Return the autoload script name for a function or variable name.
23759 * Returns NULL when out of memory.
23760 */
23761 static char_u *
23762autoload_name(name)
23763 char_u *name;
23764{
23765 char_u *p;
23766 char_u *scriptname;
23767
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023768 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023769 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23770 if (scriptname == NULL)
23771 return FALSE;
23772 STRCPY(scriptname, "autoload/");
23773 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023774 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023775 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023776 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023777 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023778 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023779}
23780
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23782
23783/*
23784 * Function given to ExpandGeneric() to obtain the list of user defined
23785 * function names.
23786 */
23787 char_u *
23788get_user_func_name(xp, idx)
23789 expand_T *xp;
23790 int idx;
23791{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023792 static long_u done;
23793 static hashitem_T *hi;
23794 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795
23796 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023798 done = 0;
23799 hi = func_hashtab.ht_array;
23800 }
23801 if (done < func_hashtab.ht_used)
23802 {
23803 if (done++ > 0)
23804 ++hi;
23805 while (HASHITEM_EMPTY(hi))
23806 ++hi;
23807 fp = HI2UF(hi);
23808
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023809 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023810 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023812 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23813 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814
23815 cat_func_name(IObuff, fp);
23816 if (xp->xp_context != EXPAND_USER_FUNC)
23817 {
23818 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023819 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 STRCAT(IObuff, ")");
23821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 return IObuff;
23823 }
23824 return NULL;
23825}
23826
23827#endif /* FEAT_CMDL_COMPL */
23828
23829/*
23830 * Copy the function name of "fp" to buffer "buf".
23831 * "buf" must be able to hold the function name plus three bytes.
23832 * Takes care of script-local function names.
23833 */
23834 static void
23835cat_func_name(buf, fp)
23836 char_u *buf;
23837 ufunc_T *fp;
23838{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023839 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 {
23841 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023842 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 }
23844 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023845 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846}
23847
23848/*
23849 * ":delfunction {name}"
23850 */
23851 void
23852ex_delfunction(eap)
23853 exarg_T *eap;
23854{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023855 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 char_u *p;
23857 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023858 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859
23860 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023861 name = trans_function_name(&p, eap->skip, 0, &fudi);
23862 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023864 {
23865 if (fudi.fd_dict != NULL && !eap->skip)
23866 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 if (!ends_excmd(*skipwhite(p)))
23870 {
23871 vim_free(name);
23872 EMSG(_(e_trailing));
23873 return;
23874 }
23875 eap->nextcmd = check_nextcmd(p);
23876 if (eap->nextcmd != NULL)
23877 *p = NUL;
23878
23879 if (!eap->skip)
23880 fp = find_func(name);
23881 vim_free(name);
23882
23883 if (!eap->skip)
23884 {
23885 if (fp == NULL)
23886 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023887 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 return;
23889 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023890 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 {
23892 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23893 return;
23894 }
23895
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023896 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898 /* Delete the dict item that refers to the function, it will
23899 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023900 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902 else
23903 func_free(fp);
23904 }
23905}
23906
23907/*
23908 * Free a function and remove it from the list of functions.
23909 */
23910 static void
23911func_free(fp)
23912 ufunc_T *fp;
23913{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023914 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023915
23916 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023917 ga_clear_strings(&(fp->uf_args));
23918 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023919#ifdef FEAT_PROFILE
23920 vim_free(fp->uf_tml_count);
23921 vim_free(fp->uf_tml_total);
23922 vim_free(fp->uf_tml_self);
23923#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023924
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023925 /* remove the function from the function hashtable */
23926 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23927 if (HASHITEM_EMPTY(hi))
23928 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023929 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023930 hash_remove(&func_hashtab, hi);
23931
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023932 vim_free(fp);
23933}
23934
23935/*
23936 * Unreference a Function: decrement the reference count and free it when it
23937 * becomes zero. Only for numbered functions.
23938 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023939 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940func_unref(name)
23941 char_u *name;
23942{
23943 ufunc_T *fp;
23944
23945 if (name != NULL && isdigit(*name))
23946 {
23947 fp = find_func(name);
23948 if (fp == NULL)
23949 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023951 {
23952 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023953 * when "uf_calls" becomes zero. */
23954 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023955 func_free(fp);
23956 }
23957 }
23958}
23959
23960/*
23961 * Count a reference to a Function.
23962 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023963 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023964func_ref(name)
23965 char_u *name;
23966{
23967 ufunc_T *fp;
23968
23969 if (name != NULL && isdigit(*name))
23970 {
23971 fp = find_func(name);
23972 if (fp == NULL)
23973 EMSG2(_(e_intern2), "func_ref()");
23974 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023975 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 }
23977}
23978
23979/*
23980 * Call a user function.
23981 */
23982 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023983call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 ufunc_T *fp; /* pointer to function */
23985 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023986 typval_T *argvars; /* arguments */
23987 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 linenr_T firstline; /* first line of range */
23989 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023990 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991{
Bram Moolenaar33570922005-01-25 22:26:29 +000023992 char_u *save_sourcing_name;
23993 linenr_T save_sourcing_lnum;
23994 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023995 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023996 int save_did_emsg;
23997 static int depth = 0;
23998 dictitem_T *v;
23999 int fixvar_idx = 0; /* index in fixvar[] */
24000 int i;
24001 int ai;
24002 char_u numbuf[NUMBUFLEN];
24003 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024004 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024005#ifdef FEAT_PROFILE
24006 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024007 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009
24010 /* If depth of calling is getting too high, don't execute the function */
24011 if (depth >= p_mfd)
24012 {
24013 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024014 rettv->v_type = VAR_NUMBER;
24015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 return;
24017 }
24018 ++depth;
24019
24020 line_breakcheck(); /* check for CTRL-C hit */
24021
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024022 fc = (funccall_T *)alloc(sizeof(funccall_T));
24023 fc->caller = current_funccal;
24024 current_funccal = fc;
24025 fc->func = fp;
24026 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024027 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024028 fc->linenr = 0;
24029 fc->returned = FALSE;
24030 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024032 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24033 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034
Bram Moolenaar33570922005-01-25 22:26:29 +000024035 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024036 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024037 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24038 * each argument variable and saves a lot of time.
24039 */
24040 /*
24041 * Init l: variables.
24042 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024043 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024044 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024045 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024046 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24047 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024048 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024049 name = v->di_key;
24050 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024051 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024052 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024053 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024054 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024055 v->di_tv.vval.v_dict = selfdict;
24056 ++selfdict->dv_refcount;
24057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024058
Bram Moolenaar33570922005-01-25 22:26:29 +000024059 /*
24060 * Init a: variables.
24061 * Set a:0 to "argcount".
24062 * Set a:000 to a list with room for the "..." arguments.
24063 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024064 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024065 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024067 /* Use "name" to avoid a warning from some compiler that checks the
24068 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024069 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024070 name = v->di_key;
24071 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024072 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024073 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024074 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024075 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024076 v->di_tv.vval.v_list = &fc->l_varlist;
24077 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24078 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24079 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024080
24081 /*
24082 * Set a:firstline to "firstline" and a:lastline to "lastline".
24083 * Set a:name to named arguments.
24084 * Set a:N to the "..." arguments.
24085 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024086 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024087 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024088 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 (varnumber_T)lastline);
24090 for (i = 0; i < argcount; ++i)
24091 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024092 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 if (ai < 0)
24094 /* named argument a:name */
24095 name = FUNCARG(fp, i);
24096 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024097 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 /* "..." argument a:1, a:2, etc. */
24099 sprintf((char *)numbuf, "%d", ai + 1);
24100 name = numbuf;
24101 }
24102 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24103 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024104 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024105 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24106 }
24107 else
24108 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024109 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24110 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024111 if (v == NULL)
24112 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024113 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024114 }
24115 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024116 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024118 /* Note: the values are copied directly to avoid alloc/free.
24119 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024120 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024121 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024122
24123 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24124 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024125 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24126 fc->l_listitems[ai].li_tv = argvars[i];
24127 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024128 }
24129 }
24130
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 /* Don't redraw while executing the function. */
24132 ++RedrawingDisabled;
24133 save_sourcing_name = sourcing_name;
24134 save_sourcing_lnum = sourcing_lnum;
24135 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024136 /* need space for function name + ("function " + 3) or "[number]" */
24137 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24138 + STRLEN(fp->uf_name) + 20;
24139 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 if (sourcing_name != NULL)
24141 {
24142 if (save_sourcing_name != NULL
24143 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024144 sprintf((char *)sourcing_name, "%s[%d]..",
24145 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 else
24147 STRCPY(sourcing_name, "function ");
24148 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24149
24150 if (p_verbose >= 12)
24151 {
24152 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024153 verbose_enter_scroll();
24154
Bram Moolenaar555b2802005-05-19 21:08:39 +000024155 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 if (p_verbose >= 14)
24157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024159 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024160 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024161 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162
24163 msg_puts((char_u *)"(");
24164 for (i = 0; i < argcount; ++i)
24165 {
24166 if (i > 0)
24167 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024168 if (argvars[i].v_type == VAR_NUMBER)
24169 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 else
24171 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024172 /* Do not want errors such as E724 here. */
24173 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024174 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024175 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024176 if (s != NULL)
24177 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024178 if (vim_strsize(s) > MSG_BUF_CLEN)
24179 {
24180 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24181 s = buf;
24182 }
24183 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024184 vim_free(tofree);
24185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 }
24187 }
24188 msg_puts((char_u *)")");
24189 }
24190 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024191
24192 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 --no_wait_return;
24194 }
24195 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024196#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024197 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024198 {
24199 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24200 func_do_profile(fp);
24201 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024202 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024203 {
24204 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024205 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024206 profile_zero(&fp->uf_tm_children);
24207 }
24208 script_prof_save(&wait_start);
24209 }
24210#endif
24211
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024213 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 save_did_emsg = did_emsg;
24215 did_emsg = FALSE;
24216
24217 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024218 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24220
24221 --RedrawingDisabled;
24222
24223 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024224 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024226 clear_tv(rettv);
24227 rettv->v_type = VAR_NUMBER;
24228 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 }
24230
Bram Moolenaar05159a02005-02-26 23:04:13 +000024231#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024232 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024233 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024234 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024235 profile_end(&call_start);
24236 profile_sub_wait(&wait_start, &call_start);
24237 profile_add(&fp->uf_tm_total, &call_start);
24238 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024239 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024240 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024241 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24242 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024243 }
24244 }
24245#endif
24246
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 /* when being verbose, mention the return value */
24248 if (p_verbose >= 12)
24249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024251 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024254 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024255 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024256 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024257 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024260 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024261 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024262 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024263 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024264
Bram Moolenaar555b2802005-05-19 21:08:39 +000024265 /* The value may be very long. Skip the middle part, so that we
24266 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024267 * truncate it at the end. Don't want errors such as E724 here. */
24268 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024269 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024270 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024271 if (s != NULL)
24272 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024273 if (vim_strsize(s) > MSG_BUF_CLEN)
24274 {
24275 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24276 s = buf;
24277 }
24278 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024279 vim_free(tofree);
24280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 }
24282 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024283
24284 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285 --no_wait_return;
24286 }
24287
24288 vim_free(sourcing_name);
24289 sourcing_name = save_sourcing_name;
24290 sourcing_lnum = save_sourcing_lnum;
24291 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024292#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024293 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 script_prof_restore(&wait_start);
24295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296
24297 if (p_verbose >= 12 && sourcing_name != NULL)
24298 {
24299 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024300 verbose_enter_scroll();
24301
Bram Moolenaar555b2802005-05-19 21:08:39 +000024302 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024304
24305 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 --no_wait_return;
24307 }
24308
24309 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024310 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024312
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024313 /* If the a:000 list and the l: and a: dicts are not referenced we can
24314 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024315 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24316 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24317 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24318 {
24319 free_funccal(fc, FALSE);
24320 }
24321 else
24322 {
24323 hashitem_T *hi;
24324 listitem_T *li;
24325 int todo;
24326
24327 /* "fc" is still in use. This can happen when returning "a:000" or
24328 * assigning "l:" to a global variable.
24329 * Link "fc" in the list for garbage collection later. */
24330 fc->caller = previous_funccal;
24331 previous_funccal = fc;
24332
24333 /* Make a copy of the a: variables, since we didn't do that above. */
24334 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24335 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24336 {
24337 if (!HASHITEM_EMPTY(hi))
24338 {
24339 --todo;
24340 v = HI2DI(hi);
24341 copy_tv(&v->di_tv, &v->di_tv);
24342 }
24343 }
24344
24345 /* Make a copy of the a:000 items, since we didn't do that above. */
24346 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24347 copy_tv(&li->li_tv, &li->li_tv);
24348 }
24349}
24350
24351/*
24352 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024353 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024354 */
24355 static int
24356can_free_funccal(fc, copyID)
24357 funccall_T *fc;
24358 int copyID;
24359{
24360 return (fc->l_varlist.lv_copyID != copyID
24361 && fc->l_vars.dv_copyID != copyID
24362 && fc->l_avars.dv_copyID != copyID);
24363}
24364
24365/*
24366 * Free "fc" and what it contains.
24367 */
24368 static void
24369free_funccal(fc, free_val)
24370 funccall_T *fc;
24371 int free_val; /* a: vars were allocated */
24372{
24373 listitem_T *li;
24374
24375 /* The a: variables typevals may not have been allocated, only free the
24376 * allocated variables. */
24377 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24378
24379 /* free all l: variables */
24380 vars_clear(&fc->l_vars.dv_hashtab);
24381
24382 /* Free the a:000 variables if they were allocated. */
24383 if (free_val)
24384 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24385 clear_tv(&li->li_tv);
24386
24387 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388}
24389
24390/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024391 * Add a number variable "name" to dict "dp" with value "nr".
24392 */
24393 static void
24394add_nr_var(dp, v, name, nr)
24395 dict_T *dp;
24396 dictitem_T *v;
24397 char *name;
24398 varnumber_T nr;
24399{
24400 STRCPY(v->di_key, name);
24401 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24402 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24403 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024404 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024405 v->di_tv.vval.v_number = nr;
24406}
24407
24408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 * ":return [expr]"
24410 */
24411 void
24412ex_return(eap)
24413 exarg_T *eap;
24414{
24415 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 int returning = FALSE;
24418
24419 if (current_funccal == NULL)
24420 {
24421 EMSG(_("E133: :return not inside a function"));
24422 return;
24423 }
24424
24425 if (eap->skip)
24426 ++emsg_skip;
24427
24428 eap->nextcmd = NULL;
24429 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024430 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 {
24432 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024433 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 }
24437 /* It's safer to return also on error. */
24438 else if (!eap->skip)
24439 {
24440 /*
24441 * Return unless the expression evaluation has been cancelled due to an
24442 * aborting error, an interrupt, or an exception.
24443 */
24444 if (!aborting())
24445 returning = do_return(eap, FALSE, TRUE, NULL);
24446 }
24447
24448 /* When skipping or the return gets pending, advance to the next command
24449 * in this line (!returning). Otherwise, ignore the rest of the line.
24450 * Following lines will be ignored by get_func_line(). */
24451 if (returning)
24452 eap->nextcmd = NULL;
24453 else if (eap->nextcmd == NULL) /* no argument */
24454 eap->nextcmd = check_nextcmd(arg);
24455
24456 if (eap->skip)
24457 --emsg_skip;
24458}
24459
24460/*
24461 * Return from a function. Possibly makes the return pending. Also called
24462 * for a pending return at the ":endtry" or after returning from an extra
24463 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024464 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024465 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 * FALSE when the return gets pending.
24467 */
24468 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024469do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 exarg_T *eap;
24471 int reanimate;
24472 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024473 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474{
24475 int idx;
24476 struct condstack *cstack = eap->cstack;
24477
24478 if (reanimate)
24479 /* Undo the return. */
24480 current_funccal->returned = FALSE;
24481
24482 /*
24483 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24484 * not in its finally clause (which then is to be executed next) is found.
24485 * In this case, make the ":return" pending for execution at the ":endtry".
24486 * Otherwise, return normally.
24487 */
24488 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24489 if (idx >= 0)
24490 {
24491 cstack->cs_pending[idx] = CSTP_RETURN;
24492
24493 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024494 /* A pending return again gets pending. "rettv" points to an
24495 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024497 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 else
24499 {
24500 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024501 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024503 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024505 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 {
24507 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024508 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024509 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 else
24511 EMSG(_(e_outofmem));
24512 }
24513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024514 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515
24516 if (reanimate)
24517 {
24518 /* The pending return value could be overwritten by a ":return"
24519 * without argument in a finally clause; reset the default
24520 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024521 current_funccal->rettv->v_type = VAR_NUMBER;
24522 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 }
24524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024525 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526 }
24527 else
24528 {
24529 current_funccal->returned = TRUE;
24530
24531 /* If the return is carried out now, store the return value. For
24532 * a return immediately after reanimation, the value is already
24533 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024534 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024536 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024537 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024539 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 }
24541 }
24542
24543 return idx < 0;
24544}
24545
24546/*
24547 * Free the variable with a pending return value.
24548 */
24549 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024550discard_pending_return(rettv)
24551 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552{
Bram Moolenaar33570922005-01-25 22:26:29 +000024553 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554}
24555
24556/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024557 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 * is an allocated string. Used by report_pending() for verbose messages.
24559 */
24560 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024561get_return_cmd(rettv)
24562 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024564 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024565 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024566 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024568 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024569 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024570 if (s == NULL)
24571 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572
24573 STRCPY(IObuff, ":return ");
24574 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24575 if (STRLEN(s) + 8 >= IOSIZE)
24576 STRCPY(IObuff + IOSIZE - 4, "...");
24577 vim_free(tofree);
24578 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579}
24580
24581/*
24582 * Get next function line.
24583 * Called by do_cmdline() to get the next line.
24584 * Returns allocated string, or NULL for end of function.
24585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 char_u *
24587get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024588 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024590 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591{
Bram Moolenaar33570922005-01-25 22:26:29 +000024592 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024593 ufunc_T *fp = fcp->func;
24594 char_u *retval;
24595 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596
24597 /* If breakpoints have been added/deleted need to check for it. */
24598 if (fcp->dbg_tick != debug_tick)
24599 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024600 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 sourcing_lnum);
24602 fcp->dbg_tick = debug_tick;
24603 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024604#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024605 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606 func_line_end(cookie);
24607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608
Bram Moolenaar05159a02005-02-26 23:04:13 +000024609 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024610 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24611 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 retval = NULL;
24613 else
24614 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024615 /* Skip NULL lines (continuation lines). */
24616 while (fcp->linenr < gap->ga_len
24617 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24618 ++fcp->linenr;
24619 if (fcp->linenr >= gap->ga_len)
24620 retval = NULL;
24621 else
24622 {
24623 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24624 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024625#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024626 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024627 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024628#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 }
24631
24632 /* Did we encounter a breakpoint? */
24633 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24634 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024635 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024637 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 sourcing_lnum);
24639 fcp->dbg_tick = debug_tick;
24640 }
24641
24642 return retval;
24643}
24644
Bram Moolenaar05159a02005-02-26 23:04:13 +000024645#if defined(FEAT_PROFILE) || defined(PROTO)
24646/*
24647 * Called when starting to read a function line.
24648 * "sourcing_lnum" must be correct!
24649 * When skipping lines it may not actually be executed, but we won't find out
24650 * until later and we need to store the time now.
24651 */
24652 void
24653func_line_start(cookie)
24654 void *cookie;
24655{
24656 funccall_T *fcp = (funccall_T *)cookie;
24657 ufunc_T *fp = fcp->func;
24658
24659 if (fp->uf_profiling && sourcing_lnum >= 1
24660 && sourcing_lnum <= fp->uf_lines.ga_len)
24661 {
24662 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024663 /* Skip continuation lines. */
24664 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24665 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024666 fp->uf_tml_execed = FALSE;
24667 profile_start(&fp->uf_tml_start);
24668 profile_zero(&fp->uf_tml_children);
24669 profile_get_wait(&fp->uf_tml_wait);
24670 }
24671}
24672
24673/*
24674 * Called when actually executing a function line.
24675 */
24676 void
24677func_line_exec(cookie)
24678 void *cookie;
24679{
24680 funccall_T *fcp = (funccall_T *)cookie;
24681 ufunc_T *fp = fcp->func;
24682
24683 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24684 fp->uf_tml_execed = TRUE;
24685}
24686
24687/*
24688 * Called when done with a function line.
24689 */
24690 void
24691func_line_end(cookie)
24692 void *cookie;
24693{
24694 funccall_T *fcp = (funccall_T *)cookie;
24695 ufunc_T *fp = fcp->func;
24696
24697 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24698 {
24699 if (fp->uf_tml_execed)
24700 {
24701 ++fp->uf_tml_count[fp->uf_tml_idx];
24702 profile_end(&fp->uf_tml_start);
24703 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024704 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024705 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24706 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707 }
24708 fp->uf_tml_idx = -1;
24709 }
24710}
24711#endif
24712
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713/*
24714 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024715 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 */
24717 int
24718func_has_ended(cookie)
24719 void *cookie;
24720{
Bram Moolenaar33570922005-01-25 22:26:29 +000024721 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024722
24723 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24724 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024725 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726 || fcp->returned);
24727}
24728
24729/*
24730 * return TRUE if cookie indicates a function which "abort"s on errors.
24731 */
24732 int
24733func_has_abort(cookie)
24734 void *cookie;
24735{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024736 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737}
24738
24739#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24740typedef enum
24741{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024742 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24743 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24744 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745} var_flavour_T;
24746
24747static var_flavour_T var_flavour __ARGS((char_u *varname));
24748
24749 static var_flavour_T
24750var_flavour(varname)
24751 char_u *varname;
24752{
24753 char_u *p = varname;
24754
24755 if (ASCII_ISUPPER(*p))
24756 {
24757 while (*(++p))
24758 if (ASCII_ISLOWER(*p))
24759 return VAR_FLAVOUR_SESSION;
24760 return VAR_FLAVOUR_VIMINFO;
24761 }
24762 else
24763 return VAR_FLAVOUR_DEFAULT;
24764}
24765#endif
24766
24767#if defined(FEAT_VIMINFO) || defined(PROTO)
24768/*
24769 * Restore global vars that start with a capital from the viminfo file
24770 */
24771 int
24772read_viminfo_varlist(virp, writing)
24773 vir_T *virp;
24774 int writing;
24775{
24776 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024777 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779
24780 if (!writing && (find_viminfo_parameter('!') != NULL))
24781 {
24782 tab = vim_strchr(virp->vir_line + 1, '\t');
24783 if (tab != NULL)
24784 {
24785 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024786 switch (*tab)
24787 {
24788 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024789#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024790 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024791#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024792 case 'D': type = VAR_DICT; break;
24793 case 'L': type = VAR_LIST; break;
24794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795
24796 tab = vim_strchr(tab, '\t');
24797 if (tab != NULL)
24798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024799 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024800 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024801 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024803#ifdef FEAT_FLOAT
24804 else if (type == VAR_FLOAT)
24805 (void)string2float(tab + 1, &tv.vval.v_float);
24806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024808 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024809 if (type == VAR_DICT || type == VAR_LIST)
24810 {
24811 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24812
24813 if (etv == NULL)
24814 /* Failed to parse back the dict or list, use it as a
24815 * string. */
24816 tv.v_type = VAR_STRING;
24817 else
24818 {
24819 vim_free(tv.vval.v_string);
24820 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024821 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024822 }
24823 }
24824
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024825 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024826
24827 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024828 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024829 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 }
24832 }
24833 }
24834
24835 return viminfo_readline(virp);
24836}
24837
24838/*
24839 * Write global vars that start with a capital to the viminfo file
24840 */
24841 void
24842write_viminfo_varlist(fp)
24843 FILE *fp;
24844{
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 hashitem_T *hi;
24846 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024847 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024848 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024849 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024851 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852
24853 if (find_viminfo_parameter('!') == NULL)
24854 return;
24855
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024856 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024858 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024859 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024861 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024863 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024864 this_var = HI2DI(hi);
24865 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024866 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024867 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024868 {
24869 case VAR_STRING: s = "STR"; break;
24870 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024871#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024872 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024873#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024874 case VAR_DICT: s = "DIC"; break;
24875 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024876 default: continue;
24877 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024879 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024880 if (p != NULL)
24881 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024882 vim_free(tofree);
24883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 }
24885 }
24886}
24887#endif
24888
24889#if defined(FEAT_SESSION) || defined(PROTO)
24890 int
24891store_session_globals(fd)
24892 FILE *fd;
24893{
Bram Moolenaar33570922005-01-25 22:26:29 +000024894 hashitem_T *hi;
24895 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024896 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 char_u *p, *t;
24898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024899 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024900 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024902 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024904 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024905 this_var = HI2DI(hi);
24906 if ((this_var->di_tv.v_type == VAR_NUMBER
24907 || this_var->di_tv.v_type == VAR_STRING)
24908 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024909 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024910 /* Escape special characters with a backslash. Turn a LF and
24911 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024912 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024913 (char_u *)"\\\"\n\r");
24914 if (p == NULL) /* out of memory */
24915 break;
24916 for (t = p; *t != NUL; ++t)
24917 if (*t == '\n')
24918 *t = 'n';
24919 else if (*t == '\r')
24920 *t = 'r';
24921 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024922 this_var->di_key,
24923 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24924 : ' ',
24925 p,
24926 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24927 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024928 || put_eol(fd) == FAIL)
24929 {
24930 vim_free(p);
24931 return FAIL;
24932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 vim_free(p);
24934 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024935#ifdef FEAT_FLOAT
24936 else if (this_var->di_tv.v_type == VAR_FLOAT
24937 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24938 {
24939 float_T f = this_var->di_tv.vval.v_float;
24940 int sign = ' ';
24941
24942 if (f < 0)
24943 {
24944 f = -f;
24945 sign = '-';
24946 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024947 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024948 this_var->di_key, sign, f) < 0)
24949 || put_eol(fd) == FAIL)
24950 return FAIL;
24951 }
24952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 }
24954 }
24955 return OK;
24956}
24957#endif
24958
Bram Moolenaar661b1822005-07-28 22:36:45 +000024959/*
24960 * Display script name where an item was last set.
24961 * Should only be invoked when 'verbose' is non-zero.
24962 */
24963 void
24964last_set_msg(scriptID)
24965 scid_T scriptID;
24966{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024967 char_u *p;
24968
Bram Moolenaar661b1822005-07-28 22:36:45 +000024969 if (scriptID != 0)
24970 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024971 p = home_replace_save(NULL, get_scriptname(scriptID));
24972 if (p != NULL)
24973 {
24974 verbose_enter();
24975 MSG_PUTS(_("\n\tLast set from "));
24976 MSG_PUTS(p);
24977 vim_free(p);
24978 verbose_leave();
24979 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024980 }
24981}
24982
Bram Moolenaard812df62008-11-09 12:46:09 +000024983/*
24984 * List v:oldfiles in a nice way.
24985 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024986 void
24987ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024988 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024989{
24990 list_T *l = vimvars[VV_OLDFILES].vv_list;
24991 listitem_T *li;
24992 int nr = 0;
24993
24994 if (l == NULL)
24995 msg((char_u *)_("No old files"));
24996 else
24997 {
24998 msg_start();
24999 msg_scroll = TRUE;
25000 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25001 {
25002 msg_outnum((long)++nr);
25003 MSG_PUTS(": ");
25004 msg_outtrans(get_tv_string(&li->li_tv));
25005 msg_putchar('\n');
25006 out_flush(); /* output one line at a time */
25007 ui_breakcheck();
25008 }
25009 /* Assume "got_int" was set to truncate the listing. */
25010 got_int = FALSE;
25011
25012#ifdef FEAT_BROWSE_CMD
25013 if (cmdmod.browse)
25014 {
25015 quit_more = FALSE;
25016 nr = prompt_for_number(FALSE);
25017 msg_starthere();
25018 if (nr > 0)
25019 {
25020 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25021 (long)nr);
25022
25023 if (p != NULL)
25024 {
25025 p = expand_env_save(p);
25026 eap->arg = p;
25027 eap->cmdidx = CMD_edit;
25028 cmdmod.browse = FALSE;
25029 do_exedit(eap, NULL);
25030 vim_free(p);
25031 }
25032 }
25033 }
25034#endif
25035 }
25036}
25037
Bram Moolenaar53744302015-07-17 17:38:22 +020025038/* reset v:option_new, v:option_old and v:option_type */
25039 void
25040reset_v_option_vars()
25041{
25042 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25043 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25044 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25045}
25046
25047
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048#endif /* FEAT_EVAL */
25049
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025051#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052
25053#ifdef WIN3264
25054/*
25055 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25056 */
25057static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25058static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25059static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25060
25061/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025062 * Get the short path (8.3) for the filename in "fnamep".
25063 * Only works for a valid file name.
25064 * When the path gets longer "fnamep" is changed and the allocated buffer
25065 * is put in "bufp".
25066 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25067 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 */
25069 static int
25070get_short_pathname(fnamep, bufp, fnamelen)
25071 char_u **fnamep;
25072 char_u **bufp;
25073 int *fnamelen;
25074{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025075 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 char_u *newbuf;
25077
25078 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 l = GetShortPathName(*fnamep, *fnamep, len);
25080 if (l > len - 1)
25081 {
25082 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025083 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 newbuf = vim_strnsave(*fnamep, l);
25085 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087
25088 vim_free(*bufp);
25089 *fnamep = *bufp = newbuf;
25090
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025091 /* Really should always succeed, as the buffer is big enough. */
25092 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 }
25094
25095 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025096 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097}
25098
25099/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025100 * Get the short path (8.3) for the filename in "fname". The converted
25101 * path is returned in "bufp".
25102 *
25103 * Some of the directories specified in "fname" may not exist. This function
25104 * will shorten the existing directories at the beginning of the path and then
25105 * append the remaining non-existing path.
25106 *
25107 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025108 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025109 * bufp - Pointer to an allocated buffer for the filename.
25110 * fnamelen - Length of the filename pointed to by fname
25111 *
25112 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 */
25114 static int
25115shortpath_for_invalid_fname(fname, bufp, fnamelen)
25116 char_u **fname;
25117 char_u **bufp;
25118 int *fnamelen;
25119{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025120 char_u *short_fname, *save_fname, *pbuf_unused;
25121 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025123 int old_len, len;
25124 int new_len, sfx_len;
25125 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126
25127 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025128 old_len = *fnamelen;
25129 save_fname = vim_strnsave(*fname, old_len);
25130 pbuf_unused = NULL;
25131 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025133 endp = save_fname + old_len - 1; /* Find the end of the copy */
25134 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025136 /*
25137 * Try shortening the supplied path till it succeeds by removing one
25138 * directory at a time from the tail of the path.
25139 */
25140 len = 0;
25141 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025143 /* go back one path-separator */
25144 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25145 --endp;
25146 if (endp <= save_fname)
25147 break; /* processed the complete path */
25148
25149 /*
25150 * Replace the path separator with a NUL and try to shorten the
25151 * resulting path.
25152 */
25153 ch = *endp;
25154 *endp = 0;
25155 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025156 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025157 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25158 {
25159 retval = FAIL;
25160 goto theend;
25161 }
25162 *endp = ch; /* preserve the string */
25163
25164 if (len > 0)
25165 break; /* successfully shortened the path */
25166
25167 /* failed to shorten the path. Skip the path separator */
25168 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 }
25170
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025171 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025173 /*
25174 * Succeeded in shortening the path. Now concatenate the shortened
25175 * path with the remaining path at the tail.
25176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025178 /* Compute the length of the new path. */
25179 sfx_len = (int)(save_endp - endp) + 1;
25180 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025184 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025186 /* There is not enough space in the currently allocated string,
25187 * copy it to a buffer big enough. */
25188 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025190 {
25191 retval = FAIL;
25192 goto theend;
25193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 }
25195 else
25196 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025197 /* Transfer short_fname to the main buffer (it's big enough),
25198 * unless get_short_pathname() did its work in-place. */
25199 *fname = *bufp = save_fname;
25200 if (short_fname != save_fname)
25201 vim_strncpy(save_fname, short_fname, len);
25202 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025204
25205 /* concat the not-shortened part of the path */
25206 vim_strncpy(*fname + len, endp, sfx_len);
25207 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025209
25210theend:
25211 vim_free(pbuf_unused);
25212 vim_free(save_fname);
25213
25214 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215}
25216
25217/*
25218 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025219 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 */
25221 static int
25222shortpath_for_partial(fnamep, bufp, fnamelen)
25223 char_u **fnamep;
25224 char_u **bufp;
25225 int *fnamelen;
25226{
25227 int sepcount, len, tflen;
25228 char_u *p;
25229 char_u *pbuf, *tfname;
25230 int hasTilde;
25231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025232 /* Count up the path separators from the RHS.. so we know which part
25233 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025235 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 if (vim_ispathsep(*p))
25237 ++sepcount;
25238
25239 /* Need full path first (use expand_env() to remove a "~/") */
25240 hasTilde = (**fnamep == '~');
25241 if (hasTilde)
25242 pbuf = tfname = expand_env_save(*fnamep);
25243 else
25244 pbuf = tfname = FullName_save(*fnamep, FALSE);
25245
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025246 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025248 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250
25251 if (len == 0)
25252 {
25253 /* Don't have a valid filename, so shorten the rest of the
25254 * path if we can. This CAN give us invalid 8.3 filenames, but
25255 * there's not a lot of point in guessing what it might be.
25256 */
25257 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025258 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 }
25261
25262 /* Count the paths backward to find the beginning of the desired string. */
25263 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025264 {
25265#ifdef FEAT_MBYTE
25266 if (has_mbyte)
25267 p -= mb_head_off(tfname, p);
25268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 if (vim_ispathsep(*p))
25270 {
25271 if (sepcount == 0 || (hasTilde && sepcount == 1))
25272 break;
25273 else
25274 sepcount --;
25275 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 if (hasTilde)
25278 {
25279 --p;
25280 if (p >= tfname)
25281 *p = '~';
25282 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 }
25285 else
25286 ++p;
25287
25288 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25289 vim_free(*bufp);
25290 *fnamelen = (int)STRLEN(p);
25291 *bufp = pbuf;
25292 *fnamep = p;
25293
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025294 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295}
25296#endif /* WIN3264 */
25297
25298/*
25299 * Adjust a filename, according to a string of modifiers.
25300 * *fnamep must be NUL terminated when called. When returning, the length is
25301 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025302 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 * When there is an error, *fnamep is set to NULL.
25304 */
25305 int
25306modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25307 char_u *src; /* string with modifiers */
25308 int *usedlen; /* characters after src that are used */
25309 char_u **fnamep; /* file name so far */
25310 char_u **bufp; /* buffer for allocated file name or NULL */
25311 int *fnamelen; /* length of fnamep */
25312{
25313 int valid = 0;
25314 char_u *tail;
25315 char_u *s, *p, *pbuf;
25316 char_u dirname[MAXPATHL];
25317 int c;
25318 int has_fullname = 0;
25319#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025320 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321 int has_shortname = 0;
25322#endif
25323
25324repeat:
25325 /* ":p" - full path/file_name */
25326 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25327 {
25328 has_fullname = 1;
25329
25330 valid |= VALID_PATH;
25331 *usedlen += 2;
25332
25333 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25334 if ((*fnamep)[0] == '~'
25335#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25336 && ((*fnamep)[1] == '/'
25337# ifdef BACKSLASH_IN_FILENAME
25338 || (*fnamep)[1] == '\\'
25339# endif
25340 || (*fnamep)[1] == NUL)
25341
25342#endif
25343 )
25344 {
25345 *fnamep = expand_env_save(*fnamep);
25346 vim_free(*bufp); /* free any allocated file name */
25347 *bufp = *fnamep;
25348 if (*fnamep == NULL)
25349 return -1;
25350 }
25351
25352 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025353 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 {
25355 if (vim_ispathsep(*p)
25356 && p[1] == '.'
25357 && (p[2] == NUL
25358 || vim_ispathsep(p[2])
25359 || (p[2] == '.'
25360 && (p[3] == NUL || vim_ispathsep(p[3])))))
25361 break;
25362 }
25363
25364 /* FullName_save() is slow, don't use it when not needed. */
25365 if (*p != NUL || !vim_isAbsName(*fnamep))
25366 {
25367 *fnamep = FullName_save(*fnamep, *p != NUL);
25368 vim_free(*bufp); /* free any allocated file name */
25369 *bufp = *fnamep;
25370 if (*fnamep == NULL)
25371 return -1;
25372 }
25373
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025374#ifdef WIN3264
25375# if _WIN32_WINNT >= 0x0500
25376 if (vim_strchr(*fnamep, '~') != NULL)
25377 {
25378 /* Expand 8.3 filename to full path. Needed to make sure the same
25379 * file does not have two different names.
25380 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25381 p = alloc(_MAX_PATH + 1);
25382 if (p != NULL)
25383 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025384 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025385 {
25386 vim_free(*bufp);
25387 *bufp = *fnamep = p;
25388 }
25389 else
25390 vim_free(p);
25391 }
25392 }
25393# endif
25394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 /* Append a path separator to a directory. */
25396 if (mch_isdir(*fnamep))
25397 {
25398 /* Make room for one or two extra characters. */
25399 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25400 vim_free(*bufp); /* free any allocated file name */
25401 *bufp = *fnamep;
25402 if (*fnamep == NULL)
25403 return -1;
25404 add_pathsep(*fnamep);
25405 }
25406 }
25407
25408 /* ":." - path relative to the current directory */
25409 /* ":~" - path relative to the home directory */
25410 /* ":8" - shortname path - postponed till after */
25411 while (src[*usedlen] == ':'
25412 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25413 {
25414 *usedlen += 2;
25415 if (c == '8')
25416 {
25417#ifdef WIN3264
25418 has_shortname = 1; /* Postpone this. */
25419#endif
25420 continue;
25421 }
25422 pbuf = NULL;
25423 /* Need full path first (use expand_env() to remove a "~/") */
25424 if (!has_fullname)
25425 {
25426 if (c == '.' && **fnamep == '~')
25427 p = pbuf = expand_env_save(*fnamep);
25428 else
25429 p = pbuf = FullName_save(*fnamep, FALSE);
25430 }
25431 else
25432 p = *fnamep;
25433
25434 has_fullname = 0;
25435
25436 if (p != NULL)
25437 {
25438 if (c == '.')
25439 {
25440 mch_dirname(dirname, MAXPATHL);
25441 s = shorten_fname(p, dirname);
25442 if (s != NULL)
25443 {
25444 *fnamep = s;
25445 if (pbuf != NULL)
25446 {
25447 vim_free(*bufp); /* free any allocated file name */
25448 *bufp = pbuf;
25449 pbuf = NULL;
25450 }
25451 }
25452 }
25453 else
25454 {
25455 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25456 /* Only replace it when it starts with '~' */
25457 if (*dirname == '~')
25458 {
25459 s = vim_strsave(dirname);
25460 if (s != NULL)
25461 {
25462 *fnamep = s;
25463 vim_free(*bufp);
25464 *bufp = s;
25465 }
25466 }
25467 }
25468 vim_free(pbuf);
25469 }
25470 }
25471
25472 tail = gettail(*fnamep);
25473 *fnamelen = (int)STRLEN(*fnamep);
25474
25475 /* ":h" - head, remove "/file_name", can be repeated */
25476 /* Don't remove the first "/" or "c:\" */
25477 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25478 {
25479 valid |= VALID_HEAD;
25480 *usedlen += 2;
25481 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025482 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025483 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 *fnamelen = (int)(tail - *fnamep);
25485#ifdef VMS
25486 if (*fnamelen > 0)
25487 *fnamelen += 1; /* the path separator is part of the path */
25488#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025489 if (*fnamelen == 0)
25490 {
25491 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25492 p = vim_strsave((char_u *)".");
25493 if (p == NULL)
25494 return -1;
25495 vim_free(*bufp);
25496 *bufp = *fnamep = tail = p;
25497 *fnamelen = 1;
25498 }
25499 else
25500 {
25501 while (tail > s && !after_pathsep(s, tail))
25502 mb_ptr_back(*fnamep, tail);
25503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 }
25505
25506 /* ":8" - shortname */
25507 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25508 {
25509 *usedlen += 2;
25510#ifdef WIN3264
25511 has_shortname = 1;
25512#endif
25513 }
25514
25515#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025516 /*
25517 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 */
25519 if (has_shortname)
25520 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025521 /* Copy the string if it is shortened by :h and when it wasn't copied
25522 * yet, because we are going to change it in place. Avoids changing
25523 * the buffer name for "%:8". */
25524 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 {
25526 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025527 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 return -1;
25529 vim_free(*bufp);
25530 *bufp = *fnamep = p;
25531 }
25532
25533 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025534 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 if (!has_fullname && !vim_isAbsName(*fnamep))
25536 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025537 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 return -1;
25539 }
25540 else
25541 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025542 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543
Bram Moolenaardc935552011-08-17 15:23:23 +020025544 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025546 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 return -1;
25548
25549 if (l == 0)
25550 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025551 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025553 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 return -1;
25555 }
25556 *fnamelen = l;
25557 }
25558 }
25559#endif /* WIN3264 */
25560
25561 /* ":t" - tail, just the basename */
25562 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25563 {
25564 *usedlen += 2;
25565 *fnamelen -= (int)(tail - *fnamep);
25566 *fnamep = tail;
25567 }
25568
25569 /* ":e" - extension, can be repeated */
25570 /* ":r" - root, without extension, can be repeated */
25571 while (src[*usedlen] == ':'
25572 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25573 {
25574 /* find a '.' in the tail:
25575 * - for second :e: before the current fname
25576 * - otherwise: The last '.'
25577 */
25578 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25579 s = *fnamep - 2;
25580 else
25581 s = *fnamep + *fnamelen - 1;
25582 for ( ; s > tail; --s)
25583 if (s[0] == '.')
25584 break;
25585 if (src[*usedlen + 1] == 'e') /* :e */
25586 {
25587 if (s > tail)
25588 {
25589 *fnamelen += (int)(*fnamep - (s + 1));
25590 *fnamep = s + 1;
25591#ifdef VMS
25592 /* cut version from the extension */
25593 s = *fnamep + *fnamelen - 1;
25594 for ( ; s > *fnamep; --s)
25595 if (s[0] == ';')
25596 break;
25597 if (s > *fnamep)
25598 *fnamelen = s - *fnamep;
25599#endif
25600 }
25601 else if (*fnamep <= tail)
25602 *fnamelen = 0;
25603 }
25604 else /* :r */
25605 {
25606 if (s > tail) /* remove one extension */
25607 *fnamelen = (int)(s - *fnamep);
25608 }
25609 *usedlen += 2;
25610 }
25611
25612 /* ":s?pat?foo?" - substitute */
25613 /* ":gs?pat?foo?" - global substitute */
25614 if (src[*usedlen] == ':'
25615 && (src[*usedlen + 1] == 's'
25616 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25617 {
25618 char_u *str;
25619 char_u *pat;
25620 char_u *sub;
25621 int sep;
25622 char_u *flags;
25623 int didit = FALSE;
25624
25625 flags = (char_u *)"";
25626 s = src + *usedlen + 2;
25627 if (src[*usedlen + 1] == 'g')
25628 {
25629 flags = (char_u *)"g";
25630 ++s;
25631 }
25632
25633 sep = *s++;
25634 if (sep)
25635 {
25636 /* find end of pattern */
25637 p = vim_strchr(s, sep);
25638 if (p != NULL)
25639 {
25640 pat = vim_strnsave(s, (int)(p - s));
25641 if (pat != NULL)
25642 {
25643 s = p + 1;
25644 /* find end of substitution */
25645 p = vim_strchr(s, sep);
25646 if (p != NULL)
25647 {
25648 sub = vim_strnsave(s, (int)(p - s));
25649 str = vim_strnsave(*fnamep, *fnamelen);
25650 if (sub != NULL && str != NULL)
25651 {
25652 *usedlen = (int)(p + 1 - src);
25653 s = do_string_sub(str, pat, sub, flags);
25654 if (s != NULL)
25655 {
25656 *fnamep = s;
25657 *fnamelen = (int)STRLEN(s);
25658 vim_free(*bufp);
25659 *bufp = s;
25660 didit = TRUE;
25661 }
25662 }
25663 vim_free(sub);
25664 vim_free(str);
25665 }
25666 vim_free(pat);
25667 }
25668 }
25669 /* after using ":s", repeat all the modifiers */
25670 if (didit)
25671 goto repeat;
25672 }
25673 }
25674
Bram Moolenaar26df0922014-02-23 23:39:13 +010025675 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25676 {
25677 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25678 if (p == NULL)
25679 return -1;
25680 vim_free(*bufp);
25681 *bufp = *fnamep = p;
25682 *fnamelen = (int)STRLEN(p);
25683 *usedlen += 2;
25684 }
25685
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 return valid;
25687}
25688
25689/*
25690 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25691 * "flags" can be "g" to do a global substitute.
25692 * Returns an allocated string, NULL for error.
25693 */
25694 char_u *
25695do_string_sub(str, pat, sub, flags)
25696 char_u *str;
25697 char_u *pat;
25698 char_u *sub;
25699 char_u *flags;
25700{
25701 int sublen;
25702 regmatch_T regmatch;
25703 int i;
25704 int do_all;
25705 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025706 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707 garray_T ga;
25708 char_u *ret;
25709 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025710 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711
25712 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25713 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025714 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715
25716 ga_init2(&ga, 1, 200);
25717
25718 do_all = (flags[0] == 'g');
25719
25720 regmatch.rm_ic = p_ic;
25721 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25722 if (regmatch.regprog != NULL)
25723 {
25724 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025725 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25727 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025728 /* Skip empty match except for first match. */
25729 if (regmatch.startp[0] == regmatch.endp[0])
25730 {
25731 if (zero_width == regmatch.startp[0])
25732 {
25733 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025734 i = MB_PTR2LEN(tail);
25735 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25736 (size_t)i);
25737 ga.ga_len += i;
25738 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025739 continue;
25740 }
25741 zero_width = regmatch.startp[0];
25742 }
25743
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 /*
25745 * Get some space for a temporary buffer to do the substitution
25746 * into. It will contain:
25747 * - The text up to where the match is.
25748 * - The substituted text.
25749 * - The text after the match.
25750 */
25751 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025752 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25754 {
25755 ga_clear(&ga);
25756 break;
25757 }
25758
25759 /* copy the text up to where the match is */
25760 i = (int)(regmatch.startp[0] - tail);
25761 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25762 /* add the substituted text */
25763 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25764 + ga.ga_len + i, TRUE, TRUE, FALSE);
25765 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025766 tail = regmatch.endp[0];
25767 if (*tail == NUL)
25768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 if (!do_all)
25770 break;
25771 }
25772
25773 if (ga.ga_data != NULL)
25774 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25775
Bram Moolenaar473de612013-06-08 18:19:48 +020025776 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 }
25778
25779 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25780 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025781 if (p_cpo == empty_option)
25782 p_cpo = save_cpo;
25783 else
25784 /* Darn, evaluating {sub} expression changed the value. */
25785 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786
25787 return ret;
25788}
25789
25790#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */