blob: 2d6c34b92afa939dfe868bd7989563aecf5884cb [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 Moolenaarbbfbaf92015-12-01 15:32:56 +01008076 {"assert_equal", 2, 3, f_assert_equal},
8077 {"assert_false", 1, 2, f_assert_false},
8078 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008082 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008085 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"bufexists", 1, 1, f_bufexists},
8087 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8088 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8089 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8090 {"buflisted", 1, 1, f_buflisted},
8091 {"bufloaded", 1, 1, f_bufloaded},
8092 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008093 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"bufwinnr", 1, 1, f_bufwinnr},
8095 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008097 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"ceil", 1, 1, f_ceil},
8101#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008102 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008103 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008105 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008107#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008108 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008109 {"complete_add", 1, 1, f_complete_add},
8110 {"complete_check", 0, 0, f_complete_check},
8111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008113 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008117#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008120 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008121 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"delete", 1, 1, f_delete},
8123 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008124 {"diff_filler", 1, 1, f_diff_filler},
8125 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008126 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008128 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"eventhandler", 0, 0, f_eventhandler},
8130 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008131 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008133#ifdef FEAT_FLOAT
8134 {"exp", 1, 1, f_exp},
8135#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008136 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008137 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008138 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8140 {"filereadable", 1, 1, f_filereadable},
8141 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008142 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008143 {"finddir", 1, 3, f_finddir},
8144 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#ifdef FEAT_FLOAT
8146 {"float2nr", 1, 1, f_float2nr},
8147 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008149#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008150 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"fnamemodify", 2, 2, f_fnamemodify},
8152 {"foldclosed", 1, 1, f_foldclosed},
8153 {"foldclosedend", 1, 1, f_foldclosedend},
8154 {"foldlevel", 1, 1, f_foldlevel},
8155 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008156 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008159 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008160 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008161 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008162 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"getchar", 0, 1, f_getchar},
8164 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008165 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"getcmdline", 0, 0, f_getcmdline},
8167 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008168 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008169 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008170 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008172 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008173 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"getfsize", 1, 1, f_getfsize},
8175 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008176 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008177 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008178 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008179 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008180 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008181 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008182 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008183 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008185 {"gettabvar", 2, 3, f_gettabvar},
8186 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"getwinposx", 0, 0, f_getwinposx},
8188 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008189 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008190 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008191 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008192 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008195 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008196 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8198 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8199 {"histadd", 2, 2, f_histadd},
8200 {"histdel", 1, 2, f_histdel},
8201 {"histget", 1, 2, f_histget},
8202 {"histnr", 1, 1, f_histnr},
8203 {"hlID", 1, 1, f_hlID},
8204 {"hlexists", 1, 1, f_hlexists},
8205 {"hostname", 0, 0, f_hostname},
8206 {"iconv", 3, 3, f_iconv},
8207 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008209 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008211 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"inputrestore", 0, 0, f_inputrestore},
8213 {"inputsave", 0, 0, f_inputsave},
8214 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008216 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008218 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008219 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"libcall", 3, 3, f_libcall},
8225 {"libcallnr", 3, 3, f_libcallnr},
8226 {"line", 1, 1, f_line},
8227 {"line2byte", 1, 1, f_line2byte},
8228 {"lispindent", 1, 1, f_lispindent},
8229 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008230#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008231 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232 {"log10", 1, 1, f_log10},
8233#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008234#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008235 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008236#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008238 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008239 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008240 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008241 {"matchadd", 2, 5, f_matchadd},
8242 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008243 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008244 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008245 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008246 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008247 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008248 {"max", 1, 1, f_max},
8249 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008250#ifdef vim_mkdir
8251 {"mkdir", 1, 3, f_mkdir},
8252#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008254#ifdef FEAT_MZSCHEME
8255 {"mzeval", 1, 1, f_mzeval},
8256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008258 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008259 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008260 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"pow", 2, 2, f_pow},
8263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008265 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008267#ifdef FEAT_PYTHON3
8268 {"py3eval", 1, 1, f_py3eval},
8269#endif
8270#ifdef FEAT_PYTHON
8271 {"pyeval", 1, 1, f_pyeval},
8272#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008273 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008274 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008275 {"reltime", 0, 2, f_reltime},
8276 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"remote_expr", 2, 3, f_remote_expr},
8278 {"remote_foreground", 1, 1, f_remote_foreground},
8279 {"remote_peek", 1, 2, f_remote_peek},
8280 {"remote_read", 1, 1, f_remote_read},
8281 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008282 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008284 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008286 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008287#ifdef FEAT_FLOAT
8288 {"round", 1, 1, f_round},
8289#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008290 {"screenattr", 2, 2, f_screenattr},
8291 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008292 {"screencol", 0, 0, f_screencol},
8293 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008294 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008295 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008296 {"searchpair", 3, 7, f_searchpair},
8297 {"searchpairpos", 3, 7, f_searchpairpos},
8298 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"server2client", 2, 2, f_server2client},
8300 {"serverlist", 0, 0, f_serverlist},
8301 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008302 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"setcmdpos", 1, 1, f_setcmdpos},
8304 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008305 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008306 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008307 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008308 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008310 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008311 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008313#ifdef FEAT_CRYPT
8314 {"sha256", 1, 1, f_sha256},
8315#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008316 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008317 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008319#ifdef FEAT_FLOAT
8320 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008321 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008323 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008324 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008325 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008326 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008327 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328#ifdef FEAT_FLOAT
8329 {"sqrt", 1, 1, f_sqrt},
8330 {"str2float", 1, 1, f_str2float},
8331#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008332 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008333 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008334 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335#ifdef HAVE_STRFTIME
8336 {"strftime", 1, 2, f_strftime},
8337#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"strlen", 1, 1, f_strlen},
8341 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008342 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008344 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008345 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"substitute", 4, 4, f_substitute},
8347 {"synID", 3, 3, f_synID},
8348 {"synIDattr", 2, 3, f_synIDattr},
8349 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008350 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008351 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008352 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008353 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008354 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008355 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008356 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008357 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008358 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008359#ifdef FEAT_FLOAT
8360 {"tan", 1, 1, f_tan},
8361 {"tanh", 1, 1, f_tanh},
8362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008364 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"tolower", 1, 1, f_tolower},
8366 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008367 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#ifdef FEAT_FLOAT
8369 {"trunc", 1, 1, f_trunc},
8370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008372 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008373 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008374 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008375 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"virtcol", 1, 1, f_virtcol},
8377 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008378 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"winbufnr", 1, 1, f_winbufnr},
8380 {"wincol", 0, 0, f_wincol},
8381 {"winheight", 1, 1, f_winheight},
8382 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008383 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008385 {"winrestview", 1, 1, f_winrestview},
8386 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008388 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008389 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390};
8391
8392#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8393
8394/*
8395 * Function given to ExpandGeneric() to obtain the list of internal
8396 * or user defined function names.
8397 */
8398 char_u *
8399get_function_name(xp, idx)
8400 expand_T *xp;
8401 int idx;
8402{
8403 static int intidx = -1;
8404 char_u *name;
8405
8406 if (idx == 0)
8407 intidx = -1;
8408 if (intidx < 0)
8409 {
8410 name = get_user_func_name(xp, idx);
8411 if (name != NULL)
8412 return name;
8413 }
8414 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8415 {
8416 STRCPY(IObuff, functions[intidx].f_name);
8417 STRCAT(IObuff, "(");
8418 if (functions[intidx].f_max_argc == 0)
8419 STRCAT(IObuff, ")");
8420 return IObuff;
8421 }
8422
8423 return NULL;
8424}
8425
8426/*
8427 * Function given to ExpandGeneric() to obtain the list of internal or
8428 * user defined variable or function names.
8429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 char_u *
8431get_expr_name(xp, idx)
8432 expand_T *xp;
8433 int idx;
8434{
8435 static int intidx = -1;
8436 char_u *name;
8437
8438 if (idx == 0)
8439 intidx = -1;
8440 if (intidx < 0)
8441 {
8442 name = get_function_name(xp, idx);
8443 if (name != NULL)
8444 return name;
8445 }
8446 return get_user_var_name(xp, ++intidx);
8447}
8448
8449#endif /* FEAT_CMDL_COMPL */
8450
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008451#if defined(EBCDIC) || defined(PROTO)
8452/*
8453 * Compare struct fst by function name.
8454 */
8455 static int
8456compare_func_name(s1, s2)
8457 const void *s1;
8458 const void *s2;
8459{
8460 struct fst *p1 = (struct fst *)s1;
8461 struct fst *p2 = (struct fst *)s2;
8462
8463 return STRCMP(p1->f_name, p2->f_name);
8464}
8465
8466/*
8467 * Sort the function table by function name.
8468 * The sorting of the table above is ASCII dependant.
8469 * On machines using EBCDIC we have to sort it.
8470 */
8471 static void
8472sortFunctions()
8473{
8474 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8475
8476 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8477}
8478#endif
8479
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481/*
8482 * Find internal function in table above.
8483 * Return index, or -1 if not found
8484 */
8485 static int
8486find_internal_func(name)
8487 char_u *name; /* name of the function */
8488{
8489 int first = 0;
8490 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8491 int cmp;
8492 int x;
8493
8494 /*
8495 * Find the function name in the table. Binary search.
8496 */
8497 while (first <= last)
8498 {
8499 x = first + ((unsigned)(last - first) >> 1);
8500 cmp = STRCMP(name, functions[x].f_name);
8501 if (cmp < 0)
8502 last = x - 1;
8503 else if (cmp > 0)
8504 first = x + 1;
8505 else
8506 return x;
8507 }
8508 return -1;
8509}
8510
8511/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8513 * name it contains, otherwise return "name".
8514 */
8515 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008516deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 char_u *name;
8518 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008519 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520{
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 int cc;
8523
8524 cc = name[*lenp];
8525 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008526 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {
8532 *lenp = 0;
8533 return (char_u *)""; /* just in case */
8534 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008535 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 }
8538
8539 return name;
8540}
8541
8542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 * Allocate a variable for the result of a function.
8544 * Return OK or FAIL.
8545 */
8546 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8548 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 char_u *name; /* name of the function */
8550 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 char_u **arg; /* argument, pointing to the '(' */
8553 linenr_T firstline; /* first line of range */
8554 linenr_T lastline; /* last line of range */
8555 int *doesrange; /* return: function handled range */
8556 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008557 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 char_u *argp;
8560 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008561 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 int argcount = 0; /* number of arguments found */
8563
8564 /*
8565 * Get the arguments.
8566 */
8567 argp = *arg;
8568 while (argcount < MAX_FUNC_ARGS)
8569 {
8570 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8571 if (*argp == ')' || *argp == ',' || *argp == NUL)
8572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8574 {
8575 ret = FAIL;
8576 break;
8577 }
8578 ++argcount;
8579 if (*argp != ',')
8580 break;
8581 }
8582 if (*argp == ')')
8583 ++argp;
8584 else
8585 ret = FAIL;
8586
8587 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008589 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 {
8592 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008593 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008594 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008595 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597
8598 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 *arg = skipwhite(argp);
8602 return ret;
8603}
8604
8605
8606/*
8607 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008608 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008609 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 */
8611 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008612call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008613 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008614 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008618 typval_T *argvars; /* vars for arguments, must have "argcount"
8619 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 linenr_T firstline; /* first line of range */
8621 linenr_T lastline; /* last line of range */
8622 int *doesrange; /* return: function handled range */
8623 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008624 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#define ERROR_UNKNOWN 0
8628#define ERROR_TOOMANY 1
8629#define ERROR_TOOFEW 2
8630#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631#define ERROR_DICT 4
8632#define ERROR_NONE 5
8633#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 int error = ERROR_NONE;
8635 int i;
8636 int llen;
8637 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638#define FLEN_FIXED 40
8639 char_u fname_buf[FLEN_FIXED + 1];
8640 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008641 char_u *name;
8642
8643 /* Make a copy of the name, if it comes from a funcref variable it could
8644 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008645 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008646 if (name == NULL)
8647 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 /*
8650 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8651 * Change <SNR>123_name() to K_SNR 123_name().
8652 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 llen = eval_fname_script(name);
8655 if (llen > 0)
8656 {
8657 fname_buf[0] = K_SPECIAL;
8658 fname_buf[1] = KS_EXTRA;
8659 fname_buf[2] = (int)KE_SNR;
8660 i = 3;
8661 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8662 {
8663 if (current_SID <= 0)
8664 error = ERROR_SCRIPT;
8665 else
8666 {
8667 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8668 i = (int)STRLEN(fname_buf);
8669 }
8670 }
8671 if (i + STRLEN(name + llen) < FLEN_FIXED)
8672 {
8673 STRCPY(fname_buf + i, name + llen);
8674 fname = fname_buf;
8675 }
8676 else
8677 {
8678 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8679 if (fname == NULL)
8680 error = ERROR_OTHER;
8681 else
8682 {
8683 mch_memmove(fname, fname_buf, (size_t)i);
8684 STRCPY(fname + i, name + llen);
8685 }
8686 }
8687 }
8688 else
8689 fname = name;
8690
8691 *doesrange = FALSE;
8692
8693
8694 /* execute the function if no errors detected and executing */
8695 if (evaluate && error == ERROR_NONE)
8696 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008697 char_u *rfname = fname;
8698
8699 /* Ignore "g:" before a function name. */
8700 if (fname[0] == 'g' && fname[1] == ':')
8701 rfname = fname + 2;
8702
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008703 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 error = ERROR_UNKNOWN;
8706
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 /*
8710 * User defined function.
8711 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008712 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715 /* Trigger FuncUndefined event, may load the function. */
8716 if (fp == NULL
8717 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008722 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
8724#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008725 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008726 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727 {
8728 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008729 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008730 }
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (fp != NULL)
8733 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008734 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008736 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 else
8743 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 int did_save_redo = FALSE;
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 /*
8747 * Call the user function.
8748 * Save and restore search patterns, script variables and
8749 * redo buffer.
8750 */
8751 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008752#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008753 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008754#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 {
8756 saveRedobuff();
8757 did_save_redo = TRUE;
8758 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008759 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008762 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8763 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8764 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008765 /* Function was unreferenced while being used, free it
8766 * now. */
8767 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008768 if (did_save_redo)
8769 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 restore_search_patterns();
8771 error = ERROR_NONE;
8772 }
8773 }
8774 }
8775 else
8776 {
8777 /*
8778 * Find the function name in the table, call its implementation.
8779 */
8780 i = find_internal_func(fname);
8781 if (i >= 0)
8782 {
8783 if (argcount < functions[i].f_min_argc)
8784 error = ERROR_TOOFEW;
8785 else if (argcount > functions[i].f_max_argc)
8786 error = ERROR_TOOMANY;
8787 else
8788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008789 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 error = ERROR_NONE;
8792 }
8793 }
8794 }
8795 /*
8796 * The function call (or "FuncUndefined" autocommand sequence) might
8797 * have been aborted by an error, an interrupt, or an explicitly thrown
8798 * exception that has not been caught so far. This situation can be
8799 * tested for by calling aborting(). For an error in an internal
8800 * function or for the "E132" error in call_user_func(), however, the
8801 * throw point at which the "force_abort" flag (temporarily reset by
8802 * emsg()) is normally updated has not been reached yet. We need to
8803 * update that flag first to make aborting() reliable.
8804 */
8805 update_force_abort();
8806 }
8807 if (error == ERROR_NONE)
8808 ret = OK;
8809
8810 /*
8811 * Report an error unless the argument evaluation or function call has been
8812 * cancelled due to an aborting error, an interrupt, or an exception.
8813 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008814 if (!aborting())
8815 {
8816 switch (error)
8817 {
8818 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008819 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 break;
8821 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008822 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 break;
8824 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008825 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008826 name);
8827 break;
8828 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008829 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 name);
8831 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008832 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008833 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 name);
8835 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 }
8837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (fname != name && fname != fname_buf)
8840 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008841 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
8843 return ret;
8844}
8845
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008846/*
8847 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008848 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008849 */
8850 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008851emsg_funcname(ermsg, name)
8852 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008853 char_u *name;
8854{
8855 char_u *p;
8856
8857 if (*name == K_SPECIAL)
8858 p = concat_str((char_u *)"<SNR>", name + 3);
8859 else
8860 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008861 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008862 if (p != name)
8863 vim_free(p);
8864}
8865
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008866/*
8867 * Return TRUE for a non-zero Number and a non-empty String.
8868 */
8869 static int
8870non_zero_arg(argvars)
8871 typval_T *argvars;
8872{
8873 return ((argvars[0].v_type == VAR_NUMBER
8874 && argvars[0].vval.v_number != 0)
8875 || (argvars[0].v_type == VAR_STRING
8876 && argvars[0].vval.v_string != NULL
8877 && *argvars[0].vval.v_string != NUL));
8878}
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880/*********************************************
8881 * Implementation of the built-in functions
8882 */
8883
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008884#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008885static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8886
8887/*
8888 * Get the float value of "argvars[0]" into "f".
8889 * Returns FAIL when the argument is not a Number or Float.
8890 */
8891 static int
8892get_float_arg(argvars, f)
8893 typval_T *argvars;
8894 float_T *f;
8895{
8896 if (argvars[0].v_type == VAR_FLOAT)
8897 {
8898 *f = argvars[0].vval.v_float;
8899 return OK;
8900 }
8901 if (argvars[0].v_type == VAR_NUMBER)
8902 {
8903 *f = (float_T)argvars[0].vval.v_number;
8904 return OK;
8905 }
8906 EMSG(_("E808: Number or Float required"));
8907 return FAIL;
8908}
8909
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008910/*
8911 * "abs(expr)" function
8912 */
8913 static void
8914f_abs(argvars, rettv)
8915 typval_T *argvars;
8916 typval_T *rettv;
8917{
8918 if (argvars[0].v_type == VAR_FLOAT)
8919 {
8920 rettv->v_type = VAR_FLOAT;
8921 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8922 }
8923 else
8924 {
8925 varnumber_T n;
8926 int error = FALSE;
8927
8928 n = get_tv_number_chk(&argvars[0], &error);
8929 if (error)
8930 rettv->vval.v_number = -1;
8931 else if (n > 0)
8932 rettv->vval.v_number = n;
8933 else
8934 rettv->vval.v_number = -n;
8935 }
8936}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008937
8938/*
8939 * "acos()" function
8940 */
8941 static void
8942f_acos(argvars, rettv)
8943 typval_T *argvars;
8944 typval_T *rettv;
8945{
8946 float_T f;
8947
8948 rettv->v_type = VAR_FLOAT;
8949 if (get_float_arg(argvars, &f) == OK)
8950 rettv->vval.v_float = acos(f);
8951 else
8952 rettv->vval.v_float = 0.0;
8953}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008954#endif
8955
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 */
8959 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008960f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T *argvars;
8962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963{
Bram Moolenaar33570922005-01-25 22:26:29 +00008964 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008969 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008970 && !tv_check_lock(l->lv_lock,
8971 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008972 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974 }
8975 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008976 EMSG(_(e_listreq));
8977}
8978
8979/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008980 * "and(expr, expr)" function
8981 */
8982 static void
8983f_and(argvars, rettv)
8984 typval_T *argvars;
8985 typval_T *rettv;
8986{
8987 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8988 & get_tv_number_chk(&argvars[1], NULL);
8989}
8990
8991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 * "append(lnum, string/list)" function
8993 */
8994 static void
8995f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *argvars;
8997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008998{
8999 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 list_T *l = NULL;
9002 listitem_T *li = NULL;
9003 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 long added = 0;
9005
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009006 /* When coming here from Insert mode, sync undo, so that this can be
9007 * undone separately from what was previously inserted. */
9008 if (u_sync_once == 2)
9009 {
9010 u_sync_once = 1; /* notify that u_sync() was called */
9011 u_sync(TRUE);
9012 }
9013
Bram Moolenaar0d660222005-01-07 21:51:51 +00009014 lnum = get_tv_lnum(argvars);
9015 if (lnum >= 0
9016 && lnum <= curbuf->b_ml.ml_line_count
9017 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009019 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 l = argvars[1].vval.v_list;
9022 if (l == NULL)
9023 return;
9024 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009026 for (;;)
9027 {
9028 if (l == NULL)
9029 tv = &argvars[1]; /* append a string */
9030 else if (li == NULL)
9031 break; /* end of list */
9032 else
9033 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 line = get_tv_string_chk(tv);
9035 if (line == NULL) /* type error */
9036 {
9037 rettv->vval.v_number = 1; /* Failed */
9038 break;
9039 }
9040 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009041 ++added;
9042 if (l == NULL)
9043 break;
9044 li = li->li_next;
9045 }
9046
9047 appended_lines_mark(lnum, added);
9048 if (curwin->w_cursor.lnum > lnum)
9049 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 else
9052 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
9055/*
9056 * "argc()" function
9057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064}
9065
9066/*
9067 * "argidx()" function
9068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009071 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075}
9076
9077/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009078 * "arglistid()" function
9079 */
9080 static void
9081f_arglistid(argvars, rettv)
9082 typval_T *argvars UNUSED;
9083 typval_T *rettv;
9084{
9085 win_T *wp;
9086 tabpage_T *tp = NULL;
9087 long n;
9088
9089 rettv->vval.v_number = -1;
9090 if (argvars[0].v_type != VAR_UNKNOWN)
9091 {
9092 if (argvars[1].v_type != VAR_UNKNOWN)
9093 {
9094 n = get_tv_number(&argvars[1]);
9095 if (n >= 0)
9096 tp = find_tabpage(n);
9097 }
9098 else
9099 tp = curtab;
9100
9101 if (tp != NULL)
9102 {
9103 wp = find_win_by_nr(&argvars[0], tp);
9104 if (wp != NULL)
9105 rettv->vval.v_number = wp->w_alist->id;
9106 }
9107 }
9108 else
9109 rettv->vval.v_number = curwin->w_alist->id;
9110}
9111
9112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 * "argv(nr)" function
9114 */
9115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *argvars;
9118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 int idx;
9121
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009122 if (argvars[0].v_type != VAR_UNKNOWN)
9123 {
9124 idx = get_tv_number_chk(&argvars[0], NULL);
9125 if (idx >= 0 && idx < ARGCOUNT)
9126 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9127 else
9128 rettv->vval.v_string = NULL;
9129 rettv->v_type = VAR_STRING;
9130 }
9131 else if (rettv_list_alloc(rettv) == OK)
9132 for (idx = 0; idx < ARGCOUNT; ++idx)
9133 list_append_string(rettv->vval.v_list,
9134 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135}
9136
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009137static void prepare_assert_error __ARGS((garray_T*gap));
9138static 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));
9139static void assert_error __ARGS((garray_T *gap));
9140static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9141
9142/*
9143 * Prepare "gap" for an assert error and add the sourcing position.
9144 */
9145 static void
9146prepare_assert_error(gap)
9147 garray_T *gap;
9148{
9149 char buf[NUMBUFLEN];
9150
9151 ga_init2(gap, 1, 100);
9152 ga_concat(gap, sourcing_name);
9153 sprintf(buf, " line %ld", (long)sourcing_lnum);
9154 ga_concat(gap, (char_u *)buf);
9155 ga_concat(gap, (char_u *)": ");
9156}
9157
9158/*
9159 * Fill "gap" with information about an assert error.
9160 */
9161 static void
9162fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9163 garray_T *gap;
9164 typval_T *opt_msg_tv;
9165 char_u *exp_str;
9166 typval_T *exp_tv;
9167 typval_T *got_tv;
9168{
9169 char_u numbuf[NUMBUFLEN];
9170 char_u *tofree;
9171
9172 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9173 {
9174 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9175 vim_free(tofree);
9176 }
9177 else
9178 {
9179 ga_concat(gap, (char_u *)"Expected ");
9180 if (exp_str == NULL)
9181 {
9182 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9183 vim_free(tofree);
9184 }
9185 else
9186 ga_concat(gap, exp_str);
9187 ga_concat(gap, (char_u *)" but got ");
9188 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9189 vim_free(tofree);
9190 }
9191}
Bram Moolenaar43345542015-11-29 17:35:35 +01009192
9193/*
9194 * Add an assert error to v:errors.
9195 */
9196 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009197assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009198 garray_T *gap;
9199{
9200 struct vimvar *vp = &vimvars[VV_ERRORS];
9201
9202 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9203 /* Make sure v:errors is a list. */
9204 set_vim_var_list(VV_ERRORS, list_alloc());
9205 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9206}
9207
Bram Moolenaar43345542015-11-29 17:35:35 +01009208/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009209 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009210 */
9211 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009212f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009213 typval_T *argvars;
9214 typval_T *rettv UNUSED;
9215{
9216 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009217
9218 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9219 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009220 prepare_assert_error(&ga);
9221 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9222 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009223 ga_clear(&ga);
9224 }
9225}
9226
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009227/*
9228 * Common for assert_true() and assert_false().
9229 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009230 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009231assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009232 typval_T *argvars;
9233 int isTrue;
9234{
9235 int error = FALSE;
9236 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009237
9238 if (argvars[0].v_type != VAR_NUMBER
9239 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9240 || error)
9241 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009242 prepare_assert_error(&ga);
9243 fill_assert_error(&ga, &argvars[1],
9244 (char_u *)(isTrue ? "True " : "False "),
9245 NULL, &argvars[0]);
9246 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009247 ga_clear(&ga);
9248 }
9249}
9250
9251/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009252 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009253 */
9254 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009255f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009256 typval_T *argvars;
9257 typval_T *rettv UNUSED;
9258{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009260}
9261
9262/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009263 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009264 */
9265 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009266f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009267 typval_T *argvars;
9268 typval_T *rettv UNUSED;
9269{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009270 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009271}
9272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009273#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009275 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009276 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009277 static void
9278f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009279 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009280 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009281{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009282 float_T f;
9283
9284 rettv->v_type = VAR_FLOAT;
9285 if (get_float_arg(argvars, &f) == OK)
9286 rettv->vval.v_float = asin(f);
9287 else
9288 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289}
9290
9291/*
9292 * "atan()" function
9293 */
9294 static void
9295f_atan(argvars, rettv)
9296 typval_T *argvars;
9297 typval_T *rettv;
9298{
9299 float_T f;
9300
9301 rettv->v_type = VAR_FLOAT;
9302 if (get_float_arg(argvars, &f) == OK)
9303 rettv->vval.v_float = atan(f);
9304 else
9305 rettv->vval.v_float = 0.0;
9306}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009307
9308/*
9309 * "atan2()" function
9310 */
9311 static void
9312f_atan2(argvars, rettv)
9313 typval_T *argvars;
9314 typval_T *rettv;
9315{
9316 float_T fx, fy;
9317
9318 rettv->v_type = VAR_FLOAT;
9319 if (get_float_arg(argvars, &fx) == OK
9320 && get_float_arg(&argvars[1], &fy) == OK)
9321 rettv->vval.v_float = atan2(fx, fy);
9322 else
9323 rettv->vval.v_float = 0.0;
9324}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009325#endif
9326
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327/*
9328 * "browse(save, title, initdir, default)" function
9329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
9335#ifdef FEAT_BROWSE
9336 int save;
9337 char_u *title;
9338 char_u *initdir;
9339 char_u *defname;
9340 char_u buf[NUMBUFLEN];
9341 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 save = get_tv_number_chk(&argvars[0], &error);
9345 title = get_tv_string_chk(&argvars[1]);
9346 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9347 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 if (error || title == NULL || initdir == NULL || defname == NULL)
9350 rettv->vval.v_string = NULL;
9351 else
9352 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009353 do_browse(save ? BROWSE_SAVE : 0,
9354 title, defname, NULL, initdir, NULL, curbuf);
9355#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009359}
9360
9361/*
9362 * "browsedir(title, initdir)" function
9363 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009366 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009368{
9369#ifdef FEAT_BROWSE
9370 char_u *title;
9371 char_u *initdir;
9372 char_u buf[NUMBUFLEN];
9373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 title = get_tv_string_chk(&argvars[0]);
9375 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 if (title == NULL || initdir == NULL)
9378 rettv->vval.v_string = NULL;
9379 else
9380 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009381 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386}
9387
Bram Moolenaar33570922005-01-25 22:26:29 +00009388static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009389
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390/*
9391 * Find a buffer by number or exact name.
9392 */
9393 static buf_T *
9394find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009399 if (avar->v_type == VAR_NUMBER)
9400 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009401 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009403 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009404 if (buf == NULL)
9405 {
9406 /* No full path name match, try a match with a URL or a "nofile"
9407 * buffer, these don't use the full path. */
9408 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9409 if (buf->b_fname != NULL
9410 && (path_with_url(buf->b_fname)
9411#ifdef FEAT_QUICKFIX
9412 || bt_nofile(buf)
9413#endif
9414 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009415 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009416 break;
9417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 }
9419 return buf;
9420}
9421
9422/*
9423 * "bufexists(expr)" function
9424 */
9425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432
9433/*
9434 * "buflisted(expr)" function
9435 */
9436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009438 typval_T *argvars;
9439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441 buf_T *buf;
9442
9443 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447/*
9448 * "bufloaded(expr)" function
9449 */
9450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454{
9455 buf_T *buf;
9456
9457 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009461static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463/*
9464 * Get buffer by number or pattern.
9465 */
9466 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009467get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009469 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 int save_magic;
9473 char_u *save_cpo;
9474 buf_T *buf;
9475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 if (tv->v_type == VAR_NUMBER)
9477 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009478 if (tv->v_type != VAR_STRING)
9479 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (name == NULL || *name == NUL)
9481 return curbuf;
9482 if (name[0] == '$' && name[1] == NUL)
9483 return lastbuf;
9484
9485 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9486 save_magic = p_magic;
9487 p_magic = TRUE;
9488 save_cpo = p_cpo;
9489 p_cpo = (char_u *)"";
9490
9491 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009492 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
9494 p_magic = save_magic;
9495 p_cpo = save_cpo;
9496
9497 /* If not found, try expanding the name, like done for bufexists(). */
9498 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
9501 return buf;
9502}
9503
9504/*
9505 * "bufname(expr)" function
9506 */
9507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 typval_T *argvars;
9510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512 buf_T *buf;
9513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009516 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 --emsg_off;
9523}
9524
9525/*
9526 * "bufnr(expr)" function
9527 */
9528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009530 typval_T *argvars;
9531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009534 int error = FALSE;
9535 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009539 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009540 --emsg_off;
9541
9542 /* If the buffer isn't found and the second argument is not zero create a
9543 * new buffer. */
9544 if (buf == NULL
9545 && argvars[1].v_type != VAR_UNKNOWN
9546 && get_tv_number_chk(&argvars[1], &error) != 0
9547 && !error
9548 && (name = get_tv_string_chk(&argvars[0])) != NULL
9549 && !error)
9550 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
9557
9558/*
9559 * "bufwinnr(nr)" function
9560 */
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566#ifdef FEAT_WINDOWS
9567 win_T *wp;
9568 int winnr = 0;
9569#endif
9570 buf_T *buf;
9571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009574 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575#ifdef FEAT_WINDOWS
9576 for (wp = firstwin; wp; wp = wp->w_next)
9577 {
9578 ++winnr;
9579 if (wp->w_buffer == buf)
9580 break;
9581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585#endif
9586 --emsg_off;
9587}
9588
9589/*
9590 * "byte2line(byte)" function
9591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#else
9600 long boff = 0;
9601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 (linenr_T)0, &boff);
9608#endif
9609}
9610
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009611 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009612byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009615 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009616{
9617#ifdef FEAT_MBYTE
9618 char_u *t;
9619#endif
9620 char_u *str;
9621 long idx;
9622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 str = get_tv_string_chk(&argvars[0]);
9624 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009627 return;
9628
9629#ifdef FEAT_MBYTE
9630 t = str;
9631 for ( ; idx > 0; idx--)
9632 {
9633 if (*t == NUL) /* EOL reached */
9634 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009635 if (enc_utf8 && comp)
9636 t += utf_ptr2len(t);
9637 else
9638 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009639 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009640 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009641#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009642 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009644#endif
9645}
9646
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009647/*
9648 * "byteidx()" function
9649 */
9650 static void
9651f_byteidx(argvars, rettv)
9652 typval_T *argvars;
9653 typval_T *rettv;
9654{
9655 byteidx(argvars, rettv, FALSE);
9656}
9657
9658/*
9659 * "byteidxcomp()" function
9660 */
9661 static void
9662f_byteidxcomp(argvars, rettv)
9663 typval_T *argvars;
9664 typval_T *rettv;
9665{
9666 byteidx(argvars, rettv, TRUE);
9667}
9668
Bram Moolenaardb913952012-06-29 12:54:53 +02009669 int
9670func_call(name, args, selfdict, rettv)
9671 char_u *name;
9672 typval_T *args;
9673 dict_T *selfdict;
9674 typval_T *rettv;
9675{
9676 listitem_T *item;
9677 typval_T argv[MAX_FUNC_ARGS + 1];
9678 int argc = 0;
9679 int dummy;
9680 int r = 0;
9681
9682 for (item = args->vval.v_list->lv_first; item != NULL;
9683 item = item->li_next)
9684 {
9685 if (argc == MAX_FUNC_ARGS)
9686 {
9687 EMSG(_("E699: Too many arguments"));
9688 break;
9689 }
9690 /* Make a copy of each argument. This is needed to be able to set
9691 * v_lock to VAR_FIXED in the copy without changing the original list.
9692 */
9693 copy_tv(&item->li_tv, &argv[argc++]);
9694 }
9695
9696 if (item == NULL)
9697 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9699 &dummy, TRUE, selfdict);
9700
9701 /* Free the arguments. */
9702 while (argc > 0)
9703 clear_tv(&argv[--argc]);
9704
9705 return r;
9706}
9707
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009708/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009709 * "call(func, arglist)" function
9710 */
9711 static void
9712f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009715{
9716 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009718
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009719 if (argvars[1].v_type != VAR_LIST)
9720 {
9721 EMSG(_(e_listreq));
9722 return;
9723 }
9724 if (argvars[1].vval.v_list == NULL)
9725 return;
9726
9727 if (argvars[0].v_type == VAR_FUNC)
9728 func = argvars[0].vval.v_string;
9729 else
9730 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 if (*func == NUL)
9732 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009733
Bram Moolenaare9a41262005-01-15 22:18:47 +00009734 if (argvars[2].v_type != VAR_UNKNOWN)
9735 {
9736 if (argvars[2].v_type != VAR_DICT)
9737 {
9738 EMSG(_(e_dictreq));
9739 return;
9740 }
9741 selfdict = argvars[2].vval.v_dict;
9742 }
9743
Bram Moolenaardb913952012-06-29 12:54:53 +02009744 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009745}
9746
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009747#ifdef FEAT_FLOAT
9748/*
9749 * "ceil({float})" function
9750 */
9751 static void
9752f_ceil(argvars, rettv)
9753 typval_T *argvars;
9754 typval_T *rettv;
9755{
9756 float_T f;
9757
9758 rettv->v_type = VAR_FLOAT;
9759 if (get_float_arg(argvars, &f) == OK)
9760 rettv->vval.v_float = ceil(f);
9761 else
9762 rettv->vval.v_float = 0.0;
9763}
9764#endif
9765
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009766/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009767 * "changenr()" function
9768 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009769 static void
9770f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009771 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009772 typval_T *rettv;
9773{
9774 rettv->vval.v_number = curbuf->b_u_seq_cur;
9775}
9776
9777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 * "char2nr(string)" function
9779 */
9780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009782 typval_T *argvars;
9783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785#ifdef FEAT_MBYTE
9786 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009787 {
9788 int utf8 = 0;
9789
9790 if (argvars[1].v_type != VAR_UNKNOWN)
9791 utf8 = get_tv_number_chk(&argvars[1], NULL);
9792
9793 if (utf8)
9794 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9795 else
9796 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 else
9799#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801}
9802
9803/*
9804 * "cindent(lnum)" function
9805 */
9806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811#ifdef FEAT_CINDENT
9812 pos_T pos;
9813 linenr_T lnum;
9814
9815 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9818 {
9819 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 curwin->w_cursor = pos;
9822 }
9823 else
9824#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009829 * "clearmatches()" function
9830 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009831 static void
9832f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009833 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009834 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009835{
9836#ifdef FEAT_SEARCH_EXTRA
9837 clear_matches(curwin);
9838#endif
9839}
9840
9841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 * "col(string)" function
9843 */
9844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009846 typval_T *argvars;
9847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
9849 colnr_T col = 0;
9850 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009851 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009853 fp = var2fpos(&argvars[0], FALSE, &fnum);
9854 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 {
9856 if (fp->col == MAXCOL)
9857 {
9858 /* '> can be MAXCOL, get the length of the line then */
9859 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009860 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 else
9862 col = MAXCOL;
9863 }
9864 else
9865 {
9866 col = fp->col + 1;
9867#ifdef FEAT_VIRTUALEDIT
9868 /* col(".") when the cursor is on the NUL at the end of the line
9869 * because of "coladd" can be seen as an extra column. */
9870 if (virtual_active() && fp == &curwin->w_cursor)
9871 {
9872 char_u *p = ml_get_cursor();
9873
9874 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9875 curwin->w_virtcol - curwin->w_cursor.coladd))
9876 {
9877# ifdef FEAT_MBYTE
9878 int l;
9879
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009880 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 col += l;
9882# else
9883 if (*p != NUL && p[1] == NUL)
9884 ++col;
9885# endif
9886 }
9887 }
9888#endif
9889 }
9890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892}
9893
Bram Moolenaar572cb562005-08-05 21:35:02 +00009894#if defined(FEAT_INS_EXPAND)
9895/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009896 * "complete()" function
9897 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009898 static void
9899f_complete(argvars, rettv)
9900 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009901 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009902{
9903 int startcol;
9904
9905 if ((State & INSERT) == 0)
9906 {
9907 EMSG(_("E785: complete() can only be used in Insert mode"));
9908 return;
9909 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009910
9911 /* Check for undo allowed here, because if something was already inserted
9912 * the line was already saved for undo and this check isn't done. */
9913 if (!undo_allowed())
9914 return;
9915
Bram Moolenaarade00832006-03-10 21:46:58 +00009916 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9917 {
9918 EMSG(_(e_invarg));
9919 return;
9920 }
9921
9922 startcol = get_tv_number_chk(&argvars[0], NULL);
9923 if (startcol <= 0)
9924 return;
9925
9926 set_completion(startcol - 1, argvars[1].vval.v_list);
9927}
9928
9929/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009930 * "complete_add()" function
9931 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009932 static void
9933f_complete_add(argvars, rettv)
9934 typval_T *argvars;
9935 typval_T *rettv;
9936{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009937 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009938}
9939
9940/*
9941 * "complete_check()" function
9942 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009943 static void
9944f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009945 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009946 typval_T *rettv;
9947{
9948 int saved = RedrawingDisabled;
9949
9950 RedrawingDisabled = 0;
9951 ins_compl_check_keys(0);
9952 rettv->vval.v_number = compl_interrupted;
9953 RedrawingDisabled = saved;
9954}
9955#endif
9956
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957/*
9958 * "confirm(message, buttons[, default [, type]])" function
9959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009962 typval_T *argvars UNUSED;
9963 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9966 char_u *message;
9967 char_u *buttons = NULL;
9968 char_u buf[NUMBUFLEN];
9969 char_u buf2[NUMBUFLEN];
9970 int def = 1;
9971 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 char_u *typestr;
9973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 message = get_tv_string_chk(&argvars[0]);
9976 if (message == NULL)
9977 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009978 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9981 if (buttons == NULL)
9982 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009986 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9989 if (typestr == NULL)
9990 error = TRUE;
9991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 switch (TOUPPER_ASC(*typestr))
9994 {
9995 case 'E': type = VIM_ERROR; break;
9996 case 'Q': type = VIM_QUESTION; break;
9997 case 'I': type = VIM_INFO; break;
9998 case 'W': type = VIM_WARNING; break;
9999 case 'G': type = VIM_GENERIC; break;
10000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 }
10002 }
10003 }
10004 }
10005
10006 if (buttons == NULL || *buttons == NUL)
10007 buttons = (char_u *)_("&Ok");
10008
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010009 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010011 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012#endif
10013}
10014
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010015/*
10016 * "copy()" function
10017 */
10018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *argvars;
10021 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010022{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010023 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010024}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010026#ifdef FEAT_FLOAT
10027/*
10028 * "cos()" function
10029 */
10030 static void
10031f_cos(argvars, rettv)
10032 typval_T *argvars;
10033 typval_T *rettv;
10034{
10035 float_T f;
10036
10037 rettv->v_type = VAR_FLOAT;
10038 if (get_float_arg(argvars, &f) == OK)
10039 rettv->vval.v_float = cos(f);
10040 else
10041 rettv->vval.v_float = 0.0;
10042}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010043
10044/*
10045 * "cosh()" function
10046 */
10047 static void
10048f_cosh(argvars, rettv)
10049 typval_T *argvars;
10050 typval_T *rettv;
10051{
10052 float_T f;
10053
10054 rettv->v_type = VAR_FLOAT;
10055 if (get_float_arg(argvars, &f) == OK)
10056 rettv->vval.v_float = cosh(f);
10057 else
10058 rettv->vval.v_float = 0.0;
10059}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010060#endif
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010063 * "count()" function
10064 */
10065 static void
10066f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010069{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010070 long n = 0;
10071 int ic = FALSE;
10072
Bram Moolenaare9a41262005-01-15 22:18:47 +000010073 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010074 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010075 listitem_T *li;
10076 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010077 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010078
Bram Moolenaare9a41262005-01-15 22:18:47 +000010079 if ((l = argvars[0].vval.v_list) != NULL)
10080 {
10081 li = l->lv_first;
10082 if (argvars[2].v_type != VAR_UNKNOWN)
10083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 int error = FALSE;
10085
10086 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010087 if (argvars[3].v_type != VAR_UNKNOWN)
10088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 idx = get_tv_number_chk(&argvars[3], &error);
10090 if (!error)
10091 {
10092 li = list_find(l, idx);
10093 if (li == NULL)
10094 EMSGN(_(e_listidx), idx);
10095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 if (error)
10098 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010099 }
10100
10101 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010102 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 ++n;
10104 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010106 else if (argvars[0].v_type == VAR_DICT)
10107 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010108 int todo;
10109 dict_T *d;
10110 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010111
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010112 if ((d = argvars[0].vval.v_dict) != NULL)
10113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 int error = FALSE;
10115
Bram Moolenaare9a41262005-01-15 22:18:47 +000010116 if (argvars[2].v_type != VAR_UNKNOWN)
10117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 if (argvars[3].v_type != VAR_UNKNOWN)
10120 EMSG(_(e_invarg));
10121 }
10122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010123 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010125 {
10126 if (!HASHITEM_EMPTY(hi))
10127 {
10128 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010129 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010130 ++n;
10131 }
10132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010133 }
10134 }
10135 else
10136 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010137 rettv->vval.v_number = n;
10138}
10139
10140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10142 *
10143 * Checks the existence of a cscope connection.
10144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010147 typval_T *argvars UNUSED;
10148 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149{
10150#ifdef FEAT_CSCOPE
10151 int num = 0;
10152 char_u *dbpath = NULL;
10153 char_u *prepend = NULL;
10154 char_u buf[NUMBUFLEN];
10155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010156 if (argvars[0].v_type != VAR_UNKNOWN
10157 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 num = (int)get_tv_number(&argvars[0]);
10160 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 }
10164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010165 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166#endif
10167}
10168
10169/*
10170 * "cursor(lnum, col)" function
10171 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010172 * Moves the cursor to the specified line and column.
10173 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 typval_T *argvars;
10178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010181#ifdef FEAT_VIRTUALEDIT
10182 long coladd = 0;
10183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010185 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010186 if (argvars[1].v_type == VAR_UNKNOWN)
10187 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010188 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010189 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010190
Bram Moolenaar493c1782014-05-28 14:34:46 +020010191 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010192 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010193 line = pos.lnum;
10194 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010195#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010196 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010197#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010198 if (curswant >= 0)
10199 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010200 }
10201 else
10202 {
10203 line = get_tv_lnum(argvars);
10204 col = get_tv_number_chk(&argvars[1], NULL);
10205#ifdef FEAT_VIRTUALEDIT
10206 if (argvars[2].v_type != VAR_UNKNOWN)
10207 coladd = get_tv_number_chk(&argvars[2], NULL);
10208#endif
10209 }
10210 if (line < 0 || col < 0
10211#ifdef FEAT_VIRTUALEDIT
10212 || coladd < 0
10213#endif
10214 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 if (line > 0)
10217 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 if (col > 0)
10219 curwin->w_cursor.col = col - 1;
10220#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010221 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222#endif
10223
10224 /* Make sure the cursor is in a valid position. */
10225 check_cursor();
10226#ifdef FEAT_MBYTE
10227 /* Correct cursor for multi-byte character. */
10228 if (has_mbyte)
10229 mb_adjust_cursor();
10230#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010231
10232 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010233 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234}
10235
10236/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010237 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 */
10239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010241 typval_T *argvars;
10242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010244 int noref = 0;
10245
10246 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010248 if (noref < 0 || noref > 1)
10249 EMSG(_(e_invarg));
10250 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010251 {
10252 current_copyID += COPYID_INC;
10253 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255}
10256
10257/*
10258 * "delete()" function
10259 */
10260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *argvars;
10263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
10265 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269}
10270
10271/*
10272 * "did_filetype()" function
10273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010276 typval_T *argvars UNUSED;
10277 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#endif
10282}
10283
10284/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010285 * "diff_filler()" function
10286 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010289 typval_T *argvars UNUSED;
10290 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010291{
10292#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010294#endif
10295}
10296
10297/*
10298 * "diff_hlID()" function
10299 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010302 typval_T *argvars UNUSED;
10303 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010304{
10305#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010307 static linenr_T prev_lnum = 0;
10308 static int changedtick = 0;
10309 static int fnum = 0;
10310 static int change_start = 0;
10311 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010312 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010313 int filler_lines;
10314 int col;
10315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 if (lnum < 0) /* ignore type error in {lnum} arg */
10317 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010318 if (lnum != prev_lnum
10319 || changedtick != curbuf->b_changedtick
10320 || fnum != curbuf->b_fnum)
10321 {
10322 /* New line, buffer, change: need to get the values. */
10323 filler_lines = diff_check(curwin, lnum);
10324 if (filler_lines < 0)
10325 {
10326 if (filler_lines == -1)
10327 {
10328 change_start = MAXCOL;
10329 change_end = -1;
10330 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10331 hlID = HLF_ADD; /* added line */
10332 else
10333 hlID = HLF_CHD; /* changed line */
10334 }
10335 else
10336 hlID = HLF_ADD; /* added line */
10337 }
10338 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010339 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010340 prev_lnum = lnum;
10341 changedtick = curbuf->b_changedtick;
10342 fnum = curbuf->b_fnum;
10343 }
10344
10345 if (hlID == HLF_CHD || hlID == HLF_TXD)
10346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010348 if (col >= change_start && col <= change_end)
10349 hlID = HLF_TXD; /* changed text */
10350 else
10351 hlID = HLF_CHD; /* changed line */
10352 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010353 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010354#endif
10355}
10356
10357/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010358 * "empty({expr})" function
10359 */
10360 static void
10361f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010362 typval_T *argvars;
10363 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010364{
10365 int n;
10366
10367 switch (argvars[0].v_type)
10368 {
10369 case VAR_STRING:
10370 case VAR_FUNC:
10371 n = argvars[0].vval.v_string == NULL
10372 || *argvars[0].vval.v_string == NUL;
10373 break;
10374 case VAR_NUMBER:
10375 n = argvars[0].vval.v_number == 0;
10376 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010377#ifdef FEAT_FLOAT
10378 case VAR_FLOAT:
10379 n = argvars[0].vval.v_float == 0.0;
10380 break;
10381#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010382 case VAR_LIST:
10383 n = argvars[0].vval.v_list == NULL
10384 || argvars[0].vval.v_list->lv_first == NULL;
10385 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 case VAR_DICT:
10387 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010389 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010390 default:
10391 EMSG2(_(e_intern2), "f_empty()");
10392 n = 0;
10393 }
10394
10395 rettv->vval.v_number = n;
10396}
10397
10398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 * "escape({string}, {chars})" function
10400 */
10401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010403 typval_T *argvars;
10404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405{
10406 char_u buf[NUMBUFLEN];
10407
Bram Moolenaar758711c2005-02-02 23:11:38 +000010408 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10409 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
10413/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010414 * "eval()" function
10415 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010416 static void
10417f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010420{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010421 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 s = get_tv_string_chk(&argvars[0]);
10424 if (s != NULL)
10425 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010426
Bram Moolenaar615b9972015-01-14 17:15:05 +010010427 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10429 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010430 if (p != NULL && !aborting())
10431 EMSG2(_(e_invexpr2), p);
10432 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010434 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010436 else if (*s != NUL)
10437 EMSG(_(e_trailing));
10438}
10439
10440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 * "eventhandler()" function
10442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449}
10450
10451/*
10452 * "executable()" function
10453 */
10454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010456 typval_T *argvars;
10457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010459 char_u *name = get_tv_string(&argvars[0]);
10460
10461 /* Check in $PATH and also check directly if there is a directory name. */
10462 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10463 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010464}
10465
10466/*
10467 * "exepath()" function
10468 */
10469 static void
10470f_exepath(argvars, rettv)
10471 typval_T *argvars;
10472 typval_T *rettv;
10473{
10474 char_u *p = NULL;
10475
Bram Moolenaarb5971142015-03-21 17:32:19 +010010476 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010477 rettv->v_type = VAR_STRING;
10478 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
10481/*
10482 * "exists()" function
10483 */
10484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 typval_T *argvars;
10487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 char_u *p;
10490 char_u *name;
10491 int n = FALSE;
10492 int len = 0;
10493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 if (*p == '$') /* environment variable */
10496 {
10497 /* first try "normal" environment variables (fast) */
10498 if (mch_getenv(p + 1) != NULL)
10499 n = TRUE;
10500 else
10501 {
10502 /* try expanding things like $VIM and ${HOME} */
10503 p = expand_env_save(p);
10504 if (p != NULL && *p != '$')
10505 n = TRUE;
10506 vim_free(p);
10507 }
10508 }
10509 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010512 if (*skipwhite(p) != NUL)
10513 n = FALSE; /* trailing garbage */
10514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 else if (*p == '*') /* internal or user defined function */
10516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010517 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 }
10519 else if (*p == ':')
10520 {
10521 n = cmd_exists(p + 1);
10522 }
10523 else if (*p == '#')
10524 {
10525#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010526 if (p[1] == '#')
10527 n = autocmd_supported(p + 2);
10528 else
10529 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#endif
10531 }
10532 else /* internal variable */
10533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010534 char_u *tofree;
10535 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010537 /* get_name_len() takes care of expanding curly braces */
10538 name = p;
10539 len = get_name_len(&p, &tofree, TRUE, FALSE);
10540 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010542 if (tofree != NULL)
10543 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010544 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010545 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010547 /* handle d.key, l[idx], f(expr) */
10548 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10549 if (n)
10550 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 }
10552 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010553 if (*p != NUL)
10554 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 }
10558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
10561
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010562#ifdef FEAT_FLOAT
10563/*
10564 * "exp()" function
10565 */
10566 static void
10567f_exp(argvars, rettv)
10568 typval_T *argvars;
10569 typval_T *rettv;
10570{
10571 float_T f;
10572
10573 rettv->v_type = VAR_FLOAT;
10574 if (get_float_arg(argvars, &f) == OK)
10575 rettv->vval.v_float = exp(f);
10576 else
10577 rettv->vval.v_float = 0.0;
10578}
10579#endif
10580
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581/*
10582 * "expand()" function
10583 */
10584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589 char_u *s;
10590 int len;
10591 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010592 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010595 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010598 if (argvars[1].v_type != VAR_UNKNOWN
10599 && argvars[2].v_type != VAR_UNKNOWN
10600 && get_tv_number_chk(&argvars[2], &error)
10601 && !error)
10602 {
10603 rettv->v_type = VAR_LIST;
10604 rettv->vval.v_list = NULL;
10605 }
10606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 if (*s == '%' || *s == '#' || *s == '<')
10609 {
10610 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010611 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010613 if (rettv->v_type == VAR_LIST)
10614 {
10615 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10616 list_append_string(rettv->vval.v_list, result, -1);
10617 else
10618 vim_free(result);
10619 }
10620 else
10621 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 }
10623 else
10624 {
10625 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010626 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 if (argvars[1].v_type != VAR_UNKNOWN
10628 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010629 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 if (!error)
10631 {
10632 ExpandInit(&xpc);
10633 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010634 if (p_wic)
10635 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010636 if (rettv->v_type == VAR_STRING)
10637 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10638 options, WILD_ALL);
10639 else if (rettv_list_alloc(rettv) != FAIL)
10640 {
10641 int i;
10642
10643 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10644 for (i = 0; i < xpc.xp_numfiles; i++)
10645 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10646 ExpandCleanup(&xpc);
10647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 }
10649 else
10650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 }
10652}
10653
10654/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010655 * Go over all entries in "d2" and add them to "d1".
10656 * When "action" is "error" then a duplicate key is an error.
10657 * When "action" is "force" then a duplicate key is overwritten.
10658 * Otherwise duplicate keys are ignored ("action" is "keep").
10659 */
10660 void
10661dict_extend(d1, d2, action)
10662 dict_T *d1;
10663 dict_T *d2;
10664 char_u *action;
10665{
10666 dictitem_T *di1;
10667 hashitem_T *hi2;
10668 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010669 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010670
10671 todo = (int)d2->dv_hashtab.ht_used;
10672 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10673 {
10674 if (!HASHITEM_EMPTY(hi2))
10675 {
10676 --todo;
10677 di1 = dict_find(d1, hi2->hi_key, -1);
10678 if (d1->dv_scope != 0)
10679 {
10680 /* Disallow replacing a builtin function in l: and g:.
10681 * Check the key to be valid when adding to any
10682 * scope. */
10683 if (d1->dv_scope == VAR_DEF_SCOPE
10684 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10685 && var_check_func_name(hi2->hi_key,
10686 di1 == NULL))
10687 break;
10688 if (!valid_varname(hi2->hi_key))
10689 break;
10690 }
10691 if (di1 == NULL)
10692 {
10693 di1 = dictitem_copy(HI2DI(hi2));
10694 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10695 dictitem_free(di1);
10696 }
10697 else if (*action == 'e')
10698 {
10699 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10700 break;
10701 }
10702 else if (*action == 'f' && HI2DI(hi2) != di1)
10703 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010704 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10705 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010706 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010707 clear_tv(&di1->di_tv);
10708 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10709 }
10710 }
10711 }
10712}
10713
10714/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010715 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010716 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010717 */
10718 static void
10719f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010722{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010723 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010724
Bram Moolenaare9a41262005-01-15 22:18:47 +000010725 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010726 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010727 list_T *l1, *l2;
10728 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010729 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010731
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 l1 = argvars[0].vval.v_list;
10733 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010734 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010735 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010736 {
10737 if (argvars[2].v_type != VAR_UNKNOWN)
10738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 before = get_tv_number_chk(&argvars[2], &error);
10740 if (error)
10741 return; /* type error; errmsg already given */
10742
Bram Moolenaar758711c2005-02-02 23:11:38 +000010743 if (before == l1->lv_len)
10744 item = NULL;
10745 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010747 item = list_find(l1, before);
10748 if (item == NULL)
10749 {
10750 EMSGN(_(e_listidx), before);
10751 return;
10752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010753 }
10754 }
10755 else
10756 item = NULL;
10757 list_extend(l1, l2, item);
10758
Bram Moolenaare9a41262005-01-15 22:18:47 +000010759 copy_tv(&argvars[0], rettv);
10760 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010762 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10763 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010764 dict_T *d1, *d2;
10765 char_u *action;
10766 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010767
10768 d1 = argvars[0].vval.v_dict;
10769 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010770 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010771 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 {
10773 /* Check the third argument. */
10774 if (argvars[2].v_type != VAR_UNKNOWN)
10775 {
10776 static char *(av[]) = {"keep", "force", "error"};
10777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 action = get_tv_string_chk(&argvars[2]);
10779 if (action == NULL)
10780 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 for (i = 0; i < 3; ++i)
10782 if (STRCMP(action, av[i]) == 0)
10783 break;
10784 if (i == 3)
10785 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010786 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 return;
10788 }
10789 }
10790 else
10791 action = (char_u *)"force";
10792
Bram Moolenaara9922d62013-05-30 13:01:18 +020010793 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010794
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 copy_tv(&argvars[0], rettv);
10796 }
10797 }
10798 else
10799 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010800}
10801
10802/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010803 * "feedkeys()" function
10804 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010805 static void
10806f_feedkeys(argvars, rettv)
10807 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010808 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010809{
10810 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010811 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010812 char_u *keys, *flags;
10813 char_u nbuf[NUMBUFLEN];
10814 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010815 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010816
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010817 /* This is not allowed in the sandbox. If the commands would still be
10818 * executed in the sandbox it would be OK, but it probably happens later,
10819 * when "sandbox" is no longer set. */
10820 if (check_secure())
10821 return;
10822
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010823 keys = get_tv_string(&argvars[0]);
10824 if (*keys != NUL)
10825 {
10826 if (argvars[1].v_type != VAR_UNKNOWN)
10827 {
10828 flags = get_tv_string_buf(&argvars[1], nbuf);
10829 for ( ; *flags != NUL; ++flags)
10830 {
10831 switch (*flags)
10832 {
10833 case 'n': remap = FALSE; break;
10834 case 'm': remap = TRUE; break;
10835 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010836 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010837 }
10838 }
10839 }
10840
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010841 /* Need to escape K_SPECIAL and CSI before putting the string in the
10842 * typeahead buffer. */
10843 keys_esc = vim_strsave_escape_csi(keys);
10844 if (keys_esc != NULL)
10845 {
10846 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010847 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010848 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010849 if (vgetc_busy)
10850 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010851 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010852 }
10853}
10854
10855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 * "filereadable()" function
10857 */
10858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010860 typval_T *argvars;
10861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010863 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 char_u *p;
10865 int n;
10866
Bram Moolenaarc236c162008-07-13 17:41:49 +000010867#ifndef O_NONBLOCK
10868# define O_NONBLOCK 0
10869#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010871 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10872 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
10874 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010875 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 }
10877 else
10878 n = FALSE;
10879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010884 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 * rights to write into.
10886 */
10887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T *argvars;
10890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010892 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893}
10894
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010895static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010896
10897 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010898findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010899 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010900 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010901 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010902{
10903#ifdef FEAT_SEARCHPATH
10904 char_u *fname;
10905 char_u *fresult = NULL;
10906 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10907 char_u *p;
10908 char_u pathbuf[NUMBUFLEN];
10909 int count = 1;
10910 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010911 int error = FALSE;
10912#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010913
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010914 rettv->vval.v_string = NULL;
10915 rettv->v_type = VAR_STRING;
10916
10917#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010919
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010920 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10923 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010924 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 else
10926 {
10927 if (*p != NUL)
10928 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010931 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010933 }
10934
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010935 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10936 error = TRUE;
10937
10938 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 do
10941 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010942 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010943 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 fresult = find_file_in_path_option(first ? fname : NULL,
10945 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010946 0, first, path,
10947 find_what,
10948 curbuf->b_ffname,
10949 find_what == FINDFILE_DIR
10950 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010951 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010952
10953 if (fresult != NULL && rettv->v_type == VAR_LIST)
10954 list_append_string(rettv->vval.v_list, fresult, -1);
10955
10956 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010958
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010959 if (rettv->v_type == VAR_STRING)
10960 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010961#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010962}
10963
Bram Moolenaar33570922005-01-25 22:26:29 +000010964static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10965static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010966
10967/*
10968 * Implementation of map() and filter().
10969 */
10970 static void
10971filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 typval_T *argvars;
10973 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974 int map;
10975{
10976 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010977 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 listitem_T *li, *nli;
10979 list_T *l = NULL;
10980 dictitem_T *di;
10981 hashtab_T *ht;
10982 hashitem_T *hi;
10983 dict_T *d = NULL;
10984 typval_T save_val;
10985 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010987 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010988 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010989 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010990 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010991 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010992 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010993
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010995 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010996 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010997 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010998 return;
10999 }
11000 else if (argvars[0].v_type == VAR_DICT)
11001 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011002 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011003 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 return;
11005 }
11006 else
11007 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011008 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 return;
11010 }
11011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012 expr = get_tv_string_buf_chk(&argvars[1], buf);
11013 /* On type errors, the preceding call has already displayed an error
11014 * message. Avoid a misleading error message for an empty string that
11015 * was not passed as argument. */
11016 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011018 prepare_vimvar(VV_VAL, &save_val);
11019 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011020
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011021 /* We reset "did_emsg" to be able to detect whether an error
11022 * occurred during evaluation of the expression. */
11023 save_did_emsg = did_emsg;
11024 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011025
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011026 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 vimvars[VV_KEY].vv_type = VAR_STRING;
11030
11031 ht = &d->dv_hashtab;
11032 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011033 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 if (!HASHITEM_EMPTY(hi))
11037 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011038 int r;
11039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011040 --todo;
11041 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011042 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011043 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11044 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 break;
11046 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011047 r = filter_map_one(&di->di_tv, expr, map, &rem);
11048 clear_tv(&vimvars[VV_KEY].vv_tv);
11049 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 break;
11051 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011052 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011053 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11054 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011055 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 }
11059 }
11060 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 }
11062 else
11063 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011064 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 for (li = l->lv_first; li != NULL; li = nli)
11067 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011068 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011069 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011071 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011072 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011073 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011074 break;
11075 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011077 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011078 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011079 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011080
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011081 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011083
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011084 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086
11087 copy_tv(&argvars[0], rettv);
11088}
11089
11090 static int
11091filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 char_u *expr;
11094 int map;
11095 int *remp;
11096{
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011099 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011100
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102 s = expr;
11103 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011104 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 if (*s != NUL) /* check for trailing chars after expr */
11106 {
11107 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011108 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011109 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 }
11111 if (map)
11112 {
11113 /* map(): replace the list item value */
11114 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011115 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116 *tv = rettv;
11117 }
11118 else
11119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 int error = FALSE;
11121
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 /* On type error, nothing has been removed; return FAIL to stop the
11126 * loop. The error message was given by get_tv_number_chk(). */
11127 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011128 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011130 retval = OK;
11131theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011132 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011133 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011134}
11135
11136/*
11137 * "filter()" function
11138 */
11139 static void
11140f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011143{
11144 filter_map(argvars, rettv, FALSE);
11145}
11146
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011147/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011148 * "finddir({fname}[, {path}[, {count}]])" function
11149 */
11150 static void
11151f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 typval_T *argvars;
11153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011154{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011155 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156}
11157
11158/*
11159 * "findfile({fname}[, {path}[, {count}]])" function
11160 */
11161 static void
11162f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 typval_T *argvars;
11164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011166 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167}
11168
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011169#ifdef FEAT_FLOAT
11170/*
11171 * "float2nr({float})" function
11172 */
11173 static void
11174f_float2nr(argvars, rettv)
11175 typval_T *argvars;
11176 typval_T *rettv;
11177{
11178 float_T f;
11179
11180 if (get_float_arg(argvars, &f) == OK)
11181 {
11182 if (f < -0x7fffffff)
11183 rettv->vval.v_number = -0x7fffffff;
11184 else if (f > 0x7fffffff)
11185 rettv->vval.v_number = 0x7fffffff;
11186 else
11187 rettv->vval.v_number = (varnumber_T)f;
11188 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011189}
11190
11191/*
11192 * "floor({float})" function
11193 */
11194 static void
11195f_floor(argvars, rettv)
11196 typval_T *argvars;
11197 typval_T *rettv;
11198{
11199 float_T f;
11200
11201 rettv->v_type = VAR_FLOAT;
11202 if (get_float_arg(argvars, &f) == OK)
11203 rettv->vval.v_float = floor(f);
11204 else
11205 rettv->vval.v_float = 0.0;
11206}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011207
11208/*
11209 * "fmod()" function
11210 */
11211 static void
11212f_fmod(argvars, rettv)
11213 typval_T *argvars;
11214 typval_T *rettv;
11215{
11216 float_T fx, fy;
11217
11218 rettv->v_type = VAR_FLOAT;
11219 if (get_float_arg(argvars, &fx) == OK
11220 && get_float_arg(&argvars[1], &fy) == OK)
11221 rettv->vval.v_float = fmod(fx, fy);
11222 else
11223 rettv->vval.v_float = 0.0;
11224}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011225#endif
11226
Bram Moolenaar0d660222005-01-07 21:51:51 +000011227/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011228 * "fnameescape({string})" function
11229 */
11230 static void
11231f_fnameescape(argvars, rettv)
11232 typval_T *argvars;
11233 typval_T *rettv;
11234{
11235 rettv->vval.v_string = vim_strsave_fnameescape(
11236 get_tv_string(&argvars[0]), FALSE);
11237 rettv->v_type = VAR_STRING;
11238}
11239
11240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 * "fnamemodify({fname}, {mods})" function
11242 */
11243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 typval_T *argvars;
11246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
11248 char_u *fname;
11249 char_u *mods;
11250 int usedlen = 0;
11251 int len;
11252 char_u *fbuf = NULL;
11253 char_u buf[NUMBUFLEN];
11254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011255 fname = get_tv_string_chk(&argvars[0]);
11256 mods = get_tv_string_buf_chk(&argvars[1], buf);
11257 if (fname == NULL || mods == NULL)
11258 fname = NULL;
11259 else
11260 {
11261 len = (int)STRLEN(fname);
11262 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 vim_free(fbuf);
11271}
11272
Bram Moolenaar33570922005-01-25 22:26:29 +000011273static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
11275/*
11276 * "foldclosed()" function
11277 */
11278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011282 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284#ifdef FEAT_FOLDING
11285 linenr_T lnum;
11286 linenr_T first, last;
11287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11290 {
11291 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11292 {
11293 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 return;
11298 }
11299 }
11300#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302}
11303
11304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305 * "foldclosed()" function
11306 */
11307 static void
11308f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311{
11312 foldclosed_both(argvars, rettv, FALSE);
11313}
11314
11315/*
11316 * "foldclosedend()" function
11317 */
11318 static void
11319f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011322{
11323 foldclosed_both(argvars, rettv, TRUE);
11324}
11325
11326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 * "foldlevel()" function
11328 */
11329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011331 typval_T *argvars UNUSED;
11332 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
11334#ifdef FEAT_FOLDING
11335 linenr_T lnum;
11336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341}
11342
11343/*
11344 * "foldtext()" function
11345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351#ifdef FEAT_FOLDING
11352 linenr_T lnum;
11353 char_u *s;
11354 char_u *r;
11355 int len;
11356 char *txt;
11357#endif
11358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->v_type = VAR_STRING;
11360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11363 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11364 <= curbuf->b_ml.ml_line_count
11365 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 {
11367 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011368 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11369 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 {
11371 if (!linewhite(lnum))
11372 break;
11373 ++lnum;
11374 }
11375
11376 /* Find interesting text in this line. */
11377 s = skipwhite(ml_get(lnum));
11378 /* skip C comment-start */
11379 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011382 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011383 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011384 {
11385 s = skipwhite(ml_get(lnum + 1));
11386 if (*s == '*')
11387 s = skipwhite(s + 1);
11388 }
11389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 txt = _("+-%s%3ld lines: ");
11391 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011392 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 + 20 /* for %3ld */
11394 + STRLEN(s))); /* concatenated */
11395 if (r != NULL)
11396 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011397 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11398 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11399 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 len = (int)STRLEN(r);
11401 STRCAT(r, s);
11402 /* remove 'foldmarker' and 'commentstring' */
11403 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 }
11406 }
11407#endif
11408}
11409
11410/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011411 * "foldtextresult(lnum)" function
11412 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011415 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011417{
11418#ifdef FEAT_FOLDING
11419 linenr_T lnum;
11420 char_u *text;
11421 char_u buf[51];
11422 foldinfo_T foldinfo;
11423 int fold_count;
11424#endif
11425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->v_type = VAR_STRING;
11427 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011428#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 /* treat illegal types and illegal string values for {lnum} the same */
11431 if (lnum < 0)
11432 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011433 fold_count = foldedCount(curwin, lnum, &foldinfo);
11434 if (fold_count > 0)
11435 {
11436 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11437 &foldinfo, buf);
11438 if (text == buf)
11439 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011441 }
11442#endif
11443}
11444
11445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 * "foreground()" function
11447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011450 typval_T *argvars UNUSED;
11451 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453#ifdef FEAT_GUI
11454 if (gui.in_use)
11455 gui_mch_set_foreground();
11456#else
11457# ifdef WIN32
11458 win32_set_foreground();
11459# endif
11460#endif
11461}
11462
11463/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464 * "function()" function
11465 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011468 typval_T *argvars;
11469 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470{
11471 char_u *s;
11472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011474 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011476 /* Don't check an autoload name for existence here. */
11477 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011478 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 else
11480 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011481 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011482 {
11483 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011484 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011485
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011486 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11487 * also be called from another script. Using trans_function_name()
11488 * would also work, but some plugins depend on the name being
11489 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011490 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011491 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011492 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011493 if (rettv->vval.v_string != NULL)
11494 {
11495 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011496 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011497 }
11498 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011499 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011500 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011502 }
11503}
11504
11505/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011506 * "garbagecollect()" function
11507 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011508 static void
11509f_garbagecollect(argvars, rettv)
11510 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011511 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011512{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011513 /* This is postponed until we are back at the toplevel, because we may be
11514 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11515 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011516
11517 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11518 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011519}
11520
11521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 * "get()" function
11523 */
11524 static void
11525f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 typval_T *argvars;
11527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011528{
Bram Moolenaar33570922005-01-25 22:26:29 +000011529 listitem_T *li;
11530 list_T *l;
11531 dictitem_T *di;
11532 dict_T *d;
11533 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 int error = FALSE;
11540
11541 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11542 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011545 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011546 else if (argvars[0].v_type == VAR_DICT)
11547 {
11548 if ((d = argvars[0].vval.v_dict) != NULL)
11549 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011550 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011551 if (di != NULL)
11552 tv = &di->di_tv;
11553 }
11554 }
11555 else
11556 EMSG2(_(e_listdictarg), "get()");
11557
11558 if (tv == NULL)
11559 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 copy_tv(&argvars[2], rettv);
11562 }
11563 else
11564 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565}
11566
Bram Moolenaar342337a2005-07-21 21:11:17 +000011567static 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 +000011568
11569/*
11570 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 * Return a range (from start to end) of lines in rettv from the specified
11572 * buffer.
11573 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011574 */
11575 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011576get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011577 buf_T *buf;
11578 linenr_T start;
11579 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 typval_T *rettv;
11582{
11583 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011584
Bram Moolenaar959a1432013-12-14 12:17:38 +010011585 rettv->v_type = VAR_STRING;
11586 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011587 if (retlist && rettv_list_alloc(rettv) == FAIL)
11588 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011589
11590 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11591 return;
11592
11593 if (!retlist)
11594 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011595 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11596 p = ml_get_buf(buf, start, FALSE);
11597 else
11598 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011599 rettv->vval.v_string = vim_strsave(p);
11600 }
11601 else
11602 {
11603 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011604 return;
11605
11606 if (start < 1)
11607 start = 1;
11608 if (end > buf->b_ml.ml_line_count)
11609 end = buf->b_ml.ml_line_count;
11610 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011611 if (list_append_string(rettv->vval.v_list,
11612 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011613 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011614 }
11615}
11616
11617/*
11618 * "getbufline()" function
11619 */
11620 static void
11621f_getbufline(argvars, rettv)
11622 typval_T *argvars;
11623 typval_T *rettv;
11624{
11625 linenr_T lnum;
11626 linenr_T end;
11627 buf_T *buf;
11628
11629 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11630 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011631 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011632 --emsg_off;
11633
Bram Moolenaar661b1822005-07-28 22:36:45 +000011634 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011635 if (argvars[2].v_type == VAR_UNKNOWN)
11636 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011637 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011638 end = get_tv_lnum_buf(&argvars[2], buf);
11639
Bram Moolenaar342337a2005-07-21 21:11:17 +000011640 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011641}
11642
Bram Moolenaar0d660222005-01-07 21:51:51 +000011643/*
11644 * "getbufvar()" function
11645 */
11646 static void
11647f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011650{
11651 buf_T *buf;
11652 buf_T *save_curbuf;
11653 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011654 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011655 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11658 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011659 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011660 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011662 rettv->v_type = VAR_STRING;
11663 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011664
11665 if (buf != NULL && varname != NULL)
11666 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011667 /* set curbuf to be our buf, temporarily */
11668 save_curbuf = curbuf;
11669 curbuf = buf;
11670
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011672 {
11673 if (get_option_tv(&varname, rettv, TRUE) == OK)
11674 done = TRUE;
11675 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011676 else if (STRCMP(varname, "changedtick") == 0)
11677 {
11678 rettv->v_type = VAR_NUMBER;
11679 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011680 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011681 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682 else
11683 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011684 /* Look up the variable. */
11685 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11686 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11687 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011689 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011690 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011691 done = TRUE;
11692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011694
11695 /* restore previous notion of curbuf */
11696 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697 }
11698
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011699 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11700 /* use the default value */
11701 copy_tv(&argvars[2], rettv);
11702
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 --emsg_off;
11704}
11705
11706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 * "getchar()" function
11708 */
11709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011711 typval_T *argvars;
11712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713{
11714 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011717 /* Position the cursor. Needed after a message that ends in a space. */
11718 windgoto(msg_row, msg_col);
11719
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 ++no_mapping;
11721 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011722 for (;;)
11723 {
11724 if (argvars[0].v_type == VAR_UNKNOWN)
11725 /* getchar(): blocking wait. */
11726 n = safe_vgetc();
11727 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11728 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011729 n = vpeekc_any();
11730 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011731 /* illegal argument or getchar(0) and no char avail: return zero */
11732 n = 0;
11733 else
11734 /* getchar(0) and char avail: return char */
11735 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011736
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011737 if (n == K_IGNORE)
11738 continue;
11739 break;
11740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 --no_mapping;
11742 --allow_keys;
11743
Bram Moolenaar219b8702006-11-01 14:32:36 +000011744 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11745 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11746 vimvars[VV_MOUSE_COL].vv_nr = 0;
11747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 if (IS_SPECIAL(n) || mod_mask != 0)
11750 {
11751 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11752 int i = 0;
11753
11754 /* Turn a special key into three bytes, plus modifier. */
11755 if (mod_mask != 0)
11756 {
11757 temp[i++] = K_SPECIAL;
11758 temp[i++] = KS_MODIFIER;
11759 temp[i++] = mod_mask;
11760 }
11761 if (IS_SPECIAL(n))
11762 {
11763 temp[i++] = K_SPECIAL;
11764 temp[i++] = K_SECOND(n);
11765 temp[i++] = K_THIRD(n);
11766 }
11767#ifdef FEAT_MBYTE
11768 else if (has_mbyte)
11769 i += (*mb_char2bytes)(n, temp + i);
11770#endif
11771 else
11772 temp[i++] = n;
11773 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 rettv->v_type = VAR_STRING;
11775 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011776
11777#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011778 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011779 {
11780 int row = mouse_row;
11781 int col = mouse_col;
11782 win_T *win;
11783 linenr_T lnum;
11784# ifdef FEAT_WINDOWS
11785 win_T *wp;
11786# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011787 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011788
11789 if (row >= 0 && col >= 0)
11790 {
11791 /* Find the window at the mouse coordinates and compute the
11792 * text position. */
11793 win = mouse_find_win(&row, &col);
11794 (void)mouse_comp_pos(win, &row, &col, &lnum);
11795# ifdef FEAT_WINDOWS
11796 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011797 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011798# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011799 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011800 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11801 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11802 }
11803 }
11804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 }
11806}
11807
11808/*
11809 * "getcharmod()" function
11810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011813 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817}
11818
11819/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011820 * "getcharsearch()" function
11821 */
11822 static void
11823f_getcharsearch(argvars, rettv)
11824 typval_T *argvars UNUSED;
11825 typval_T *rettv;
11826{
11827 if (rettv_dict_alloc(rettv) != FAIL)
11828 {
11829 dict_T *dict = rettv->vval.v_dict;
11830
11831 dict_add_nr_str(dict, "char", 0L, last_csearch());
11832 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11833 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11834 }
11835}
11836
11837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 * "getcmdline()" function
11839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011842 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->v_type = VAR_STRING;
11846 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847}
11848
11849/*
11850 * "getcmdpos()" function
11851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858}
11859
11860/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011861 * "getcmdtype()" function
11862 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011863 static void
11864f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011865 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011866 typval_T *rettv;
11867{
11868 rettv->v_type = VAR_STRING;
11869 rettv->vval.v_string = alloc(2);
11870 if (rettv->vval.v_string != NULL)
11871 {
11872 rettv->vval.v_string[0] = get_cmdline_type();
11873 rettv->vval.v_string[1] = NUL;
11874 }
11875}
11876
11877/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011878 * "getcmdwintype()" function
11879 */
11880 static void
11881f_getcmdwintype(argvars, rettv)
11882 typval_T *argvars UNUSED;
11883 typval_T *rettv;
11884{
11885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = NULL;
11887#ifdef FEAT_CMDWIN
11888 rettv->vval.v_string = alloc(2);
11889 if (rettv->vval.v_string != NULL)
11890 {
11891 rettv->vval.v_string[0] = cmdwin_type;
11892 rettv->vval.v_string[1] = NUL;
11893 }
11894#endif
11895}
11896
11897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 * "getcwd()" function
11899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011905 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011908 rettv->vval.v_string = NULL;
11909 cwd = alloc(MAXPATHL);
11910 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011912 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11913 {
11914 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011916 if (rettv->vval.v_string != NULL)
11917 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011919 }
11920 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 }
11922}
11923
11924/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011925 * "getfontname()" function
11926 */
11927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011930 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011931{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932 rettv->v_type = VAR_STRING;
11933 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011934#ifdef FEAT_GUI
11935 if (gui.in_use)
11936 {
11937 GuiFont font;
11938 char_u *name = NULL;
11939
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011940 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011941 {
11942 /* Get the "Normal" font. Either the name saved by
11943 * hl_set_font_name() or from the font ID. */
11944 font = gui.norm_font;
11945 name = hl_get_font_name();
11946 }
11947 else
11948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011950 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11951 return;
11952 font = gui_mch_get_font(name, FALSE);
11953 if (font == NOFONT)
11954 return; /* Invalid font name, return empty string. */
11955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011958 gui_mch_free_font(font);
11959 }
11960#endif
11961}
11962
11963/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011964 * "getfperm({fname})" function
11965 */
11966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *argvars;
11969 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011970{
11971 char_u *fname;
11972 struct stat st;
11973 char_u *perm = NULL;
11974 char_u flags[] = "rwx";
11975 int i;
11976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011980 if (mch_stat((char *)fname, &st) >= 0)
11981 {
11982 perm = vim_strsave((char_u *)"---------");
11983 if (perm != NULL)
11984 {
11985 for (i = 0; i < 9; i++)
11986 {
11987 if (st.st_mode & (1 << (8 - i)))
11988 perm[i] = flags[i % 3];
11989 }
11990 }
11991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011993}
11994
11995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 * "getfsize({fname})" function
11997 */
11998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012000 typval_T *argvars;
12001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002{
12003 char_u *fname;
12004 struct stat st;
12005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009
12010 if (mch_stat((char *)fname, &st) >= 0)
12011 {
12012 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012017
12018 /* non-perfect check for overflow */
12019 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12020 rettv->vval.v_number = -2;
12021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 }
12023 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025}
12026
12027/*
12028 * "getftime({fname})" function
12029 */
12030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *argvars;
12033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034{
12035 char_u *fname;
12036 struct stat st;
12037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
12040 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044}
12045
12046/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012047 * "getftype({fname})" function
12048 */
12049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 typval_T *argvars;
12052 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012053{
12054 char_u *fname;
12055 struct stat st;
12056 char_u *type = NULL;
12057 char *t;
12058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062 if (mch_lstat((char *)fname, &st) >= 0)
12063 {
12064#ifdef S_ISREG
12065 if (S_ISREG(st.st_mode))
12066 t = "file";
12067 else if (S_ISDIR(st.st_mode))
12068 t = "dir";
12069# ifdef S_ISLNK
12070 else if (S_ISLNK(st.st_mode))
12071 t = "link";
12072# endif
12073# ifdef S_ISBLK
12074 else if (S_ISBLK(st.st_mode))
12075 t = "bdev";
12076# endif
12077# ifdef S_ISCHR
12078 else if (S_ISCHR(st.st_mode))
12079 t = "cdev";
12080# endif
12081# ifdef S_ISFIFO
12082 else if (S_ISFIFO(st.st_mode))
12083 t = "fifo";
12084# endif
12085# ifdef S_ISSOCK
12086 else if (S_ISSOCK(st.st_mode))
12087 t = "fifo";
12088# endif
12089 else
12090 t = "other";
12091#else
12092# ifdef S_IFMT
12093 switch (st.st_mode & S_IFMT)
12094 {
12095 case S_IFREG: t = "file"; break;
12096 case S_IFDIR: t = "dir"; break;
12097# ifdef S_IFLNK
12098 case S_IFLNK: t = "link"; break;
12099# endif
12100# ifdef S_IFBLK
12101 case S_IFBLK: t = "bdev"; break;
12102# endif
12103# ifdef S_IFCHR
12104 case S_IFCHR: t = "cdev"; break;
12105# endif
12106# ifdef S_IFIFO
12107 case S_IFIFO: t = "fifo"; break;
12108# endif
12109# ifdef S_IFSOCK
12110 case S_IFSOCK: t = "socket"; break;
12111# endif
12112 default: t = "other";
12113 }
12114# else
12115 if (mch_isdir(fname))
12116 t = "dir";
12117 else
12118 t = "file";
12119# endif
12120#endif
12121 type = vim_strsave((char_u *)t);
12122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012124}
12125
12126/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012127 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 */
12129 static void
12130f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133{
12134 linenr_T lnum;
12135 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012136 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012137
12138 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012139 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012140 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012141 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012142 retlist = FALSE;
12143 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012145 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012147 retlist = TRUE;
12148 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012149
Bram Moolenaar342337a2005-07-21 21:11:17 +000012150 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151}
12152
12153/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012154 * "getmatches()" function
12155 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012156 static void
12157f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012158 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012159 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012160{
12161#ifdef FEAT_SEARCH_EXTRA
12162 dict_T *dict;
12163 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012164 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012165
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012166 if (rettv_list_alloc(rettv) == OK)
12167 {
12168 while (cur != NULL)
12169 {
12170 dict = dict_alloc();
12171 if (dict == NULL)
12172 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012173 if (cur->match.regprog == NULL)
12174 {
12175 /* match added with matchaddpos() */
12176 for (i = 0; i < MAXPOSMATCH; ++i)
12177 {
12178 llpos_T *llpos;
12179 char buf[6];
12180 list_T *l;
12181
12182 llpos = &cur->pos.pos[i];
12183 if (llpos->lnum == 0)
12184 break;
12185 l = list_alloc();
12186 if (l == NULL)
12187 break;
12188 list_append_number(l, (varnumber_T)llpos->lnum);
12189 if (llpos->col > 0)
12190 {
12191 list_append_number(l, (varnumber_T)llpos->col);
12192 list_append_number(l, (varnumber_T)llpos->len);
12193 }
12194 sprintf(buf, "pos%d", i + 1);
12195 dict_add_list(dict, buf, l);
12196 }
12197 }
12198 else
12199 {
12200 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12201 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012202 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012203 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12204 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012205# ifdef FEAT_CONCEAL
12206 if (cur->conceal_char)
12207 {
12208 char_u buf[MB_MAXBYTES + 1];
12209
12210 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12211 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12212 }
12213# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012214 list_append_dict(rettv->vval.v_list, dict);
12215 cur = cur->next;
12216 }
12217 }
12218#endif
12219}
12220
12221/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012222 * "getpid()" function
12223 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012224 static void
12225f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012226 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012227 typval_T *rettv;
12228{
12229 rettv->vval.v_number = mch_get_pid();
12230}
12231
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012232static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12233
12234/*
12235 * "getcurpos()" function
12236 */
12237 static void
12238f_getcurpos(argvars, rettv)
12239 typval_T *argvars;
12240 typval_T *rettv;
12241{
12242 getpos_both(argvars, rettv, TRUE);
12243}
12244
Bram Moolenaar18081e32008-02-20 19:11:07 +000012245/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012246 * "getpos(string)" function
12247 */
12248 static void
12249f_getpos(argvars, rettv)
12250 typval_T *argvars;
12251 typval_T *rettv;
12252{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012253 getpos_both(argvars, rettv, FALSE);
12254}
12255
12256 static void
12257getpos_both(argvars, rettv, getcurpos)
12258 typval_T *argvars;
12259 typval_T *rettv;
12260 int getcurpos;
12261{
Bram Moolenaara5525202006-03-02 22:52:09 +000012262 pos_T *fp;
12263 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012264 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012265
12266 if (rettv_list_alloc(rettv) == OK)
12267 {
12268 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012269 if (getcurpos)
12270 fp = &curwin->w_cursor;
12271 else
12272 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012273 if (fnum != -1)
12274 list_append_number(l, (varnumber_T)fnum);
12275 else
12276 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012277 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12278 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012279 list_append_number(l, (fp != NULL)
12280 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012281 : (varnumber_T)0);
12282 list_append_number(l,
12283#ifdef FEAT_VIRTUALEDIT
12284 (fp != NULL) ? (varnumber_T)fp->coladd :
12285#endif
12286 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012287 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012288 list_append_number(l, curwin->w_curswant == MAXCOL ?
12289 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012290 }
12291 else
12292 rettv->vval.v_number = FALSE;
12293}
12294
12295/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012296 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012297 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012298 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012299f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012300 typval_T *argvars UNUSED;
12301 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012302{
12303#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012304 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012305#endif
12306
Bram Moolenaar2641f772005-03-25 21:58:17 +000012307#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012308 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012309 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012310 wp = NULL;
12311 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12312 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012313 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012314 if (wp == NULL)
12315 return;
12316 }
12317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012318 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012319 }
12320#endif
12321}
12322
12323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 * "getreg()" function
12325 */
12326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 typval_T *argvars;
12329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 char_u *strregname;
12332 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012333 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012334 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012335 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012337 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012339 strregname = get_tv_string_chk(&argvars[0]);
12340 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012341 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012343 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012344 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12345 return_list = get_tv_number_chk(&argvars[2], &error);
12346 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012349 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012350
12351 if (error)
12352 return;
12353
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 regname = (strregname == NULL ? '"' : *strregname);
12355 if (regname == 0)
12356 regname = '"';
12357
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012358 if (return_list)
12359 {
12360 rettv->v_type = VAR_LIST;
12361 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12362 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012363 if (rettv->vval.v_list != NULL)
12364 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012365 }
12366 else
12367 {
12368 rettv->v_type = VAR_STRING;
12369 rettv->vval.v_string = get_reg_contents(regname,
12370 arg2 ? GREG_EXPR_SRC : 0);
12371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372}
12373
12374/*
12375 * "getregtype()" function
12376 */
12377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *argvars;
12380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381{
12382 char_u *strregname;
12383 int regname;
12384 char_u buf[NUMBUFLEN + 2];
12385 long reglen = 0;
12386
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 {
12389 strregname = get_tv_string_chk(&argvars[0]);
12390 if (strregname == NULL) /* type error; errmsg already given */
12391 {
12392 rettv->v_type = VAR_STRING;
12393 rettv->vval.v_string = NULL;
12394 return;
12395 }
12396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 else
12398 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
12401 regname = (strregname == NULL ? '"' : *strregname);
12402 if (regname == 0)
12403 regname = '"';
12404
12405 buf[0] = NUL;
12406 buf[1] = NUL;
12407 switch (get_reg_type(regname, &reglen))
12408 {
12409 case MLINE: buf[0] = 'V'; break;
12410 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411 case MBLOCK:
12412 buf[0] = Ctrl_V;
12413 sprintf((char *)buf + 1, "%ld", reglen + 1);
12414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->v_type = VAR_STRING;
12417 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418}
12419
12420/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012421 * "gettabvar()" function
12422 */
12423 static void
12424f_gettabvar(argvars, rettv)
12425 typval_T *argvars;
12426 typval_T *rettv;
12427{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012428 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012429 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012430 dictitem_T *v;
12431 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012432 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012433
12434 rettv->v_type = VAR_STRING;
12435 rettv->vval.v_string = NULL;
12436
12437 varname = get_tv_string_chk(&argvars[1]);
12438 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12439 if (tp != NULL && varname != NULL)
12440 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012441 /* Set tp to be our tabpage, temporarily. Also set the window to the
12442 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012443 if (switch_win(&oldcurwin, &oldtabpage,
12444 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012445 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012446 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012447 /* look up the variable */
12448 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12449 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12450 if (v != NULL)
12451 {
12452 copy_tv(&v->di_tv, rettv);
12453 done = TRUE;
12454 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012455 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012456
12457 /* restore previous notion of curwin */
12458 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012459 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012460
12461 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012462 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012463}
12464
12465/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012466 * "gettabwinvar()" function
12467 */
12468 static void
12469f_gettabwinvar(argvars, rettv)
12470 typval_T *argvars;
12471 typval_T *rettv;
12472{
12473 getwinvar(argvars, rettv, 1);
12474}
12475
12476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 * "getwinposx()" function
12478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485#ifdef FEAT_GUI
12486 if (gui.in_use)
12487 {
12488 int x, y;
12489
12490 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 }
12493#endif
12494}
12495
12496/*
12497 * "getwinposy()" function
12498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505#ifdef FEAT_GUI
12506 if (gui.in_use)
12507 {
12508 int x, y;
12509
12510 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 }
12513#endif
12514}
12515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012516/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012517 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012518 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012519 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012520find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012521 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012522 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012523{
12524#ifdef FEAT_WINDOWS
12525 win_T *wp;
12526#endif
12527 int nr;
12528
12529 nr = get_tv_number_chk(vp, NULL);
12530
12531#ifdef FEAT_WINDOWS
12532 if (nr < 0)
12533 return NULL;
12534 if (nr == 0)
12535 return curwin;
12536
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012537 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12538 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012539 if (--nr <= 0)
12540 break;
12541 return wp;
12542#else
12543 if (nr == 0 || nr == 1)
12544 return curwin;
12545 return NULL;
12546#endif
12547}
12548
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549/*
12550 * "getwinvar()" function
12551 */
12552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 typval_T *argvars;
12555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012557 getwinvar(argvars, rettv, 0);
12558}
12559
12560/*
12561 * getwinvar() and gettabwinvar()
12562 */
12563 static void
12564getwinvar(argvars, rettv, off)
12565 typval_T *argvars;
12566 typval_T *rettv;
12567 int off; /* 1 for gettabwinvar() */
12568{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012569 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012572 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012573 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012574#ifdef FEAT_WINDOWS
12575 win_T *oldcurwin;
12576 tabpage_T *oldtabpage;
12577 int need_switch_win;
12578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012580#ifdef FEAT_WINDOWS
12581 if (off == 1)
12582 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12583 else
12584 tp = curtab;
12585#endif
12586 win = find_win_by_nr(&argvars[off], tp);
12587 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012588 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012590 rettv->v_type = VAR_STRING;
12591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592
12593 if (win != NULL && varname != NULL)
12594 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012595#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012596 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012597 * otherwise the window is not valid. Only do this when needed,
12598 * autocommands get blocked. */
12599 need_switch_win = !(tp == curtab && win == curwin);
12600 if (!need_switch_win
12601 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12602#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012603 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012604 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012606 if (get_option_tv(&varname, rettv, 1) == OK)
12607 done = TRUE;
12608 }
12609 else
12610 {
12611 /* Look up the variable. */
12612 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12613 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12614 varname, FALSE);
12615 if (v != NULL)
12616 {
12617 copy_tv(&v->di_tv, rettv);
12618 done = TRUE;
12619 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012622
Bram Moolenaarba117c22015-09-29 16:53:22 +020012623#ifdef FEAT_WINDOWS
12624 if (need_switch_win)
12625 /* restore previous notion of curwin */
12626 restore_win(oldcurwin, oldtabpage, TRUE);
12627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 }
12629
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012630 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12631 /* use the default return value */
12632 copy_tv(&argvars[off + 2], rettv);
12633
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 --emsg_off;
12635}
12636
12637/*
12638 * "glob()" function
12639 */
12640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012641f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 typval_T *argvars;
12643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012645 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012647 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012649 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012650 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012652 if (argvars[1].v_type != VAR_UNKNOWN)
12653 {
12654 if (get_tv_number_chk(&argvars[1], &error))
12655 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012657 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012658 if (get_tv_number_chk(&argvars[2], &error))
12659 {
12660 rettv->v_type = VAR_LIST;
12661 rettv->vval.v_list = NULL;
12662 }
12663 if (argvars[3].v_type != VAR_UNKNOWN
12664 && get_tv_number_chk(&argvars[3], &error))
12665 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012666 }
12667 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012668 if (!error)
12669 {
12670 ExpandInit(&xpc);
12671 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012672 if (p_wic)
12673 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012674 if (rettv->v_type == VAR_STRING)
12675 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012676 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012677 else if (rettv_list_alloc(rettv) != FAIL)
12678 {
12679 int i;
12680
12681 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12682 NULL, options, WILD_ALL_KEEP);
12683 for (i = 0; i < xpc.xp_numfiles; i++)
12684 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12685
12686 ExpandCleanup(&xpc);
12687 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012688 }
12689 else
12690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691}
12692
12693/*
12694 * "globpath()" function
12695 */
12696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 typval_T *argvars;
12699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012701 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012704 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012705 garray_T ga;
12706 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012708 /* When the optional second argument is non-zero, don't remove matches
12709 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012711 if (argvars[2].v_type != VAR_UNKNOWN)
12712 {
12713 if (get_tv_number_chk(&argvars[2], &error))
12714 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012715 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012716 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012717 if (get_tv_number_chk(&argvars[3], &error))
12718 {
12719 rettv->v_type = VAR_LIST;
12720 rettv->vval.v_list = NULL;
12721 }
12722 if (argvars[4].v_type != VAR_UNKNOWN
12723 && get_tv_number_chk(&argvars[4], &error))
12724 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012725 }
12726 }
12727 if (file != NULL && !error)
12728 {
12729 ga_init2(&ga, (int)sizeof(char_u *), 10);
12730 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12731 if (rettv->v_type == VAR_STRING)
12732 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12733 else if (rettv_list_alloc(rettv) != FAIL)
12734 for (i = 0; i < ga.ga_len; ++i)
12735 list_append_string(rettv->vval.v_list,
12736 ((char_u **)(ga.ga_data))[i], -1);
12737 ga_clear_strings(&ga);
12738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741}
12742
12743/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012744 * "glob2regpat()" function
12745 */
12746 static void
12747f_glob2regpat(argvars, rettv)
12748 typval_T *argvars;
12749 typval_T *rettv;
12750{
12751 char_u *pat = get_tv_string_chk(&argvars[0]);
12752
12753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "has()" function
12759 */
12760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765 int i;
12766 char_u *name;
12767 int n = FALSE;
12768 static char *(has_list[]) =
12769 {
12770#ifdef AMIGA
12771 "amiga",
12772# ifdef FEAT_ARP
12773 "arp",
12774# endif
12775#endif
12776#ifdef __BEOS__
12777 "beos",
12778#endif
12779#ifdef MSDOS
12780# ifdef DJGPP
12781 "dos32",
12782# else
12783 "dos16",
12784# endif
12785#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012786#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 "mac",
12788#endif
12789#if defined(MACOS_X_UNIX)
12790 "macunix",
12791#endif
12792#ifdef OS2
12793 "os2",
12794#endif
12795#ifdef __QNX__
12796 "qnx",
12797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef UNIX
12799 "unix",
12800#endif
12801#ifdef VMS
12802 "vms",
12803#endif
12804#ifdef WIN16
12805 "win16",
12806#endif
12807#ifdef WIN32
12808 "win32",
12809#endif
12810#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12811 "win32unix",
12812#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012813#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 "win64",
12815#endif
12816#ifdef EBCDIC
12817 "ebcdic",
12818#endif
12819#ifndef CASE_INSENSITIVE_FILENAME
12820 "fname_case",
12821#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012822#ifdef HAVE_ACL
12823 "acl",
12824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825#ifdef FEAT_ARABIC
12826 "arabic",
12827#endif
12828#ifdef FEAT_AUTOCMD
12829 "autocmd",
12830#endif
12831#ifdef FEAT_BEVAL
12832 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012833# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12834 "balloon_multiline",
12835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836#endif
12837#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12838 "builtin_terms",
12839# ifdef ALL_BUILTIN_TCAPS
12840 "all_builtin_terms",
12841# endif
12842#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012843#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12844 || defined(FEAT_GUI_W32) \
12845 || defined(FEAT_GUI_MOTIF))
12846 "browsefilter",
12847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848#ifdef FEAT_BYTEOFF
12849 "byte_offset",
12850#endif
12851#ifdef FEAT_CINDENT
12852 "cindent",
12853#endif
12854#ifdef FEAT_CLIENTSERVER
12855 "clientserver",
12856#endif
12857#ifdef FEAT_CLIPBOARD
12858 "clipboard",
12859#endif
12860#ifdef FEAT_CMDL_COMPL
12861 "cmdline_compl",
12862#endif
12863#ifdef FEAT_CMDHIST
12864 "cmdline_hist",
12865#endif
12866#ifdef FEAT_COMMENTS
12867 "comments",
12868#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012869#ifdef FEAT_CONCEAL
12870 "conceal",
12871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#ifdef FEAT_CRYPT
12873 "cryptv",
12874#endif
12875#ifdef FEAT_CSCOPE
12876 "cscope",
12877#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012878#ifdef FEAT_CURSORBIND
12879 "cursorbind",
12880#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012881#ifdef CURSOR_SHAPE
12882 "cursorshape",
12883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884#ifdef DEBUG
12885 "debug",
12886#endif
12887#ifdef FEAT_CON_DIALOG
12888 "dialog_con",
12889#endif
12890#ifdef FEAT_GUI_DIALOG
12891 "dialog_gui",
12892#endif
12893#ifdef FEAT_DIFF
12894 "diff",
12895#endif
12896#ifdef FEAT_DIGRAPHS
12897 "digraphs",
12898#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012899#ifdef FEAT_DIRECTX
12900 "directx",
12901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#ifdef FEAT_DND
12903 "dnd",
12904#endif
12905#ifdef FEAT_EMACS_TAGS
12906 "emacs_tags",
12907#endif
12908 "eval", /* always present, of course! */
12909#ifdef FEAT_EX_EXTRA
12910 "ex_extra",
12911#endif
12912#ifdef FEAT_SEARCH_EXTRA
12913 "extra_search",
12914#endif
12915#ifdef FEAT_FKMAP
12916 "farsi",
12917#endif
12918#ifdef FEAT_SEARCHPATH
12919 "file_in_path",
12920#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012921#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012922 "filterpipe",
12923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924#ifdef FEAT_FIND_ID
12925 "find_in_path",
12926#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012927#ifdef FEAT_FLOAT
12928 "float",
12929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930#ifdef FEAT_FOLDING
12931 "folding",
12932#endif
12933#ifdef FEAT_FOOTER
12934 "footer",
12935#endif
12936#if !defined(USE_SYSTEM) && defined(UNIX)
12937 "fork",
12938#endif
12939#ifdef FEAT_GETTEXT
12940 "gettext",
12941#endif
12942#ifdef FEAT_GUI
12943 "gui",
12944#endif
12945#ifdef FEAT_GUI_ATHENA
12946# ifdef FEAT_GUI_NEXTAW
12947 "gui_neXtaw",
12948# else
12949 "gui_athena",
12950# endif
12951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#ifdef FEAT_GUI_GTK
12953 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012956#ifdef FEAT_GUI_GNOME
12957 "gui_gnome",
12958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959#ifdef FEAT_GUI_MAC
12960 "gui_mac",
12961#endif
12962#ifdef FEAT_GUI_MOTIF
12963 "gui_motif",
12964#endif
12965#ifdef FEAT_GUI_PHOTON
12966 "gui_photon",
12967#endif
12968#ifdef FEAT_GUI_W16
12969 "gui_win16",
12970#endif
12971#ifdef FEAT_GUI_W32
12972 "gui_win32",
12973#endif
12974#ifdef FEAT_HANGULIN
12975 "hangul_input",
12976#endif
12977#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12978 "iconv",
12979#endif
12980#ifdef FEAT_INS_EXPAND
12981 "insert_expand",
12982#endif
12983#ifdef FEAT_JUMPLIST
12984 "jumplist",
12985#endif
12986#ifdef FEAT_KEYMAP
12987 "keymap",
12988#endif
12989#ifdef FEAT_LANGMAP
12990 "langmap",
12991#endif
12992#ifdef FEAT_LIBCALL
12993 "libcall",
12994#endif
12995#ifdef FEAT_LINEBREAK
12996 "linebreak",
12997#endif
12998#ifdef FEAT_LISP
12999 "lispindent",
13000#endif
13001#ifdef FEAT_LISTCMDS
13002 "listcmds",
13003#endif
13004#ifdef FEAT_LOCALMAP
13005 "localmap",
13006#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013007#ifdef FEAT_LUA
13008# ifndef DYNAMIC_LUA
13009 "lua",
13010# endif
13011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012#ifdef FEAT_MENU
13013 "menu",
13014#endif
13015#ifdef FEAT_SESSION
13016 "mksession",
13017#endif
13018#ifdef FEAT_MODIFY_FNAME
13019 "modify_fname",
13020#endif
13021#ifdef FEAT_MOUSE
13022 "mouse",
13023#endif
13024#ifdef FEAT_MOUSESHAPE
13025 "mouseshape",
13026#endif
13027#if defined(UNIX) || defined(VMS)
13028# ifdef FEAT_MOUSE_DEC
13029 "mouse_dec",
13030# endif
13031# ifdef FEAT_MOUSE_GPM
13032 "mouse_gpm",
13033# endif
13034# ifdef FEAT_MOUSE_JSB
13035 "mouse_jsbterm",
13036# endif
13037# ifdef FEAT_MOUSE_NET
13038 "mouse_netterm",
13039# endif
13040# ifdef FEAT_MOUSE_PTERM
13041 "mouse_pterm",
13042# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013043# ifdef FEAT_MOUSE_SGR
13044 "mouse_sgr",
13045# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013046# ifdef FEAT_SYSMOUSE
13047 "mouse_sysmouse",
13048# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013049# ifdef FEAT_MOUSE_URXVT
13050 "mouse_urxvt",
13051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052# ifdef FEAT_MOUSE_XTERM
13053 "mouse_xterm",
13054# endif
13055#endif
13056#ifdef FEAT_MBYTE
13057 "multi_byte",
13058#endif
13059#ifdef FEAT_MBYTE_IME
13060 "multi_byte_ime",
13061#endif
13062#ifdef FEAT_MULTI_LANG
13063 "multi_lang",
13064#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013065#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013066#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013067 "mzscheme",
13068#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070#ifdef FEAT_OLE
13071 "ole",
13072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073#ifdef FEAT_PATH_EXTRA
13074 "path_extra",
13075#endif
13076#ifdef FEAT_PERL
13077#ifndef DYNAMIC_PERL
13078 "perl",
13079#endif
13080#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013081#ifdef FEAT_PERSISTENT_UNDO
13082 "persistent_undo",
13083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_PYTHON
13085#ifndef DYNAMIC_PYTHON
13086 "python",
13087#endif
13088#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013089#ifdef FEAT_PYTHON3
13090#ifndef DYNAMIC_PYTHON3
13091 "python3",
13092#endif
13093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094#ifdef FEAT_POSTSCRIPT
13095 "postscript",
13096#endif
13097#ifdef FEAT_PRINTER
13098 "printer",
13099#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013100#ifdef FEAT_PROFILE
13101 "profile",
13102#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013103#ifdef FEAT_RELTIME
13104 "reltime",
13105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106#ifdef FEAT_QUICKFIX
13107 "quickfix",
13108#endif
13109#ifdef FEAT_RIGHTLEFT
13110 "rightleft",
13111#endif
13112#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13113 "ruby",
13114#endif
13115#ifdef FEAT_SCROLLBIND
13116 "scrollbind",
13117#endif
13118#ifdef FEAT_CMDL_INFO
13119 "showcmd",
13120 "cmdline_info",
13121#endif
13122#ifdef FEAT_SIGNS
13123 "signs",
13124#endif
13125#ifdef FEAT_SMARTINDENT
13126 "smartindent",
13127#endif
13128#ifdef FEAT_SNIFF
13129 "sniff",
13130#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013131#ifdef STARTUPTIME
13132 "startuptime",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_STL_OPT
13135 "statusline",
13136#endif
13137#ifdef FEAT_SUN_WORKSHOP
13138 "sun_workshop",
13139#endif
13140#ifdef FEAT_NETBEANS_INTG
13141 "netbeans_intg",
13142#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013143#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013144 "spell",
13145#endif
13146#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 "syntax",
13148#endif
13149#if defined(USE_SYSTEM) || !defined(UNIX)
13150 "system",
13151#endif
13152#ifdef FEAT_TAG_BINS
13153 "tag_binary",
13154#endif
13155#ifdef FEAT_TAG_OLDSTATIC
13156 "tag_old_static",
13157#endif
13158#ifdef FEAT_TAG_ANYWHITE
13159 "tag_any_white",
13160#endif
13161#ifdef FEAT_TCL
13162# ifndef DYNAMIC_TCL
13163 "tcl",
13164# endif
13165#endif
13166#ifdef TERMINFO
13167 "terminfo",
13168#endif
13169#ifdef FEAT_TERMRESPONSE
13170 "termresponse",
13171#endif
13172#ifdef FEAT_TEXTOBJ
13173 "textobjects",
13174#endif
13175#ifdef HAVE_TGETENT
13176 "tgetent",
13177#endif
13178#ifdef FEAT_TITLE
13179 "title",
13180#endif
13181#ifdef FEAT_TOOLBAR
13182 "toolbar",
13183#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013184#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13185 "unnamedplus",
13186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187#ifdef FEAT_USR_CMDS
13188 "user-commands", /* was accidentally included in 5.4 */
13189 "user_commands",
13190#endif
13191#ifdef FEAT_VIMINFO
13192 "viminfo",
13193#endif
13194#ifdef FEAT_VERTSPLIT
13195 "vertsplit",
13196#endif
13197#ifdef FEAT_VIRTUALEDIT
13198 "virtualedit",
13199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_VISUALEXTRA
13202 "visualextra",
13203#endif
13204#ifdef FEAT_VREPLACE
13205 "vreplace",
13206#endif
13207#ifdef FEAT_WILDIGN
13208 "wildignore",
13209#endif
13210#ifdef FEAT_WILDMENU
13211 "wildmenu",
13212#endif
13213#ifdef FEAT_WINDOWS
13214 "windows",
13215#endif
13216#ifdef FEAT_WAK
13217 "winaltkeys",
13218#endif
13219#ifdef FEAT_WRITEBACKUP
13220 "writebackup",
13221#endif
13222#ifdef FEAT_XIM
13223 "xim",
13224#endif
13225#ifdef FEAT_XFONTSET
13226 "xfontset",
13227#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013228#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013229 "xpm",
13230 "xpm_w32", /* for backward compatibility */
13231#else
13232# if defined(HAVE_XPM)
13233 "xpm",
13234# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236#ifdef USE_XSMP
13237 "xsmp",
13238#endif
13239#ifdef USE_XSMP_INTERACT
13240 "xsmp_interact",
13241#endif
13242#ifdef FEAT_XCLIPBOARD
13243 "xterm_clipboard",
13244#endif
13245#ifdef FEAT_XTERM_SAVE
13246 "xterm_save",
13247#endif
13248#if defined(UNIX) && defined(FEAT_X11)
13249 "X11",
13250#endif
13251 NULL
13252 };
13253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 for (i = 0; has_list[i] != NULL; ++i)
13256 if (STRICMP(name, has_list[i]) == 0)
13257 {
13258 n = TRUE;
13259 break;
13260 }
13261
13262 if (n == FALSE)
13263 {
13264 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013265 {
13266 if (name[5] == '-'
13267 && STRLEN(name) > 11
13268 && vim_isdigit(name[6])
13269 && vim_isdigit(name[8])
13270 && vim_isdigit(name[10]))
13271 {
13272 int major = atoi((char *)name + 6);
13273 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013274
13275 /* Expect "patch-9.9.01234". */
13276 n = (major < VIM_VERSION_MAJOR
13277 || (major == VIM_VERSION_MAJOR
13278 && (minor < VIM_VERSION_MINOR
13279 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013280 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013281 }
13282 else
13283 n = has_patch(atoi((char *)name + 5));
13284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 else if (STRICMP(name, "vim_starting") == 0)
13286 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013287#ifdef FEAT_MBYTE
13288 else if (STRICMP(name, "multi_byte_encoding") == 0)
13289 n = has_mbyte;
13290#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013291#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13292 else if (STRICMP(name, "balloon_multiline") == 0)
13293 n = multiline_balloon_available();
13294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295#ifdef DYNAMIC_TCL
13296 else if (STRICMP(name, "tcl") == 0)
13297 n = tcl_enabled(FALSE);
13298#endif
13299#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13300 else if (STRICMP(name, "iconv") == 0)
13301 n = iconv_enabled(FALSE);
13302#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013303#ifdef DYNAMIC_LUA
13304 else if (STRICMP(name, "lua") == 0)
13305 n = lua_enabled(FALSE);
13306#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013307#ifdef DYNAMIC_MZSCHEME
13308 else if (STRICMP(name, "mzscheme") == 0)
13309 n = mzscheme_enabled(FALSE);
13310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311#ifdef DYNAMIC_RUBY
13312 else if (STRICMP(name, "ruby") == 0)
13313 n = ruby_enabled(FALSE);
13314#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013315#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316#ifdef DYNAMIC_PYTHON
13317 else if (STRICMP(name, "python") == 0)
13318 n = python_enabled(FALSE);
13319#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013320#endif
13321#ifdef FEAT_PYTHON3
13322#ifdef DYNAMIC_PYTHON3
13323 else if (STRICMP(name, "python3") == 0)
13324 n = python3_enabled(FALSE);
13325#endif
13326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#ifdef DYNAMIC_PERL
13328 else if (STRICMP(name, "perl") == 0)
13329 n = perl_enabled(FALSE);
13330#endif
13331#ifdef FEAT_GUI
13332 else if (STRICMP(name, "gui_running") == 0)
13333 n = (gui.in_use || gui.starting);
13334# ifdef FEAT_GUI_W32
13335 else if (STRICMP(name, "gui_win32s") == 0)
13336 n = gui_is_win32s();
13337# endif
13338# ifdef FEAT_BROWSE
13339 else if (STRICMP(name, "browse") == 0)
13340 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13341# endif
13342#endif
13343#ifdef FEAT_SYN_HL
13344 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013345 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346#endif
13347#if defined(WIN3264)
13348 else if (STRICMP(name, "win95") == 0)
13349 n = mch_windows95();
13350#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013351#ifdef FEAT_NETBEANS_INTG
13352 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013353 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 }
13356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358}
13359
13360/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013361 * "has_key()" function
13362 */
13363 static void
13364f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *argvars;
13366 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013367{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013368 if (argvars[0].v_type != VAR_DICT)
13369 {
13370 EMSG(_(e_dictreq));
13371 return;
13372 }
13373 if (argvars[0].vval.v_dict == NULL)
13374 return;
13375
13376 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013377 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013378}
13379
13380/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013381 * "haslocaldir()" function
13382 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013383 static void
13384f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013385 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013386 typval_T *rettv;
13387{
13388 rettv->vval.v_number = (curwin->w_localdir != NULL);
13389}
13390
13391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 * "hasmapto()" function
13393 */
13394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 typval_T *argvars;
13397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398{
13399 char_u *name;
13400 char_u *mode;
13401 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013402 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013405 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 mode = (char_u *)"nvo";
13407 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013410 if (argvars[2].v_type != VAR_UNKNOWN)
13411 abbr = get_tv_number(&argvars[2]);
13412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
Bram Moolenaar2c932302006-03-18 21:42:09 +000013414 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418}
13419
13420/*
13421 * "histadd()" function
13422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427{
13428#ifdef FEAT_CMDHIST
13429 int histype;
13430 char_u *str;
13431 char_u buf[NUMBUFLEN];
13432#endif
13433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 if (check_restricted() || check_secure())
13436 return;
13437#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13439 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 if (histype >= 0)
13441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 if (*str != NUL)
13444 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013445 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 return;
13449 }
13450 }
13451#endif
13452}
13453
13454/*
13455 * "histdel()" function
13456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013459 typval_T *argvars UNUSED;
13460 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461{
13462#ifdef FEAT_CMDHIST
13463 int n;
13464 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13468 if (str == NULL)
13469 n = 0;
13470 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013472 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013473 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 else
13478 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 get_tv_string_buf(&argvars[1], buf));
13481 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482#endif
13483}
13484
13485/*
13486 * "histget()" function
13487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492{
13493#ifdef FEAT_CMDHIST
13494 int type;
13495 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13499 if (str == NULL)
13500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 {
13503 type = get_histtype(str);
13504 if (argvars[1].v_type == VAR_UNKNOWN)
13505 idx = get_history_idx(type);
13506 else
13507 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13508 /* -1 on type error */
13509 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
13518 * "histnr()" function
13519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013522 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
13525 int i;
13526
13527#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 char_u *history = get_tv_string_chk(&argvars[0]);
13529
13530 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 if (i >= HIST_CMD && i < HIST_COUNT)
13532 i = get_history_idx(i);
13533 else
13534#endif
13535 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537}
13538
13539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 * "highlightID(name)" function
13541 */
13542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *argvars;
13545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548}
13549
13550/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551 * "highlight_exists()" function
13552 */
13553 static void
13554f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013557{
13558 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13559}
13560
13561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 * "hostname()" function
13563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013566 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568{
13569 char_u hostname[256];
13570
13571 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->v_type = VAR_STRING;
13573 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574}
13575
13576/*
13577 * iconv() function
13578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583{
13584#ifdef FEAT_MBYTE
13585 char_u buf1[NUMBUFLEN];
13586 char_u buf2[NUMBUFLEN];
13587 char_u *from, *to, *str;
13588 vimconv_T vimconv;
13589#endif
13590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->v_type = VAR_STRING;
13592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593
13594#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 str = get_tv_string(&argvars[0]);
13596 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13597 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 vimconv.vc_type = CONV_NONE;
13599 convert_setup(&vimconv, from, to);
13600
13601 /* If the encodings are equal, no conversion needed. */
13602 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
13607 convert_setup(&vimconv, NULL, NULL);
13608 vim_free(from);
13609 vim_free(to);
13610#endif
13611}
13612
13613/*
13614 * "indent()" function
13615 */
13616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *argvars;
13619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620{
13621 linenr_T lnum;
13622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013630/*
13631 * "index()" function
13632 */
13633 static void
13634f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013637{
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 list_T *l;
13639 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013640 long idx = 0;
13641 int ic = FALSE;
13642
13643 rettv->vval.v_number = -1;
13644 if (argvars[0].v_type != VAR_LIST)
13645 {
13646 EMSG(_(e_listreq));
13647 return;
13648 }
13649 l = argvars[0].vval.v_list;
13650 if (l != NULL)
13651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013652 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 int error = FALSE;
13656
Bram Moolenaar758711c2005-02-02 23:11:38 +000013657 /* Start at specified item. Use the cached index that list_find()
13658 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013660 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013661 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 ic = get_tv_number_chk(&argvars[3], &error);
13663 if (error)
13664 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013665 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013666
Bram Moolenaar758711c2005-02-02 23:11:38 +000013667 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013668 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013669 {
13670 rettv->vval.v_number = idx;
13671 break;
13672 }
13673 }
13674}
13675
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676static int inputsecret_flag = 0;
13677
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013678static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13679
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013681 * This function is used by f_input() and f_inputdialog() functions. The third
13682 * argument to f_input() specifies the type of completion to use at the
13683 * prompt. The third argument to f_inputdialog() specifies the value to return
13684 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 */
13686 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013687get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013690 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013692 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 char_u *p = NULL;
13694 int c;
13695 char_u buf[NUMBUFLEN];
13696 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013698 int xp_type = EXPAND_NOTHING;
13699 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703
13704#ifdef NO_CONSOLE_INPUT
13705 /* While starting up, there is no place to enter text. */
13706 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#endif
13709
13710 cmd_silent = FALSE; /* Want to see the prompt. */
13711 if (prompt != NULL)
13712 {
13713 /* Only the part of the message after the last NL is considered as
13714 * prompt for the command line */
13715 p = vim_strrchr(prompt, '\n');
13716 if (p == NULL)
13717 p = prompt;
13718 else
13719 {
13720 ++p;
13721 c = *p;
13722 *p = NUL;
13723 msg_start();
13724 msg_clr_eos();
13725 msg_puts_attr(prompt, echo_attr);
13726 msg_didout = FALSE;
13727 msg_starthere();
13728 *p = c;
13729 }
13730 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013732 if (argvars[1].v_type != VAR_UNKNOWN)
13733 {
13734 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13735 if (defstr != NULL)
13736 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013738 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013739 {
13740 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013741 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013742 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013743
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013744 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013745 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013746
Bram Moolenaar4463f292005-09-25 22:20:24 +000013747 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13748 if (xp_name == NULL)
13749 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013750
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013751 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013752
Bram Moolenaar4463f292005-09-25 22:20:24 +000013753 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13754 &xp_arg) == FAIL)
13755 return;
13756 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013757 }
13758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013759 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013760 {
13761# ifdef FEAT_EX_EXTRA
13762 int save_ex_normal_busy = ex_normal_busy;
13763 ex_normal_busy = 0;
13764# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013766 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13767 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013768# ifdef FEAT_EX_EXTRA
13769 ex_normal_busy = save_ex_normal_busy;
13770# endif
13771 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013772 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013773 && argvars[1].v_type != VAR_UNKNOWN
13774 && argvars[2].v_type != VAR_UNKNOWN)
13775 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13776 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013777
13778 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 /* since the user typed this, no need to wait for return */
13781 need_wait_return = FALSE;
13782 msg_didout = FALSE;
13783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 cmd_silent = cmd_silent_save;
13785}
13786
13787/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013788 * "input()" function
13789 * Also handles inputsecret() when inputsecret is set.
13790 */
13791 static void
13792f_input(argvars, rettv)
13793 typval_T *argvars;
13794 typval_T *rettv;
13795{
13796 get_user_input(argvars, rettv, FALSE);
13797}
13798
13799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 * "inputdialog()" function
13801 */
13802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 typval_T *argvars;
13805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806{
13807#if defined(FEAT_GUI_TEXTDIALOG)
13808 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13809 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13810 {
13811 char_u *message;
13812 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 message = get_tv_string_chk(&argvars[0]);
13816 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013817 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013818 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 else
13820 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 if (message != NULL && defstr != NULL
13822 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013823 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 else
13826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 if (message != NULL && defstr != NULL
13828 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013829 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_string = vim_strsave(
13831 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 }
13837 else
13838#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013839 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013842/*
13843 * "inputlist()" function
13844 */
13845 static void
13846f_inputlist(argvars, rettv)
13847 typval_T *argvars;
13848 typval_T *rettv;
13849{
13850 listitem_T *li;
13851 int selected;
13852 int mouse_used;
13853
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013854#ifdef NO_CONSOLE_INPUT
13855 /* While starting up, there is no place to enter text. */
13856 if (no_console_input())
13857 return;
13858#endif
13859 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13860 {
13861 EMSG2(_(e_listarg), "inputlist()");
13862 return;
13863 }
13864
13865 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013866 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013867 lines_left = Rows; /* avoid more prompt */
13868 msg_scroll = TRUE;
13869 msg_clr_eos();
13870
13871 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13872 {
13873 msg_puts(get_tv_string(&li->li_tv));
13874 msg_putchar('\n');
13875 }
13876
13877 /* Ask for choice. */
13878 selected = prompt_for_number(&mouse_used);
13879 if (mouse_used)
13880 selected -= lines_left;
13881
13882 rettv->vval.v_number = selected;
13883}
13884
13885
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13887
13888/*
13889 * "inputrestore()" function
13890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013893 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895{
13896 if (ga_userinput.ga_len > 0)
13897 {
13898 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13900 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013901 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 }
13903 else if (p_verbose > 1)
13904 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013905 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 }
13908}
13909
13910/*
13911 * "inputsave()" function
13912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013918 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 if (ga_grow(&ga_userinput, 1) == OK)
13920 {
13921 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13922 + ga_userinput.ga_len);
13923 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013924 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 }
13926 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928}
13929
13930/*
13931 * "inputsecret()" function
13932 */
13933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013935 typval_T *argvars;
13936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
13938 ++cmdline_star;
13939 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 --cmdline_star;
13942 --inputsecret_flag;
13943}
13944
13945/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013946 * "insert()" function
13947 */
13948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 typval_T *argvars;
13951 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013952{
13953 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013954 listitem_T *item;
13955 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013957
13958 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013960 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013961 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013962 {
13963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013964 before = get_tv_number_chk(&argvars[2], &error);
13965 if (error)
13966 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013967
Bram Moolenaar758711c2005-02-02 23:11:38 +000013968 if (before == l->lv_len)
13969 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013970 else
13971 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013972 item = list_find(l, before);
13973 if (item == NULL)
13974 {
13975 EMSGN(_(e_listidx), before);
13976 l = NULL;
13977 }
13978 }
13979 if (l != NULL)
13980 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013981 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013982 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013983 }
13984 }
13985}
13986
13987/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013988 * "invert(expr)" function
13989 */
13990 static void
13991f_invert(argvars, rettv)
13992 typval_T *argvars;
13993 typval_T *rettv;
13994{
13995 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13996}
13997
13998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 * "isdirectory()" function
14000 */
14001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014002f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014003 typval_T *argvars;
14004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007}
14008
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014009/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014010 * "islocked()" function
14011 */
14012 static void
14013f_islocked(argvars, rettv)
14014 typval_T *argvars;
14015 typval_T *rettv;
14016{
14017 lval_T lv;
14018 char_u *end;
14019 dictitem_T *di;
14020
14021 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014022 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14023 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014024 if (end != NULL && lv.ll_name != NULL)
14025 {
14026 if (*end != NUL)
14027 EMSG(_(e_trailing));
14028 else
14029 {
14030 if (lv.ll_tv == NULL)
14031 {
14032 if (check_changedtick(lv.ll_name))
14033 rettv->vval.v_number = 1; /* always locked */
14034 else
14035 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014036 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014037 if (di != NULL)
14038 {
14039 /* Consider a variable locked when:
14040 * 1. the variable itself is locked
14041 * 2. the value of the variable is locked.
14042 * 3. the List or Dict value is locked.
14043 */
14044 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14045 || tv_islocked(&di->di_tv));
14046 }
14047 }
14048 }
14049 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014050 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014051 else if (lv.ll_newkey != NULL)
14052 EMSG2(_(e_dictkey), lv.ll_newkey);
14053 else if (lv.ll_list != NULL)
14054 /* List item. */
14055 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14056 else
14057 /* Dictionary item. */
14058 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14059 }
14060 }
14061
14062 clear_lval(&lv);
14063}
14064
Bram Moolenaar33570922005-01-25 22:26:29 +000014065static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014066
14067/*
14068 * Turn a dict into a list:
14069 * "what" == 0: list of keys
14070 * "what" == 1: list of values
14071 * "what" == 2: list of items
14072 */
14073 static void
14074dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014077 int what;
14078{
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 list_T *l2;
14080 dictitem_T *di;
14081 hashitem_T *hi;
14082 listitem_T *li;
14083 listitem_T *li2;
14084 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014085 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014086
Bram Moolenaar8c711452005-01-14 21:53:12 +000014087 if (argvars[0].v_type != VAR_DICT)
14088 {
14089 EMSG(_(e_dictreq));
14090 return;
14091 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014092 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014093 return;
14094
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014095 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014096 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014097
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014098 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014101 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014103 --todo;
14104 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014106 li = listitem_alloc();
14107 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014109 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014110
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014111 if (what == 0)
14112 {
14113 /* keys() */
14114 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014115 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014116 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14117 }
14118 else if (what == 1)
14119 {
14120 /* values() */
14121 copy_tv(&di->di_tv, &li->li_tv);
14122 }
14123 else
14124 {
14125 /* items() */
14126 l2 = list_alloc();
14127 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014128 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014129 li->li_tv.vval.v_list = l2;
14130 if (l2 == NULL)
14131 break;
14132 ++l2->lv_refcount;
14133
14134 li2 = listitem_alloc();
14135 if (li2 == NULL)
14136 break;
14137 list_append(l2, li2);
14138 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014139 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014140 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14141
14142 li2 = listitem_alloc();
14143 if (li2 == NULL)
14144 break;
14145 list_append(l2, li2);
14146 copy_tv(&di->di_tv, &li2->li_tv);
14147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014148 }
14149 }
14150}
14151
14152/*
14153 * "items(dict)" function
14154 */
14155 static void
14156f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014157 typval_T *argvars;
14158 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014159{
14160 dict_list(argvars, rettv, 2);
14161}
14162
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014164 * "join()" function
14165 */
14166 static void
14167f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014168 typval_T *argvars;
14169 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014170{
14171 garray_T ga;
14172 char_u *sep;
14173
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014174 if (argvars[0].v_type != VAR_LIST)
14175 {
14176 EMSG(_(e_listreq));
14177 return;
14178 }
14179 if (argvars[0].vval.v_list == NULL)
14180 return;
14181 if (argvars[1].v_type == VAR_UNKNOWN)
14182 sep = (char_u *)" ";
14183 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014185
14186 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187
14188 if (sep != NULL)
14189 {
14190 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014191 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 ga_append(&ga, NUL);
14193 rettv->vval.v_string = (char_u *)ga.ga_data;
14194 }
14195 else
14196 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197}
14198
14199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200 * "keys()" function
14201 */
14202 static void
14203f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014204 typval_T *argvars;
14205 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014206{
14207 dict_list(argvars, rettv, 0);
14208}
14209
14210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 * "last_buffer_nr()" function.
14212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014215 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217{
14218 int n = 0;
14219 buf_T *buf;
14220
14221 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14222 if (n < buf->b_fnum)
14223 n = buf->b_fnum;
14224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014226}
14227
14228/*
14229 * "len()" function
14230 */
14231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014232f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014233 typval_T *argvars;
14234 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014235{
14236 switch (argvars[0].v_type)
14237 {
14238 case VAR_STRING:
14239 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = (varnumber_T)STRLEN(
14241 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014242 break;
14243 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014245 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246 case VAR_DICT:
14247 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14248 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014249 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014250 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014251 break;
14252 }
14253}
14254
Bram Moolenaar33570922005-01-25 22:26:29 +000014255static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014256
14257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014259 typval_T *argvars;
14260 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014261 int type;
14262{
14263#ifdef FEAT_LIBCALL
14264 char_u *string_in;
14265 char_u **string_result;
14266 int nr_result;
14267#endif
14268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014269 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014270 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014272
14273 if (check_restricted() || check_secure())
14274 return;
14275
14276#ifdef FEAT_LIBCALL
14277 /* The first two args must be strings, otherwise its meaningless */
14278 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14279 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014280 string_in = NULL;
14281 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014282 string_in = argvars[2].vval.v_string;
14283 if (type == VAR_NUMBER)
14284 string_result = NULL;
14285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 if (mch_libcall(argvars[0].vval.v_string,
14288 argvars[1].vval.v_string,
14289 string_in,
14290 argvars[2].vval.v_number,
14291 string_result,
14292 &nr_result) == OK
14293 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295 }
14296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297}
14298
14299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014300 * "libcall()" function
14301 */
14302 static void
14303f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014304 typval_T *argvars;
14305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014306{
14307 libcall_common(argvars, rettv, VAR_STRING);
14308}
14309
14310/*
14311 * "libcallnr()" function
14312 */
14313 static void
14314f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 typval_T *argvars;
14316 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317{
14318 libcall_common(argvars, rettv, VAR_NUMBER);
14319}
14320
14321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 * "line(string)" function
14323 */
14324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014326 typval_T *argvars;
14327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328{
14329 linenr_T lnum = 0;
14330 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014331 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014333 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 if (fp != NULL)
14335 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337}
14338
14339/*
14340 * "line2byte(lnum)" function
14341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346{
14347#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349#else
14350 linenr_T lnum;
14351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14357 if (rettv->vval.v_number >= 0)
14358 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359#endif
14360}
14361
14362/*
14363 * "lispindent(lnum)" function
14364 */
14365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369{
14370#ifdef FEAT_LISP
14371 pos_T pos;
14372 linenr_T lnum;
14373
14374 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14377 {
14378 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 curwin->w_cursor = pos;
14381 }
14382 else
14383#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385}
14386
14387/*
14388 * "localtime()" function
14389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014392 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396}
14397
Bram Moolenaar33570922005-01-25 22:26:29 +000014398static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
14400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014402 typval_T *argvars;
14403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 int exact;
14405{
14406 char_u *keys;
14407 char_u *which;
14408 char_u buf[NUMBUFLEN];
14409 char_u *keys_buf = NULL;
14410 char_u *rhs;
14411 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014412 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014413 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014414 mapblock_T *mp;
14415 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416
14417 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->v_type = VAR_STRING;
14419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 if (*keys == NUL)
14423 return;
14424
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014425 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014429 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014430 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014431 if (argvars[3].v_type != VAR_UNKNOWN)
14432 get_dict = get_tv_number(&argvars[3]);
14433 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 else
14436 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 if (which == NULL)
14438 return;
14439
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 mode = get_map_mode(&which, 0);
14441
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014442 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014443 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014445
14446 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014448 /* Return a string. */
14449 if (rhs != NULL)
14450 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451
Bram Moolenaarbd743252010-10-20 21:23:33 +020014452 }
14453 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14454 {
14455 /* Return a dictionary. */
14456 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14457 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14458 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
Bram Moolenaarbd743252010-10-20 21:23:33 +020014460 dict_add_nr_str(dict, "lhs", 0L, lhs);
14461 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14462 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14463 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14464 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14465 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14466 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014467 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014468 dict_add_nr_str(dict, "mode", 0L, mapmode);
14469
14470 vim_free(lhs);
14471 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 }
14473}
14474
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014475#ifdef FEAT_FLOAT
14476/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014477 * "log()" function
14478 */
14479 static void
14480f_log(argvars, rettv)
14481 typval_T *argvars;
14482 typval_T *rettv;
14483{
14484 float_T f;
14485
14486 rettv->v_type = VAR_FLOAT;
14487 if (get_float_arg(argvars, &f) == OK)
14488 rettv->vval.v_float = log(f);
14489 else
14490 rettv->vval.v_float = 0.0;
14491}
14492
14493/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014494 * "log10()" function
14495 */
14496 static void
14497f_log10(argvars, rettv)
14498 typval_T *argvars;
14499 typval_T *rettv;
14500{
14501 float_T f;
14502
14503 rettv->v_type = VAR_FLOAT;
14504 if (get_float_arg(argvars, &f) == OK)
14505 rettv->vval.v_float = log10(f);
14506 else
14507 rettv->vval.v_float = 0.0;
14508}
14509#endif
14510
Bram Moolenaar1dced572012-04-05 16:54:08 +020014511#ifdef FEAT_LUA
14512/*
14513 * "luaeval()" function
14514 */
14515 static void
14516f_luaeval(argvars, rettv)
14517 typval_T *argvars;
14518 typval_T *rettv;
14519{
14520 char_u *str;
14521 char_u buf[NUMBUFLEN];
14522
14523 str = get_tv_string_buf(&argvars[0], buf);
14524 do_luaeval(str, argvars + 1, rettv);
14525}
14526#endif
14527
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014529 * "map()" function
14530 */
14531 static void
14532f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014533 typval_T *argvars;
14534 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535{
14536 filter_map(argvars, rettv, TRUE);
14537}
14538
14539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 */
14542 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014543f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *argvars;
14545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014547 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548}
14549
14550/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 */
14553 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014555 typval_T *argvars;
14556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014558 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
Bram Moolenaar33570922005-01-25 22:26:29 +000014561static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
14563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014565 typval_T *argvars;
14566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 int type;
14568{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014570 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014571 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 char_u *pat;
14573 regmatch_T regmatch;
14574 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 char_u *save_cpo;
14577 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014578 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014579 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014580 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 list_T *l = NULL;
14582 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014584 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585
14586 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14587 save_cpo = p_cpo;
14588 p_cpo = (char_u *)"";
14589
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014590 rettv->vval.v_number = -1;
14591 if (type == 3)
14592 {
14593 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014594 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014595 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014596 }
14597 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014599 rettv->v_type = VAR_STRING;
14600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014603 if (argvars[0].v_type == VAR_LIST)
14604 {
14605 if ((l = argvars[0].vval.v_list) == NULL)
14606 goto theend;
14607 li = l->lv_first;
14608 }
14609 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014610 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014611 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014612 len = (long)STRLEN(str);
14613 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14616 if (pat == NULL)
14617 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014618
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 int error = FALSE;
14622
14623 start = get_tv_number_chk(&argvars[2], &error);
14624 if (error)
14625 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014626 if (l != NULL)
14627 {
14628 li = list_find(l, start);
14629 if (li == NULL)
14630 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014631 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014632 }
14633 else
14634 {
14635 if (start < 0)
14636 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014637 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014638 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014639 /* When "count" argument is there ignore matches before "start",
14640 * otherwise skip part of the string. Differs when pattern is "^"
14641 * or "\<". */
14642 if (argvars[3].v_type != VAR_UNKNOWN)
14643 startcol = start;
14644 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014645 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014646 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014647 len -= start;
14648 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014649 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014650
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 nth = get_tv_number_chk(&argvars[3], &error);
14653 if (error)
14654 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 }
14656
14657 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14658 if (regmatch.regprog != NULL)
14659 {
14660 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014661
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014662 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014663 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014664 if (l != NULL)
14665 {
14666 if (li == NULL)
14667 {
14668 match = FALSE;
14669 break;
14670 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014672 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014673 if (str == NULL)
14674 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014675 }
14676
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014677 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014679 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014680 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014681 if (l == NULL && !match)
14682 break;
14683
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014684 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014685 if (l != NULL)
14686 {
14687 li = li->li_next;
14688 ++idx;
14689 }
14690 else
14691 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014692#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014693 startcol = (colnr_T)(regmatch.startp[0]
14694 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014695#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014696 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014697#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014698 if (startcol > (colnr_T)len
14699 || str + startcol <= regmatch.startp[0])
14700 {
14701 match = FALSE;
14702 break;
14703 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014704 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014705 }
14706
14707 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014710 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711 int i;
14712
14713 /* return list with matched string and submatches */
14714 for (i = 0; i < NSUBEXP; ++i)
14715 {
14716 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014717 {
14718 if (list_append_string(rettv->vval.v_list,
14719 (char_u *)"", 0) == FAIL)
14720 break;
14721 }
14722 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014723 regmatch.startp[i],
14724 (int)(regmatch.endp[i] - regmatch.startp[i]))
14725 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 }
14728 }
14729 else if (type == 2)
14730 {
14731 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014732 if (l != NULL)
14733 copy_tv(&li->li_tv, rettv);
14734 else
14735 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014737 }
14738 else if (l != NULL)
14739 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 else
14741 {
14742 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 (varnumber_T)(regmatch.startp[0] - str);
14745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014748 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 }
14750 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014751 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 }
14753
14754theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 p_cpo = save_cpo;
14757}
14758
14759/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760 * "match()" function
14761 */
14762 static void
14763f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 typval_T *argvars;
14765 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766{
14767 find_some_match(argvars, rettv, 1);
14768}
14769
14770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014771 * "matchadd()" function
14772 */
14773 static void
14774f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014775 typval_T *argvars UNUSED;
14776 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014777{
14778#ifdef FEAT_SEARCH_EXTRA
14779 char_u buf[NUMBUFLEN];
14780 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14781 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14782 int prio = 10; /* default priority */
14783 int id = -1;
14784 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014785 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014786
14787 rettv->vval.v_number = -1;
14788
14789 if (grp == NULL || pat == NULL)
14790 return;
14791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014792 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014793 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014794 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014795 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014796 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014797 if (argvars[4].v_type != VAR_UNKNOWN)
14798 {
14799 if (argvars[4].v_type != VAR_DICT)
14800 {
14801 EMSG(_(e_dictreq));
14802 return;
14803 }
14804 if (dict_find(argvars[4].vval.v_dict,
14805 (char_u *)"conceal", -1) != NULL)
14806 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14807 (char_u *)"conceal", FALSE);
14808 }
14809 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014810 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014811 if (error == TRUE)
14812 return;
14813 if (id >= 1 && id <= 3)
14814 {
14815 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14816 return;
14817 }
14818
Bram Moolenaar6561d522015-07-21 15:48:27 +020014819 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14820 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014821#endif
14822}
14823
14824/*
14825 * "matchaddpos()" function
14826 */
14827 static void
14828f_matchaddpos(argvars, rettv)
14829 typval_T *argvars UNUSED;
14830 typval_T *rettv UNUSED;
14831{
14832#ifdef FEAT_SEARCH_EXTRA
14833 char_u buf[NUMBUFLEN];
14834 char_u *group;
14835 int prio = 10;
14836 int id = -1;
14837 int error = FALSE;
14838 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014839 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014840
14841 rettv->vval.v_number = -1;
14842
14843 group = get_tv_string_buf_chk(&argvars[0], buf);
14844 if (group == NULL)
14845 return;
14846
14847 if (argvars[1].v_type != VAR_LIST)
14848 {
14849 EMSG2(_(e_listarg), "matchaddpos()");
14850 return;
14851 }
14852 l = argvars[1].vval.v_list;
14853 if (l == NULL)
14854 return;
14855
14856 if (argvars[2].v_type != VAR_UNKNOWN)
14857 {
14858 prio = get_tv_number_chk(&argvars[2], &error);
14859 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014860 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014861 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014862 if (argvars[4].v_type != VAR_UNKNOWN)
14863 {
14864 if (argvars[4].v_type != VAR_DICT)
14865 {
14866 EMSG(_(e_dictreq));
14867 return;
14868 }
14869 if (dict_find(argvars[4].vval.v_dict,
14870 (char_u *)"conceal", -1) != NULL)
14871 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14872 (char_u *)"conceal", FALSE);
14873 }
14874 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014875 }
14876 if (error == TRUE)
14877 return;
14878
14879 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14880 if (id == 1 || id == 2)
14881 {
14882 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14883 return;
14884 }
14885
Bram Moolenaar6561d522015-07-21 15:48:27 +020014886 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14887 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014888#endif
14889}
14890
14891/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014892 * "matcharg()" function
14893 */
14894 static void
14895f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014896 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014897 typval_T *rettv;
14898{
14899 if (rettv_list_alloc(rettv) == OK)
14900 {
14901#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902 int id = get_tv_number(&argvars[0]);
14903 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014904
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014905 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014906 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014907 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14908 {
14909 list_append_string(rettv->vval.v_list,
14910 syn_id2name(m->hlg_id), -1);
14911 list_append_string(rettv->vval.v_list, m->pattern, -1);
14912 }
14913 else
14914 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014915 list_append_string(rettv->vval.v_list, NULL, -1);
14916 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014917 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014918 }
14919#endif
14920 }
14921}
14922
14923/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014924 * "matchdelete()" function
14925 */
14926 static void
14927f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014928 typval_T *argvars UNUSED;
14929 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014930{
14931#ifdef FEAT_SEARCH_EXTRA
14932 rettv->vval.v_number = match_delete(curwin,
14933 (int)get_tv_number(&argvars[0]), TRUE);
14934#endif
14935}
14936
14937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 * "matchend()" function
14939 */
14940 static void
14941f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 typval_T *argvars;
14943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944{
14945 find_some_match(argvars, rettv, 0);
14946}
14947
14948/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014949 * "matchlist()" function
14950 */
14951 static void
14952f_matchlist(argvars, rettv)
14953 typval_T *argvars;
14954 typval_T *rettv;
14955{
14956 find_some_match(argvars, rettv, 3);
14957}
14958
14959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 * "matchstr()" function
14961 */
14962 static void
14963f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 typval_T *argvars;
14965 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966{
14967 find_some_match(argvars, rettv, 2);
14968}
14969
Bram Moolenaar33570922005-01-25 22:26:29 +000014970static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014971
14972 static void
14973max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014974 typval_T *argvars;
14975 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014976 int domax;
14977{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014978 long n = 0;
14979 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014980 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014981
14982 if (argvars[0].v_type == VAR_LIST)
14983 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014984 list_T *l;
14985 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014986
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014987 l = argvars[0].vval.v_list;
14988 if (l != NULL)
14989 {
14990 li = l->lv_first;
14991 if (li != NULL)
14992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014993 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014994 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014995 {
14996 li = li->li_next;
14997 if (li == NULL)
14998 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015000 if (domax ? i > n : i < n)
15001 n = i;
15002 }
15003 }
15004 }
15005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015006 else if (argvars[0].v_type == VAR_DICT)
15007 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015008 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015009 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015011 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015012
15013 d = argvars[0].vval.v_dict;
15014 if (d != NULL)
15015 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015016 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015017 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015019 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015021 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015023 if (first)
15024 {
15025 n = i;
15026 first = FALSE;
15027 }
15028 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015029 n = i;
15030 }
15031 }
15032 }
15033 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015034 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015035 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015037}
15038
15039/*
15040 * "max()" function
15041 */
15042 static void
15043f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015044 typval_T *argvars;
15045 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015046{
15047 max_min(argvars, rettv, TRUE);
15048}
15049
15050/*
15051 * "min()" function
15052 */
15053 static void
15054f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015055 typval_T *argvars;
15056 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015057{
15058 max_min(argvars, rettv, FALSE);
15059}
15060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015061static int mkdir_recurse __ARGS((char_u *dir, int prot));
15062
15063/*
15064 * Create the directory in which "dir" is located, and higher levels when
15065 * needed.
15066 */
15067 static int
15068mkdir_recurse(dir, prot)
15069 char_u *dir;
15070 int prot;
15071{
15072 char_u *p;
15073 char_u *updir;
15074 int r = FAIL;
15075
15076 /* Get end of directory name in "dir".
15077 * We're done when it's "/" or "c:/". */
15078 p = gettail_sep(dir);
15079 if (p <= get_past_head(dir))
15080 return OK;
15081
15082 /* If the directory exists we're done. Otherwise: create it.*/
15083 updir = vim_strnsave(dir, (int)(p - dir));
15084 if (updir == NULL)
15085 return FAIL;
15086 if (mch_isdir(updir))
15087 r = OK;
15088 else if (mkdir_recurse(updir, prot) == OK)
15089 r = vim_mkdir_emsg(updir, prot);
15090 vim_free(updir);
15091 return r;
15092}
15093
15094#ifdef vim_mkdir
15095/*
15096 * "mkdir()" function
15097 */
15098 static void
15099f_mkdir(argvars, rettv)
15100 typval_T *argvars;
15101 typval_T *rettv;
15102{
15103 char_u *dir;
15104 char_u buf[NUMBUFLEN];
15105 int prot = 0755;
15106
15107 rettv->vval.v_number = FAIL;
15108 if (check_restricted() || check_secure())
15109 return;
15110
15111 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015112 if (*dir == NUL)
15113 rettv->vval.v_number = FAIL;
15114 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015115 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015116 if (*gettail(dir) == NUL)
15117 /* remove trailing slashes */
15118 *gettail_sep(dir) = NUL;
15119
15120 if (argvars[1].v_type != VAR_UNKNOWN)
15121 {
15122 if (argvars[2].v_type != VAR_UNKNOWN)
15123 prot = get_tv_number_chk(&argvars[2], NULL);
15124 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15125 mkdir_recurse(dir, prot);
15126 }
15127 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015128 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015129}
15130#endif
15131
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 * "mode()" function
15134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 typval_T *argvars;
15138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015140 char_u buf[3];
15141
15142 buf[1] = NUL;
15143 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 if (VIsual_active)
15146 {
15147 if (VIsual_select)
15148 buf[0] = VIsual_mode + 's' - 'v';
15149 else
15150 buf[0] = VIsual_mode;
15151 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015152 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015153 || State == CONFIRM)
15154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015156 if (State == ASKMORE)
15157 buf[1] = 'm';
15158 else if (State == CONFIRM)
15159 buf[1] = '?';
15160 }
15161 else if (State == EXTERNCMD)
15162 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 else if (State & INSERT)
15164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015165#ifdef FEAT_VREPLACE
15166 if (State & VREPLACE_FLAG)
15167 {
15168 buf[0] = 'R';
15169 buf[1] = 'v';
15170 }
15171 else
15172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 if (State & REPLACE_FLAG)
15174 buf[0] = 'R';
15175 else
15176 buf[0] = 'i';
15177 }
15178 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015181 if (exmode_active)
15182 buf[1] = 'v';
15183 }
15184 else if (exmode_active)
15185 {
15186 buf[0] = 'c';
15187 buf[1] = 'e';
15188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015192 if (finish_op)
15193 buf[1] = 'o';
15194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015196 /* Clear out the minor mode when the argument is not a non-zero number or
15197 * non-empty string. */
15198 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015199 buf[1] = NUL;
15200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201 rettv->vval.v_string = vim_strsave(buf);
15202 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203}
15204
Bram Moolenaar429fa852013-04-15 12:27:36 +020015205#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015206/*
15207 * "mzeval()" function
15208 */
15209 static void
15210f_mzeval(argvars, rettv)
15211 typval_T *argvars;
15212 typval_T *rettv;
15213{
15214 char_u *str;
15215 char_u buf[NUMBUFLEN];
15216
15217 str = get_tv_string_buf(&argvars[0], buf);
15218 do_mzeval(str, rettv);
15219}
Bram Moolenaar75676462013-01-30 14:55:42 +010015220
15221 void
15222mzscheme_call_vim(name, args, rettv)
15223 char_u *name;
15224 typval_T *args;
15225 typval_T *rettv;
15226{
15227 typval_T argvars[3];
15228
15229 argvars[0].v_type = VAR_STRING;
15230 argvars[0].vval.v_string = name;
15231 copy_tv(args, &argvars[1]);
15232 argvars[2].v_type = VAR_UNKNOWN;
15233 f_call(argvars, rettv);
15234 clear_tv(&argvars[1]);
15235}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015236#endif
15237
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239 * "nextnonblank()" function
15240 */
15241 static void
15242f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 typval_T *argvars;
15244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245{
15246 linenr_T lnum;
15247
15248 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251 {
15252 lnum = 0;
15253 break;
15254 }
15255 if (*skipwhite(ml_get(lnum)) != NUL)
15256 break;
15257 }
15258 rettv->vval.v_number = lnum;
15259}
15260
15261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 * "nr2char()" function
15263 */
15264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015266 typval_T *argvars;
15267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268{
15269 char_u buf[NUMBUFLEN];
15270
15271#ifdef FEAT_MBYTE
15272 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015273 {
15274 int utf8 = 0;
15275
15276 if (argvars[1].v_type != VAR_UNKNOWN)
15277 utf8 = get_tv_number_chk(&argvars[1], NULL);
15278 if (utf8)
15279 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15280 else
15281 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 else
15284#endif
15285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 buf[1] = NUL;
15288 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015289 rettv->v_type = VAR_STRING;
15290 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015291}
15292
15293/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015294 * "or(expr, expr)" function
15295 */
15296 static void
15297f_or(argvars, rettv)
15298 typval_T *argvars;
15299 typval_T *rettv;
15300{
15301 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15302 | get_tv_number_chk(&argvars[1], NULL);
15303}
15304
15305/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015306 * "pathshorten()" function
15307 */
15308 static void
15309f_pathshorten(argvars, rettv)
15310 typval_T *argvars;
15311 typval_T *rettv;
15312{
15313 char_u *p;
15314
15315 rettv->v_type = VAR_STRING;
15316 p = get_tv_string_chk(&argvars[0]);
15317 if (p == NULL)
15318 rettv->vval.v_string = NULL;
15319 else
15320 {
15321 p = vim_strsave(p);
15322 rettv->vval.v_string = p;
15323 if (p != NULL)
15324 shorten_dir(p);
15325 }
15326}
15327
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015328#ifdef FEAT_FLOAT
15329/*
15330 * "pow()" function
15331 */
15332 static void
15333f_pow(argvars, rettv)
15334 typval_T *argvars;
15335 typval_T *rettv;
15336{
15337 float_T fx, fy;
15338
15339 rettv->v_type = VAR_FLOAT;
15340 if (get_float_arg(argvars, &fx) == OK
15341 && get_float_arg(&argvars[1], &fy) == OK)
15342 rettv->vval.v_float = pow(fx, fy);
15343 else
15344 rettv->vval.v_float = 0.0;
15345}
15346#endif
15347
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349 * "prevnonblank()" function
15350 */
15351 static void
15352f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015353 typval_T *argvars;
15354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355{
15356 linenr_T lnum;
15357
15358 lnum = get_tv_lnum(argvars);
15359 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15360 lnum = 0;
15361 else
15362 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15363 --lnum;
15364 rettv->vval.v_number = lnum;
15365}
15366
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015367#ifdef HAVE_STDARG_H
15368/* This dummy va_list is here because:
15369 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15370 * - locally in the function results in a "used before set" warning
15371 * - using va_start() to initialize it gives "function with fixed args" error */
15372static va_list ap;
15373#endif
15374
Bram Moolenaar8c711452005-01-14 21:53:12 +000015375/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015376 * "printf()" function
15377 */
15378 static void
15379f_printf(argvars, rettv)
15380 typval_T *argvars;
15381 typval_T *rettv;
15382{
15383 rettv->v_type = VAR_STRING;
15384 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015385#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015386 {
15387 char_u buf[NUMBUFLEN];
15388 int len;
15389 char_u *s;
15390 int saved_did_emsg = did_emsg;
15391 char *fmt;
15392
15393 /* Get the required length, allocate the buffer and do it for real. */
15394 did_emsg = FALSE;
15395 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015396 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015397 if (!did_emsg)
15398 {
15399 s = alloc(len + 1);
15400 if (s != NULL)
15401 {
15402 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015403 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015404 }
15405 }
15406 did_emsg |= saved_did_emsg;
15407 }
15408#endif
15409}
15410
15411/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015412 * "pumvisible()" function
15413 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015414 static void
15415f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015416 typval_T *argvars UNUSED;
15417 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015419#ifdef FEAT_INS_EXPAND
15420 if (pum_visible())
15421 rettv->vval.v_number = 1;
15422#endif
15423}
15424
Bram Moolenaardb913952012-06-29 12:54:53 +020015425#ifdef FEAT_PYTHON3
15426/*
15427 * "py3eval()" function
15428 */
15429 static void
15430f_py3eval(argvars, rettv)
15431 typval_T *argvars;
15432 typval_T *rettv;
15433{
15434 char_u *str;
15435 char_u buf[NUMBUFLEN];
15436
15437 str = get_tv_string_buf(&argvars[0], buf);
15438 do_py3eval(str, rettv);
15439}
15440#endif
15441
15442#ifdef FEAT_PYTHON
15443/*
15444 * "pyeval()" function
15445 */
15446 static void
15447f_pyeval(argvars, rettv)
15448 typval_T *argvars;
15449 typval_T *rettv;
15450{
15451 char_u *str;
15452 char_u buf[NUMBUFLEN];
15453
15454 str = get_tv_string_buf(&argvars[0], buf);
15455 do_pyeval(str, rettv);
15456}
15457#endif
15458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015459/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015460 * "range()" function
15461 */
15462 static void
15463f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 typval_T *argvars;
15465 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015466{
15467 long start;
15468 long end;
15469 long stride = 1;
15470 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015474 if (argvars[1].v_type == VAR_UNKNOWN)
15475 {
15476 end = start - 1;
15477 start = 0;
15478 }
15479 else
15480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015481 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015482 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015484 }
15485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015486 if (error)
15487 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015488 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015489 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015490 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015491 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015492 else
15493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015494 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015495 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015496 if (list_append_number(rettv->vval.v_list,
15497 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015498 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015499 }
15500}
15501
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015502/*
15503 * "readfile()" function
15504 */
15505 static void
15506f_readfile(argvars, rettv)
15507 typval_T *argvars;
15508 typval_T *rettv;
15509{
15510 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015512 char_u *fname;
15513 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015514 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15515 int io_size = sizeof(buf);
15516 int readlen; /* size of last fread() */
15517 char_u *prev = NULL; /* previously read bytes, if any */
15518 long prevlen = 0; /* length of data in prev */
15519 long prevsize = 0; /* size of prev buffer */
15520 long maxline = MAXLNUM;
15521 long cnt = 0;
15522 char_u *p; /* position in buf */
15523 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015524
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015525 if (argvars[1].v_type != VAR_UNKNOWN)
15526 {
15527 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15528 binary = TRUE;
15529 if (argvars[2].v_type != VAR_UNKNOWN)
15530 maxline = get_tv_number(&argvars[2]);
15531 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015533 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015535
15536 /* Always open the file in binary mode, library functions have a mind of
15537 * their own about CR-LF conversion. */
15538 fname = get_tv_string(&argvars[0]);
15539 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15540 {
15541 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15542 return;
15543 }
15544
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015545 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015547 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015549 /* This for loop processes what was read, but is also entered at end
15550 * of file so that either:
15551 * - an incomplete line gets written
15552 * - a "binary" file gets an empty line at the end if it ends in a
15553 * newline. */
15554 for (p = buf, start = buf;
15555 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15556 ++p)
15557 {
15558 if (*p == '\n' || readlen <= 0)
15559 {
15560 listitem_T *li;
15561 char_u *s = NULL;
15562 long_u len = p - start;
15563
15564 /* Finished a line. Remove CRs before NL. */
15565 if (readlen > 0 && !binary)
15566 {
15567 while (len > 0 && start[len - 1] == '\r')
15568 --len;
15569 /* removal may cross back to the "prev" string */
15570 if (len == 0)
15571 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15572 --prevlen;
15573 }
15574 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015575 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576 else
15577 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015578 /* Change "prev" buffer to be the right size. This way
15579 * the bytes are only copied once, and very long lines are
15580 * allocated only once. */
15581 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015583 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015584 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015585 prev = NULL; /* the list will own the string */
15586 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015587 }
15588 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015589 if (s == NULL)
15590 {
15591 do_outofmem_msg((long_u) prevlen + len + 1);
15592 failed = TRUE;
15593 break;
15594 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015595
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015596 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 {
15598 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015599 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015600 break;
15601 }
15602 li->li_tv.v_type = VAR_STRING;
15603 li->li_tv.v_lock = 0;
15604 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015605 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015606
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015607 start = p + 1; /* step over newline */
15608 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609 break;
15610 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015611 else if (*p == NUL)
15612 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015613#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015614 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15615 * when finding the BF and check the previous two bytes. */
15616 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015617 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015618 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15619 * + 1, these may be in the "prev" string. */
15620 char_u back1 = p >= buf + 1 ? p[-1]
15621 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15622 char_u back2 = p >= buf + 2 ? p[-2]
15623 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15624 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015625
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015628 char_u *dest = p - 2;
15629
15630 /* Usually a BOM is at the beginning of a file, and so at
15631 * the beginning of a line; then we can just step over it.
15632 */
15633 if (start == dest)
15634 start = p + 1;
15635 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015636 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015637 /* have to shuffle buf to close gap */
15638 int adjust_prevlen = 0;
15639
15640 if (dest < buf)
15641 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015642 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015643 dest = buf;
15644 }
15645 if (readlen > p - buf + 1)
15646 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15647 readlen -= 3 - adjust_prevlen;
15648 prevlen -= adjust_prevlen;
15649 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015650 }
15651 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015652 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015653#endif
15654 } /* for */
15655
15656 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15657 break;
15658 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015660 /* There's part of a line in buf, store it in "prev". */
15661 if (p - start + prevlen >= prevsize)
15662 {
15663 /* need bigger "prev" buffer */
15664 char_u *newprev;
15665
15666 /* A common use case is ordinary text files and "prev" gets a
15667 * fragment of a line, so the first allocation is made
15668 * small, to avoid repeatedly 'allocing' large and
15669 * 'reallocing' small. */
15670 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015671 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 else
15673 {
15674 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015675 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015676 prevsize = grow50pc > growmin ? grow50pc : growmin;
15677 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015678 newprev = prev == NULL ? alloc(prevsize)
15679 : vim_realloc(prev, prevsize);
15680 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015681 {
15682 do_outofmem_msg((long_u)prevsize);
15683 failed = TRUE;
15684 break;
15685 }
15686 prev = newprev;
15687 }
15688 /* Add the line part to end of "prev". */
15689 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015690 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015691 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015692 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015694 /*
15695 * For a negative line count use only the lines at the end of the file,
15696 * free the rest.
15697 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015698 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015699 while (cnt > -maxline)
15700 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015702 --cnt;
15703 }
15704
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015705 if (failed)
15706 {
15707 list_free(rettv->vval.v_list, TRUE);
15708 /* readfile doc says an empty list is returned on error */
15709 rettv->vval.v_list = list_alloc();
15710 }
15711
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015712 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015713 fclose(fd);
15714}
15715
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015716#if defined(FEAT_RELTIME)
15717static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15718
15719/*
15720 * Convert a List to proftime_T.
15721 * Return FAIL when there is something wrong.
15722 */
15723 static int
15724list2proftime(arg, tm)
15725 typval_T *arg;
15726 proftime_T *tm;
15727{
15728 long n1, n2;
15729 int error = FALSE;
15730
15731 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15732 || arg->vval.v_list->lv_len != 2)
15733 return FAIL;
15734 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15735 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15736# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015737 tm->HighPart = n1;
15738 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015739# else
15740 tm->tv_sec = n1;
15741 tm->tv_usec = n2;
15742# endif
15743 return error ? FAIL : OK;
15744}
15745#endif /* FEAT_RELTIME */
15746
15747/*
15748 * "reltime()" function
15749 */
15750 static void
15751f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015752 typval_T *argvars UNUSED;
15753 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015754{
15755#ifdef FEAT_RELTIME
15756 proftime_T res;
15757 proftime_T start;
15758
15759 if (argvars[0].v_type == VAR_UNKNOWN)
15760 {
15761 /* No arguments: get current time. */
15762 profile_start(&res);
15763 }
15764 else if (argvars[1].v_type == VAR_UNKNOWN)
15765 {
15766 if (list2proftime(&argvars[0], &res) == FAIL)
15767 return;
15768 profile_end(&res);
15769 }
15770 else
15771 {
15772 /* Two arguments: compute the difference. */
15773 if (list2proftime(&argvars[0], &start) == FAIL
15774 || list2proftime(&argvars[1], &res) == FAIL)
15775 return;
15776 profile_sub(&res, &start);
15777 }
15778
15779 if (rettv_list_alloc(rettv) == OK)
15780 {
15781 long n1, n2;
15782
15783# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015784 n1 = res.HighPart;
15785 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015786# else
15787 n1 = res.tv_sec;
15788 n2 = res.tv_usec;
15789# endif
15790 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15791 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15792 }
15793#endif
15794}
15795
15796/*
15797 * "reltimestr()" function
15798 */
15799 static void
15800f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015801 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015802 typval_T *rettv;
15803{
15804#ifdef FEAT_RELTIME
15805 proftime_T tm;
15806#endif
15807
15808 rettv->v_type = VAR_STRING;
15809 rettv->vval.v_string = NULL;
15810#ifdef FEAT_RELTIME
15811 if (list2proftime(&argvars[0], &tm) == OK)
15812 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15813#endif
15814}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015815
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15817static void make_connection __ARGS((void));
15818static int check_connection __ARGS((void));
15819
15820 static void
15821make_connection()
15822{
15823 if (X_DISPLAY == NULL
15824# ifdef FEAT_GUI
15825 && !gui.in_use
15826# endif
15827 )
15828 {
15829 x_force_connect = TRUE;
15830 setup_term_clip();
15831 x_force_connect = FALSE;
15832 }
15833}
15834
15835 static int
15836check_connection()
15837{
15838 make_connection();
15839 if (X_DISPLAY == NULL)
15840 {
15841 EMSG(_("E240: No connection to Vim server"));
15842 return FAIL;
15843 }
15844 return OK;
15845}
15846#endif
15847
15848#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015849static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850
15851 static void
15852remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T *argvars;
15854 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 int expr;
15856{
15857 char_u *server_name;
15858 char_u *keys;
15859 char_u *r = NULL;
15860 char_u buf[NUMBUFLEN];
15861# ifdef WIN32
15862 HWND w;
15863# else
15864 Window w;
15865# endif
15866
15867 if (check_restricted() || check_secure())
15868 return;
15869
15870# ifdef FEAT_X11
15871 if (check_connection() == FAIL)
15872 return;
15873# endif
15874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015875 server_name = get_tv_string_chk(&argvars[0]);
15876 if (server_name == NULL)
15877 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015878 keys = get_tv_string_buf(&argvars[1], buf);
15879# ifdef WIN32
15880 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15881# else
15882 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15883 < 0)
15884# endif
15885 {
15886 if (r != NULL)
15887 EMSG(r); /* sending worked but evaluation failed */
15888 else
15889 EMSG2(_("E241: Unable to send to %s"), server_name);
15890 return;
15891 }
15892
15893 rettv->vval.v_string = r;
15894
15895 if (argvars[2].v_type != VAR_UNKNOWN)
15896 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015898 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015899 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015900
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015901 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015902 v.di_tv.v_type = VAR_STRING;
15903 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015904 idvar = get_tv_string_chk(&argvars[2]);
15905 if (idvar != NULL)
15906 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 }
15909}
15910#endif
15911
15912/*
15913 * "remote_expr()" function
15914 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 static void
15916f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015918 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919{
15920 rettv->v_type = VAR_STRING;
15921 rettv->vval.v_string = NULL;
15922#ifdef FEAT_CLIENTSERVER
15923 remote_common(argvars, rettv, TRUE);
15924#endif
15925}
15926
15927/*
15928 * "remote_foreground()" function
15929 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015930 static void
15931f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015932 typval_T *argvars UNUSED;
15933 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015935#ifdef FEAT_CLIENTSERVER
15936# ifdef WIN32
15937 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 {
15939 char_u *server_name = get_tv_string_chk(&argvars[0]);
15940
15941 if (server_name != NULL)
15942 serverForeground(server_name);
15943 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944# else
15945 /* Send a foreground() expression to the server. */
15946 argvars[1].v_type = VAR_STRING;
15947 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15948 argvars[2].v_type = VAR_UNKNOWN;
15949 remote_common(argvars, rettv, TRUE);
15950 vim_free(argvars[1].vval.v_string);
15951# endif
15952#endif
15953}
15954
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 static void
15956f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959{
15960#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015962 char_u *s = NULL;
15963# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015964 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015966 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015967
15968 if (check_restricted() || check_secure())
15969 {
15970 rettv->vval.v_number = -1;
15971 return;
15972 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015973 serverid = get_tv_string_chk(&argvars[0]);
15974 if (serverid == NULL)
15975 {
15976 rettv->vval.v_number = -1;
15977 return; /* type error; errmsg already given */
15978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015980 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981 if (n == 0)
15982 rettv->vval.v_number = -1;
15983 else
15984 {
15985 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15986 rettv->vval.v_number = (s != NULL);
15987 }
15988# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989 if (check_connection() == FAIL)
15990 return;
15991
15992 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015994# endif
15995
15996 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 char_u *retvar;
15999
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 v.di_tv.v_type = VAR_STRING;
16001 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016002 retvar = get_tv_string_chk(&argvars[1]);
16003 if (retvar != NULL)
16004 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016006 }
16007#else
16008 rettv->vval.v_number = -1;
16009#endif
16010}
16011
Bram Moolenaar0d660222005-01-07 21:51:51 +000016012 static void
16013f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016016{
16017 char_u *r = NULL;
16018
16019#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 char_u *serverid = get_tv_string_chk(&argvars[0]);
16021
16022 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023 {
16024# ifdef WIN32
16025 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016026 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016028 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 if (n != 0)
16030 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16031 if (r == NULL)
16032# else
16033 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035# endif
16036 EMSG(_("E277: Unable to read a server reply"));
16037 }
16038#endif
16039 rettv->v_type = VAR_STRING;
16040 rettv->vval.v_string = r;
16041}
16042
16043/*
16044 * "remote_send()" function
16045 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046 static void
16047f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016050{
16051 rettv->v_type = VAR_STRING;
16052 rettv->vval.v_string = NULL;
16053#ifdef FEAT_CLIENTSERVER
16054 remote_common(argvars, rettv, FALSE);
16055#endif
16056}
16057
16058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016059 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016060 */
16061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016062f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 typval_T *argvars;
16064 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016065{
Bram Moolenaar33570922005-01-25 22:26:29 +000016066 list_T *l;
16067 listitem_T *item, *item2;
16068 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016069 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016070 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016071 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016072 dict_T *d;
16073 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016074 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016075
Bram Moolenaar8c711452005-01-14 21:53:12 +000016076 if (argvars[0].v_type == VAR_DICT)
16077 {
16078 if (argvars[2].v_type != VAR_UNKNOWN)
16079 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016080 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016081 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 key = get_tv_string_chk(&argvars[1]);
16084 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 di = dict_find(d, key, -1);
16087 if (di == NULL)
16088 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016089 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16090 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 {
16092 *rettv = di->di_tv;
16093 init_tv(&di->di_tv);
16094 dictitem_remove(d, di);
16095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016096 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016097 }
16098 }
16099 else if (argvars[0].v_type != VAR_LIST)
16100 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016101 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016102 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016104 int error = FALSE;
16105
16106 idx = get_tv_number_chk(&argvars[1], &error);
16107 if (error)
16108 ; /* type error: do nothing, errmsg already given */
16109 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016110 EMSGN(_(e_listidx), idx);
16111 else
16112 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016113 if (argvars[2].v_type == VAR_UNKNOWN)
16114 {
16115 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016116 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016117 *rettv = item->li_tv;
16118 vim_free(item);
16119 }
16120 else
16121 {
16122 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016123 end = get_tv_number_chk(&argvars[2], &error);
16124 if (error)
16125 ; /* type error: do nothing */
16126 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016127 EMSGN(_(e_listidx), end);
16128 else
16129 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016130 int cnt = 0;
16131
16132 for (li = item; li != NULL; li = li->li_next)
16133 {
16134 ++cnt;
16135 if (li == item2)
16136 break;
16137 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016138 if (li == NULL) /* didn't find "item2" after "item" */
16139 EMSG(_(e_invrange));
16140 else
16141 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016142 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016144 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016145 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016146 l->lv_first = item;
16147 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016148 item->li_prev = NULL;
16149 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016150 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016151 }
16152 }
16153 }
16154 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016155 }
16156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157}
16158
16159/*
16160 * "rename({from}, {to})" function
16161 */
16162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016163f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016164 typval_T *argvars;
16165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166{
16167 char_u buf[NUMBUFLEN];
16168
16169 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16173 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174}
16175
16176/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016177 * "repeat()" function
16178 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016179 static void
16180f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *argvars;
16182 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016183{
16184 char_u *p;
16185 int n;
16186 int slen;
16187 int len;
16188 char_u *r;
16189 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016190
16191 n = get_tv_number(&argvars[1]);
16192 if (argvars[0].v_type == VAR_LIST)
16193 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016194 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016195 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 if (list_extend(rettv->vval.v_list,
16197 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016198 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016199 }
16200 else
16201 {
16202 p = get_tv_string(&argvars[0]);
16203 rettv->v_type = VAR_STRING;
16204 rettv->vval.v_string = NULL;
16205
16206 slen = (int)STRLEN(p);
16207 len = slen * n;
16208 if (len <= 0)
16209 return;
16210
16211 r = alloc(len + 1);
16212 if (r != NULL)
16213 {
16214 for (i = 0; i < n; i++)
16215 mch_memmove(r + i * slen, p, (size_t)slen);
16216 r[len] = NUL;
16217 }
16218
16219 rettv->vval.v_string = r;
16220 }
16221}
16222
16223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 * "resolve()" function
16225 */
16226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016227f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016228 typval_T *argvars;
16229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230{
16231 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016232#ifdef HAVE_READLINK
16233 char_u *buf = NULL;
16234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016236 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237#ifdef FEAT_SHORTCUT
16238 {
16239 char_u *v = NULL;
16240
16241 v = mch_resolve_shortcut(p);
16242 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 }
16247#else
16248# ifdef HAVE_READLINK
16249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 char_u *cpy;
16251 int len;
16252 char_u *remain = NULL;
16253 char_u *q;
16254 int is_relative_to_current = FALSE;
16255 int has_trailing_pathsep = FALSE;
16256 int limit = 100;
16257
16258 p = vim_strsave(p);
16259
16260 if (p[0] == '.' && (vim_ispathsep(p[1])
16261 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16262 is_relative_to_current = TRUE;
16263
16264 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016265 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016268 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
16271 q = getnextcomp(p);
16272 if (*q != NUL)
16273 {
16274 /* Separate the first path component in "p", and keep the
16275 * remainder (beginning with the path separator). */
16276 remain = vim_strsave(q - 1);
16277 q[-1] = NUL;
16278 }
16279
Bram Moolenaard9462e32011-04-11 21:35:11 +020016280 buf = alloc(MAXPATHL + 1);
16281 if (buf == NULL)
16282 goto fail;
16283
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 for (;;)
16285 {
16286 for (;;)
16287 {
16288 len = readlink((char *)p, (char *)buf, MAXPATHL);
16289 if (len <= 0)
16290 break;
16291 buf[len] = NUL;
16292
16293 if (limit-- == 0)
16294 {
16295 vim_free(p);
16296 vim_free(remain);
16297 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 goto fail;
16300 }
16301
16302 /* Ensure that the result will have a trailing path separator
16303 * if the argument has one. */
16304 if (remain == NULL && has_trailing_pathsep)
16305 add_pathsep(buf);
16306
16307 /* Separate the first path component in the link value and
16308 * concatenate the remainders. */
16309 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16310 if (*q != NUL)
16311 {
16312 if (remain == NULL)
16313 remain = vim_strsave(q - 1);
16314 else
16315 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016316 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 if (cpy != NULL)
16318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 vim_free(remain);
16320 remain = cpy;
16321 }
16322 }
16323 q[-1] = NUL;
16324 }
16325
16326 q = gettail(p);
16327 if (q > p && *q == NUL)
16328 {
16329 /* Ignore trailing path separator. */
16330 q[-1] = NUL;
16331 q = gettail(p);
16332 }
16333 if (q > p && !mch_isFullName(buf))
16334 {
16335 /* symlink is relative to directory of argument */
16336 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16337 if (cpy != NULL)
16338 {
16339 STRCPY(cpy, p);
16340 STRCPY(gettail(cpy), buf);
16341 vim_free(p);
16342 p = cpy;
16343 }
16344 }
16345 else
16346 {
16347 vim_free(p);
16348 p = vim_strsave(buf);
16349 }
16350 }
16351
16352 if (remain == NULL)
16353 break;
16354
16355 /* Append the first path component of "remain" to "p". */
16356 q = getnextcomp(remain + 1);
16357 len = q - remain - (*q != NUL);
16358 cpy = vim_strnsave(p, STRLEN(p) + len);
16359 if (cpy != NULL)
16360 {
16361 STRNCAT(cpy, remain, len);
16362 vim_free(p);
16363 p = cpy;
16364 }
16365 /* Shorten "remain". */
16366 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016367 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 else
16369 {
16370 vim_free(remain);
16371 remain = NULL;
16372 }
16373 }
16374
16375 /* If the result is a relative path name, make it explicitly relative to
16376 * the current directory if and only if the argument had this form. */
16377 if (!vim_ispathsep(*p))
16378 {
16379 if (is_relative_to_current
16380 && *p != NUL
16381 && !(p[0] == '.'
16382 && (p[1] == NUL
16383 || vim_ispathsep(p[1])
16384 || (p[1] == '.'
16385 && (p[2] == NUL
16386 || vim_ispathsep(p[2]))))))
16387 {
16388 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016389 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 if (cpy != NULL)
16391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 vim_free(p);
16393 p = cpy;
16394 }
16395 }
16396 else if (!is_relative_to_current)
16397 {
16398 /* Strip leading "./". */
16399 q = p;
16400 while (q[0] == '.' && vim_ispathsep(q[1]))
16401 q += 2;
16402 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016403 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 }
16405 }
16406
16407 /* Ensure that the result will have no trailing path separator
16408 * if the argument had none. But keep "/" or "//". */
16409 if (!has_trailing_pathsep)
16410 {
16411 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016412 if (after_pathsep(p, q))
16413 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 }
16415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016416 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 }
16418# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016419 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420# endif
16421#endif
16422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424
16425#ifdef HAVE_READLINK
16426fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016427 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430}
16431
16432/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 */
16435 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016437 typval_T *argvars;
16438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439{
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 list_T *l;
16441 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 if (argvars[0].v_type != VAR_LIST)
16444 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016445 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016446 && !tv_check_lock(l->lv_lock,
16447 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 {
16449 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016450 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016451 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 while (li != NULL)
16453 {
16454 ni = li->li_prev;
16455 list_append(l, li);
16456 li = ni;
16457 }
16458 rettv->vval.v_list = l;
16459 rettv->v_type = VAR_LIST;
16460 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016461 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463}
16464
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016465#define SP_NOMOVE 0x01 /* don't move cursor */
16466#define SP_REPEAT 0x02 /* repeat to find outer pair */
16467#define SP_RETCOUNT 0x04 /* return matchcount */
16468#define SP_SETPCMARK 0x08 /* set previous context mark */
16469#define SP_START 0x10 /* accept match at start position */
16470#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16471#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016472
Bram Moolenaar33570922005-01-25 22:26:29 +000016473static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474
16475/*
16476 * Get flags for a search function.
16477 * Possibly sets "p_ws".
16478 * Returns BACKWARD, FORWARD or zero (for an error).
16479 */
16480 static int
16481get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 int *flagsp;
16484{
16485 int dir = FORWARD;
16486 char_u *flags;
16487 char_u nbuf[NUMBUFLEN];
16488 int mask;
16489
16490 if (varp->v_type != VAR_UNKNOWN)
16491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 flags = get_tv_string_buf_chk(varp, nbuf);
16493 if (flags == NULL)
16494 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 while (*flags != NUL)
16496 {
16497 switch (*flags)
16498 {
16499 case 'b': dir = BACKWARD; break;
16500 case 'w': p_ws = TRUE; break;
16501 case 'W': p_ws = FALSE; break;
16502 default: mask = 0;
16503 if (flagsp != NULL)
16504 switch (*flags)
16505 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016506 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016507 case 'e': mask = SP_END; break;
16508 case 'm': mask = SP_RETCOUNT; break;
16509 case 'n': mask = SP_NOMOVE; break;
16510 case 'p': mask = SP_SUBPAT; break;
16511 case 'r': mask = SP_REPEAT; break;
16512 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513 }
16514 if (mask == 0)
16515 {
16516 EMSG2(_(e_invarg2), flags);
16517 dir = 0;
16518 }
16519 else
16520 *flagsp |= mask;
16521 }
16522 if (dir == 0)
16523 break;
16524 ++flags;
16525 }
16526 }
16527 return dir;
16528}
16529
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016531 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016533 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016534search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016535 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016536 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016537 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016539 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 char_u *pat;
16541 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016542 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 int save_p_ws = p_ws;
16544 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016545 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016546 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016547 proftime_T tm;
16548#ifdef FEAT_RELTIME
16549 long time_limit = 0;
16550#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016551 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016552 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016555 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016556 if (dir == 0)
16557 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016558 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016559 if (flags & SP_START)
16560 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016561 if (flags & SP_END)
16562 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563
Bram Moolenaar76929292008-01-06 19:07:36 +000016564 /* Optional arguments: line number to stop searching and timeout. */
16565 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016566 {
16567 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16568 if (lnum_stop < 0)
16569 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016570#ifdef FEAT_RELTIME
16571 if (argvars[3].v_type != VAR_UNKNOWN)
16572 {
16573 time_limit = get_tv_number_chk(&argvars[3], NULL);
16574 if (time_limit < 0)
16575 goto theend;
16576 }
16577#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016578 }
16579
Bram Moolenaar76929292008-01-06 19:07:36 +000016580#ifdef FEAT_RELTIME
16581 /* Set the time limit, if there is one. */
16582 profile_setlimit(time_limit, &tm);
16583#endif
16584
Bram Moolenaar231334e2005-07-25 20:46:57 +000016585 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016586 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016587 * Check to make sure only those flags are set.
16588 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16589 * flags cannot be set. Check for that condition also.
16590 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016591 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016592 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016595 goto theend;
16596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016598 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016599 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016600 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016601 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016603 if (flags & SP_SUBPAT)
16604 retval = subpatnum;
16605 else
16606 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016607 if (flags & SP_SETPCMARK)
16608 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016610 if (match_pos != NULL)
16611 {
16612 /* Store the match cursor position */
16613 match_pos->lnum = pos.lnum;
16614 match_pos->col = pos.col + 1;
16615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 /* "/$" will put the cursor after the end of the line, may need to
16617 * correct that here */
16618 check_cursor();
16619 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016620
16621 /* If 'n' flag is used: restore cursor position. */
16622 if (flags & SP_NOMOVE)
16623 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016624 else
16625 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016626theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016628
16629 return retval;
16630}
16631
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016632#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016633
16634/*
16635 * round() is not in C90, use ceil() or floor() instead.
16636 */
16637 float_T
16638vim_round(f)
16639 float_T f;
16640{
16641 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16642}
16643
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016644/*
16645 * "round({float})" function
16646 */
16647 static void
16648f_round(argvars, rettv)
16649 typval_T *argvars;
16650 typval_T *rettv;
16651{
16652 float_T f;
16653
16654 rettv->v_type = VAR_FLOAT;
16655 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016656 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016657 else
16658 rettv->vval.v_float = 0.0;
16659}
16660#endif
16661
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016663 * "screenattr()" function
16664 */
16665 static void
16666f_screenattr(argvars, rettv)
16667 typval_T *argvars UNUSED;
16668 typval_T *rettv;
16669{
16670 int row;
16671 int col;
16672 int c;
16673
16674 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16675 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16676 if (row < 0 || row >= screen_Rows
16677 || col < 0 || col >= screen_Columns)
16678 c = -1;
16679 else
16680 c = ScreenAttrs[LineOffset[row] + col];
16681 rettv->vval.v_number = c;
16682}
16683
16684/*
16685 * "screenchar()" function
16686 */
16687 static void
16688f_screenchar(argvars, rettv)
16689 typval_T *argvars UNUSED;
16690 typval_T *rettv;
16691{
16692 int row;
16693 int col;
16694 int off;
16695 int c;
16696
16697 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16698 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16699 if (row < 0 || row >= screen_Rows
16700 || col < 0 || col >= screen_Columns)
16701 c = -1;
16702 else
16703 {
16704 off = LineOffset[row] + col;
16705#ifdef FEAT_MBYTE
16706 if (enc_utf8 && ScreenLinesUC[off] != 0)
16707 c = ScreenLinesUC[off];
16708 else
16709#endif
16710 c = ScreenLines[off];
16711 }
16712 rettv->vval.v_number = c;
16713}
16714
16715/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016716 * "screencol()" function
16717 *
16718 * First column is 1 to be consistent with virtcol().
16719 */
16720 static void
16721f_screencol(argvars, rettv)
16722 typval_T *argvars UNUSED;
16723 typval_T *rettv;
16724{
16725 rettv->vval.v_number = screen_screencol() + 1;
16726}
16727
16728/*
16729 * "screenrow()" function
16730 */
16731 static void
16732f_screenrow(argvars, rettv)
16733 typval_T *argvars UNUSED;
16734 typval_T *rettv;
16735{
16736 rettv->vval.v_number = screen_screenrow() + 1;
16737}
16738
16739/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016740 * "search()" function
16741 */
16742 static void
16743f_search(argvars, rettv)
16744 typval_T *argvars;
16745 typval_T *rettv;
16746{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016747 int flags = 0;
16748
16749 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750}
16751
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016753 * "searchdecl()" function
16754 */
16755 static void
16756f_searchdecl(argvars, rettv)
16757 typval_T *argvars;
16758 typval_T *rettv;
16759{
16760 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016761 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016762 int error = FALSE;
16763 char_u *name;
16764
16765 rettv->vval.v_number = 1; /* default: FAIL */
16766
16767 name = get_tv_string_chk(&argvars[0]);
16768 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016769 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016770 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016771 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16772 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16773 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016774 if (!error && name != NULL)
16775 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016776 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016777}
16778
16779/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016780 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016782 static int
16783searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016785 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786{
16787 char_u *spat, *mpat, *epat;
16788 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 int dir;
16791 int flags = 0;
16792 char_u nbuf1[NUMBUFLEN];
16793 char_u nbuf2[NUMBUFLEN];
16794 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016795 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016796 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016797 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016800 spat = get_tv_string_chk(&argvars[0]);
16801 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16802 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16803 if (spat == NULL || mpat == NULL || epat == NULL)
16804 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 /* Handle the optional fourth argument: flags */
16807 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016808 if (dir == 0)
16809 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016810
16811 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016812 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16813 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016814 if ((flags & (SP_END | SP_SUBPAT)) != 0
16815 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016816 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016817 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016818 goto theend;
16819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016821 /* Using 'r' implies 'W', otherwise it doesn't work. */
16822 if (flags & SP_REPEAT)
16823 p_ws = FALSE;
16824
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016825 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016826 if (argvars[3].v_type == VAR_UNKNOWN
16827 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 skip = (char_u *)"";
16829 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016831 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016832 if (argvars[5].v_type != VAR_UNKNOWN)
16833 {
16834 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16835 if (lnum_stop < 0)
16836 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016837#ifdef FEAT_RELTIME
16838 if (argvars[6].v_type != VAR_UNKNOWN)
16839 {
16840 time_limit = get_tv_number_chk(&argvars[6], NULL);
16841 if (time_limit < 0)
16842 goto theend;
16843 }
16844#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016845 }
16846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016847 if (skip == NULL)
16848 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016850 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016851 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016852
16853theend:
16854 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016855
16856 return retval;
16857}
16858
16859/*
16860 * "searchpair()" function
16861 */
16862 static void
16863f_searchpair(argvars, rettv)
16864 typval_T *argvars;
16865 typval_T *rettv;
16866{
16867 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16868}
16869
16870/*
16871 * "searchpairpos()" function
16872 */
16873 static void
16874f_searchpairpos(argvars, rettv)
16875 typval_T *argvars;
16876 typval_T *rettv;
16877{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016878 pos_T match_pos;
16879 int lnum = 0;
16880 int col = 0;
16881
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016882 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016883 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016884
16885 if (searchpair_cmn(argvars, &match_pos) > 0)
16886 {
16887 lnum = match_pos.lnum;
16888 col = match_pos.col;
16889 }
16890
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016891 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16892 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016893}
16894
16895/*
16896 * Search for a start/middle/end thing.
16897 * Used by searchpair(), see its documentation for the details.
16898 * Returns 0 or -1 for no match,
16899 */
16900 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016901do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16902 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016903 char_u *spat; /* start pattern */
16904 char_u *mpat; /* middle pattern */
16905 char_u *epat; /* end pattern */
16906 int dir; /* BACKWARD or FORWARD */
16907 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016908 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016910 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016911 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016912{
16913 char_u *save_cpo;
16914 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16915 long retval = 0;
16916 pos_T pos;
16917 pos_T firstpos;
16918 pos_T foundpos;
16919 pos_T save_cursor;
16920 pos_T save_pos;
16921 int n;
16922 int r;
16923 int nest = 1;
16924 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016925 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016926 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016927
16928 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16929 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016930 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016931
Bram Moolenaar76929292008-01-06 19:07:36 +000016932#ifdef FEAT_RELTIME
16933 /* Set the time limit, if there is one. */
16934 profile_setlimit(time_limit, &tm);
16935#endif
16936
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016937 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16938 * start/middle/end (pat3, for the top pair). */
16939 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16940 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16941 if (pat2 == NULL || pat3 == NULL)
16942 goto theend;
16943 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16944 if (*mpat == NUL)
16945 STRCPY(pat3, pat2);
16946 else
16947 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16948 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016949 if (flags & SP_START)
16950 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016951
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 save_cursor = curwin->w_cursor;
16953 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016954 clearpos(&firstpos);
16955 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 pat = pat3;
16957 for (;;)
16958 {
16959 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016960 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16962 /* didn't find it or found the first match again: FAIL */
16963 break;
16964
16965 if (firstpos.lnum == 0)
16966 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016967 if (equalpos(pos, foundpos))
16968 {
16969 /* Found the same position again. Can happen with a pattern that
16970 * has "\zs" at the end and searching backwards. Advance one
16971 * character and try again. */
16972 if (dir == BACKWARD)
16973 decl(&pos);
16974 else
16975 incl(&pos);
16976 }
16977 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016979 /* clear the start flag to avoid getting stuck here */
16980 options &= ~SEARCH_START;
16981
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 /* If the skip pattern matches, ignore this match. */
16983 if (*skip != NUL)
16984 {
16985 save_pos = curwin->w_cursor;
16986 curwin->w_cursor = pos;
16987 r = eval_to_bool(skip, &err, NULL, FALSE);
16988 curwin->w_cursor = save_pos;
16989 if (err)
16990 {
16991 /* Evaluating {skip} caused an error, break here. */
16992 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016993 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 break;
16995 }
16996 if (r)
16997 continue;
16998 }
16999
17000 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17001 {
17002 /* Found end when searching backwards or start when searching
17003 * forward: nested pair. */
17004 ++nest;
17005 pat = pat2; /* nested, don't search for middle */
17006 }
17007 else
17008 {
17009 /* Found end when searching forward or start when searching
17010 * backward: end of (nested) pair; or found middle in outer pair. */
17011 if (--nest == 1)
17012 pat = pat3; /* outer level, search for middle */
17013 }
17014
17015 if (nest == 0)
17016 {
17017 /* Found the match: return matchcount or line number. */
17018 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017019 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017021 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017022 if (flags & SP_SETPCMARK)
17023 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 curwin->w_cursor = pos;
17025 if (!(flags & SP_REPEAT))
17026 break;
17027 nest = 1; /* search for next unmatched */
17028 }
17029 }
17030
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017031 if (match_pos != NULL)
17032 {
17033 /* Store the match cursor position */
17034 match_pos->lnum = curwin->w_cursor.lnum;
17035 match_pos->col = curwin->w_cursor.col + 1;
17036 }
17037
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017039 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 curwin->w_cursor = save_cursor;
17041
17042theend:
17043 vim_free(pat2);
17044 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017045 if (p_cpo == empty_option)
17046 p_cpo = save_cpo;
17047 else
17048 /* Darn, evaluating the {skip} expression changed the value. */
17049 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017050
17051 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052}
17053
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017054/*
17055 * "searchpos()" function
17056 */
17057 static void
17058f_searchpos(argvars, rettv)
17059 typval_T *argvars;
17060 typval_T *rettv;
17061{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017062 pos_T match_pos;
17063 int lnum = 0;
17064 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017065 int n;
17066 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017068 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017069 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017070
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017071 n = search_cmn(argvars, &match_pos, &flags);
17072 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017073 {
17074 lnum = match_pos.lnum;
17075 col = match_pos.col;
17076 }
17077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017078 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17079 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017080 if (flags & SP_SUBPAT)
17081 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017082}
17083
17084
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085 static void
17086f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090#ifdef FEAT_CLIENTSERVER
17091 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 char_u *server = get_tv_string_chk(&argvars[0]);
17093 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 if (server == NULL || reply == NULL)
17097 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 if (check_restricted() || check_secure())
17099 return;
17100# ifdef FEAT_X11
17101 if (check_connection() == FAIL)
17102 return;
17103# endif
17104
17105 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 EMSG(_("E258: Unable to send to client"));
17108 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 rettv->vval.v_number = 0;
17111#else
17112 rettv->vval.v_number = -1;
17113#endif
17114}
17115
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 static void
17117f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017119 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120{
17121 char_u *r = NULL;
17122
17123#ifdef FEAT_CLIENTSERVER
17124# ifdef WIN32
17125 r = serverGetVimNames();
17126# else
17127 make_connection();
17128 if (X_DISPLAY != NULL)
17129 r = serverGetVimNames(X_DISPLAY);
17130# endif
17131#endif
17132 rettv->v_type = VAR_STRING;
17133 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134}
17135
17136/*
17137 * "setbufvar()" function
17138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017140f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017142 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143{
17144 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 char_u nbuf[NUMBUFLEN];
17149
17150 if (check_restricted() || check_secure())
17151 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17153 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017154 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 varp = &argvars[2];
17156
17157 if (buf != NULL && varname != NULL && varp != NULL)
17158 {
17159 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161
17162 if (*varname == '&')
17163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017164 long numval;
17165 char_u *strval;
17166 int error = FALSE;
17167
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 numval = get_tv_number_chk(varp, &error);
17170 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017171 if (!error && strval != NULL)
17172 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 }
17174 else
17175 {
17176 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17177 if (bufvarname != NULL)
17178 {
17179 STRCPY(bufvarname, "b:");
17180 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017181 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 vim_free(bufvarname);
17183 }
17184 }
17185
17186 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189}
17190
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017191 static void
17192f_setcharsearch(argvars, rettv)
17193 typval_T *argvars;
17194 typval_T *rettv UNUSED;
17195{
17196 dict_T *d;
17197 dictitem_T *di;
17198 char_u *csearch;
17199
17200 if (argvars[0].v_type != VAR_DICT)
17201 {
17202 EMSG(_(e_dictreq));
17203 return;
17204 }
17205
17206 if ((d = argvars[0].vval.v_dict) != NULL)
17207 {
17208 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17209 if (csearch != NULL)
17210 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017211#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017212 if (enc_utf8)
17213 {
17214 int pcc[MAX_MCO];
17215 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017216
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017217 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17218 }
17219 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017220#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017221 set_last_csearch(PTR2CHAR(csearch),
17222 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017223 }
17224
17225 di = dict_find(d, (char_u *)"forward", -1);
17226 if (di != NULL)
17227 set_csearch_direction(get_tv_number(&di->di_tv)
17228 ? FORWARD : BACKWARD);
17229
17230 di = dict_find(d, (char_u *)"until", -1);
17231 if (di != NULL)
17232 set_csearch_until(!!get_tv_number(&di->di_tv));
17233 }
17234}
17235
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236/*
17237 * "setcmdpos()" function
17238 */
17239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017240f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017241 typval_T *argvars;
17242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 int pos = (int)get_tv_number(&argvars[0]) - 1;
17245
17246 if (pos >= 0)
17247 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248}
17249
17250/*
17251 * "setline()" function
17252 */
17253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017254f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017255 typval_T *argvars;
17256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257{
17258 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017259 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017260 list_T *l = NULL;
17261 listitem_T *li = NULL;
17262 long added = 0;
17263 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017265 lnum = get_tv_lnum(&argvars[0]);
17266 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017268 l = argvars[1].vval.v_list;
17269 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017271 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017272 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017273
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017274 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017275 for (;;)
17276 {
17277 if (l != NULL)
17278 {
17279 /* list argument, get next string */
17280 if (li == NULL)
17281 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017282 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017283 li = li->li_next;
17284 }
17285
17286 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017287 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017288 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017289
17290 /* When coming here from Insert mode, sync undo, so that this can be
17291 * undone separately from what was previously inserted. */
17292 if (u_sync_once == 2)
17293 {
17294 u_sync_once = 1; /* notify that u_sync() was called */
17295 u_sync(TRUE);
17296 }
17297
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017298 if (lnum <= curbuf->b_ml.ml_line_count)
17299 {
17300 /* existing line, replace it */
17301 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17302 {
17303 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017304 if (lnum == curwin->w_cursor.lnum)
17305 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017306 rettv->vval.v_number = 0; /* OK */
17307 }
17308 }
17309 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17310 {
17311 /* lnum is one past the last line, append the line */
17312 ++added;
17313 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17314 rettv->vval.v_number = 0; /* OK */
17315 }
17316
17317 if (l == NULL) /* only one string argument */
17318 break;
17319 ++lnum;
17320 }
17321
17322 if (added > 0)
17323 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324}
17325
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017326static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17327
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017329 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017330 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017331 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017332set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017333 win_T *wp UNUSED;
17334 typval_T *list_arg UNUSED;
17335 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017336 typval_T *rettv;
17337{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017338#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017339 char_u *act;
17340 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017341#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017342
Bram Moolenaar2641f772005-03-25 21:58:17 +000017343 rettv->vval.v_number = -1;
17344
17345#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017346 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017347 EMSG(_(e_listreq));
17348 else
17349 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017350 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017351
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017352 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017353 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017354 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 if (act == NULL)
17356 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017357 if (*act == 'a' || *act == 'r')
17358 action = *act;
17359 }
17360
Bram Moolenaar81484f42012-12-05 15:16:47 +010017361 if (l != NULL && set_errorlist(wp, l, action,
17362 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017363 rettv->vval.v_number = 0;
17364 }
17365#endif
17366}
17367
17368/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017369 * "setloclist()" function
17370 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017371 static void
17372f_setloclist(argvars, rettv)
17373 typval_T *argvars;
17374 typval_T *rettv;
17375{
17376 win_T *win;
17377
17378 rettv->vval.v_number = -1;
17379
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017380 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017381 if (win != NULL)
17382 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17383}
17384
17385/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017386 * "setmatches()" function
17387 */
17388 static void
17389f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017390 typval_T *argvars UNUSED;
17391 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017392{
17393#ifdef FEAT_SEARCH_EXTRA
17394 list_T *l;
17395 listitem_T *li;
17396 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017397 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017398
17399 rettv->vval.v_number = -1;
17400 if (argvars[0].v_type != VAR_LIST)
17401 {
17402 EMSG(_(e_listreq));
17403 return;
17404 }
17405 if ((l = argvars[0].vval.v_list) != NULL)
17406 {
17407
17408 /* To some extent make sure that we are dealing with a list from
17409 * "getmatches()". */
17410 li = l->lv_first;
17411 while (li != NULL)
17412 {
17413 if (li->li_tv.v_type != VAR_DICT
17414 || (d = li->li_tv.vval.v_dict) == NULL)
17415 {
17416 EMSG(_(e_invarg));
17417 return;
17418 }
17419 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017420 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17421 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017422 && dict_find(d, (char_u *)"priority", -1) != NULL
17423 && dict_find(d, (char_u *)"id", -1) != NULL))
17424 {
17425 EMSG(_(e_invarg));
17426 return;
17427 }
17428 li = li->li_next;
17429 }
17430
17431 clear_matches(curwin);
17432 li = l->lv_first;
17433 while (li != NULL)
17434 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017435 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017436 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017437 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017438 char_u *group;
17439 int priority;
17440 int id;
17441 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017442
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017443 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017444 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17445 {
17446 if (s == NULL)
17447 {
17448 s = list_alloc();
17449 if (s == NULL)
17450 return;
17451 }
17452
17453 /* match from matchaddpos() */
17454 for (i = 1; i < 9; i++)
17455 {
17456 sprintf((char *)buf, (char *)"pos%d", i);
17457 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17458 {
17459 if (di->di_tv.v_type != VAR_LIST)
17460 return;
17461
17462 list_append_tv(s, &di->di_tv);
17463 s->lv_refcount++;
17464 }
17465 else
17466 break;
17467 }
17468 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017469
17470 group = get_dict_string(d, (char_u *)"group", FALSE);
17471 priority = (int)get_dict_number(d, (char_u *)"priority");
17472 id = (int)get_dict_number(d, (char_u *)"id");
17473 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17474 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17475 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017476 if (i == 0)
17477 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017478 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017479 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017480 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017481 }
17482 else
17483 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017484 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017485 list_unref(s);
17486 s = NULL;
17487 }
17488
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017489 li = li->li_next;
17490 }
17491 rettv->vval.v_number = 0;
17492 }
17493#endif
17494}
17495
17496/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017497 * "setpos()" function
17498 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017499 static void
17500f_setpos(argvars, rettv)
17501 typval_T *argvars;
17502 typval_T *rettv;
17503{
17504 pos_T pos;
17505 int fnum;
17506 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017507 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017508
Bram Moolenaar08250432008-02-13 11:42:46 +000017509 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017510 name = get_tv_string_chk(argvars);
17511 if (name != NULL)
17512 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017513 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017514 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017515 if (--pos.col < 0)
17516 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017517 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017518 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017519 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017520 if (fnum == curbuf->b_fnum)
17521 {
17522 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017523 if (curswant >= 0)
17524 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017525 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017526 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017527 }
17528 else
17529 EMSG(_(e_invarg));
17530 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017531 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17532 {
17533 /* set mark */
17534 if (setmark_pos(name[1], &pos, fnum) == OK)
17535 rettv->vval.v_number = 0;
17536 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017537 else
17538 EMSG(_(e_invarg));
17539 }
17540 }
17541}
17542
17543/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017544 * "setqflist()" function
17545 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017546 static void
17547f_setqflist(argvars, rettv)
17548 typval_T *argvars;
17549 typval_T *rettv;
17550{
17551 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17552}
17553
17554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 * "setreg()" function
17556 */
17557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 typval_T *argvars;
17560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561{
17562 int regname;
17563 char_u *strregname;
17564 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 int append;
17567 char_u yank_type;
17568 long block_len;
17569
17570 block_len = -1;
17571 yank_type = MAUTO;
17572 append = FALSE;
17573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017574 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017575 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017577 if (strregname == NULL)
17578 return; /* type error; errmsg already given */
17579 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 if (regname == 0 || regname == '@')
17581 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017585 stropt = get_tv_string_chk(&argvars[2]);
17586 if (stropt == NULL)
17587 return; /* type error */
17588 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 switch (*stropt)
17590 {
17591 case 'a': case 'A': /* append */
17592 append = TRUE;
17593 break;
17594 case 'v': case 'c': /* character-wise selection */
17595 yank_type = MCHAR;
17596 break;
17597 case 'V': case 'l': /* line-wise selection */
17598 yank_type = MLINE;
17599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 case 'b': case Ctrl_V: /* block-wise selection */
17601 yank_type = MBLOCK;
17602 if (VIM_ISDIGIT(stropt[1]))
17603 {
17604 ++stropt;
17605 block_len = getdigits(&stropt) - 1;
17606 --stropt;
17607 }
17608 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 }
17610 }
17611
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017612 if (argvars[1].v_type == VAR_LIST)
17613 {
17614 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017615 char_u **allocval;
17616 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017617 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017618 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017619 int len = argvars[1].vval.v_list->lv_len;
17620 listitem_T *li;
17621
Bram Moolenaar7d647822014-04-05 21:28:56 +020017622 /* First half: use for pointers to result lines; second half: use for
17623 * pointers to allocated copies. */
17624 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017625 if (lstval == NULL)
17626 return;
17627 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017628 allocval = lstval + len + 2;
17629 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017630
17631 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17632 li = li->li_next)
17633 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017634 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017635 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017636 goto free_lstval;
17637 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017638 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017639 /* Need to make a copy, next get_tv_string_buf_chk() will
17640 * overwrite the string. */
17641 strval = vim_strsave(buf);
17642 if (strval == NULL)
17643 goto free_lstval;
17644 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017645 }
17646 *curval++ = strval;
17647 }
17648 *curval++ = NULL;
17649
17650 write_reg_contents_lst(regname, lstval, -1,
17651 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017652free_lstval:
17653 while (curallocval > allocval)
17654 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017655 vim_free(lstval);
17656 }
17657 else
17658 {
17659 strval = get_tv_string_chk(&argvars[1]);
17660 if (strval == NULL)
17661 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017665 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017668/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017669 * "settabvar()" function
17670 */
17671 static void
17672f_settabvar(argvars, rettv)
17673 typval_T *argvars;
17674 typval_T *rettv;
17675{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017676#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017677 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017678 tabpage_T *tp;
17679#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017680 char_u *varname, *tabvarname;
17681 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017682
17683 rettv->vval.v_number = 0;
17684
17685 if (check_restricted() || check_secure())
17686 return;
17687
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017688#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017689 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017690#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017691 varname = get_tv_string_chk(&argvars[1]);
17692 varp = &argvars[2];
17693
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017694 if (varname != NULL && varp != NULL
17695#ifdef FEAT_WINDOWS
17696 && tp != NULL
17697#endif
17698 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017699 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017700#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017701 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017702 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017703#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017704
17705 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17706 if (tabvarname != NULL)
17707 {
17708 STRCPY(tabvarname, "t:");
17709 STRCPY(tabvarname + 2, varname);
17710 set_var(tabvarname, varp, TRUE);
17711 vim_free(tabvarname);
17712 }
17713
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017714#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017715 /* Restore current tabpage */
17716 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017717 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017718#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017719 }
17720}
17721
17722/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017723 * "settabwinvar()" function
17724 */
17725 static void
17726f_settabwinvar(argvars, rettv)
17727 typval_T *argvars;
17728 typval_T *rettv;
17729{
17730 setwinvar(argvars, rettv, 1);
17731}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732
17733/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017734 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017738 typval_T *argvars;
17739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017741 setwinvar(argvars, rettv, 0);
17742}
17743
17744/*
17745 * "setwinvar()" and "settabwinvar()" functions
17746 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017747
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017748 static void
17749setwinvar(argvars, rettv, off)
17750 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017751 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017752 int off;
17753{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 win_T *win;
17755#ifdef FEAT_WINDOWS
17756 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017757 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017758 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759#endif
17760 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017761 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017763 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764
17765 if (check_restricted() || check_secure())
17766 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017767
17768#ifdef FEAT_WINDOWS
17769 if (off == 1)
17770 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17771 else
17772 tp = curtab;
17773#endif
17774 win = find_win_by_nr(&argvars[off], tp);
17775 varname = get_tv_string_chk(&argvars[off + 1]);
17776 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
17778 if (win != NULL && varname != NULL && varp != NULL)
17779 {
17780#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017781 need_switch_win = !(tp == curtab && win == curwin);
17782 if (!need_switch_win
17783 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017786 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017788 long numval;
17789 char_u *strval;
17790 int error = FALSE;
17791
17792 ++varname;
17793 numval = get_tv_number_chk(varp, &error);
17794 strval = get_tv_string_buf_chk(varp, nbuf);
17795 if (!error && strval != NULL)
17796 set_option_value(varname, numval, strval, OPT_LOCAL);
17797 }
17798 else
17799 {
17800 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17801 if (winvarname != NULL)
17802 {
17803 STRCPY(winvarname, "w:");
17804 STRCPY(winvarname + 2, varname);
17805 set_var(winvarname, varp, TRUE);
17806 vim_free(winvarname);
17807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 }
17809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017811 if (need_switch_win)
17812 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813#endif
17814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815}
17816
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017817#ifdef FEAT_CRYPT
17818/*
17819 * "sha256({string})" function
17820 */
17821 static void
17822f_sha256(argvars, rettv)
17823 typval_T *argvars;
17824 typval_T *rettv;
17825{
17826 char_u *p;
17827
17828 p = get_tv_string(&argvars[0]);
17829 rettv->vval.v_string = vim_strsave(
17830 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17831 rettv->v_type = VAR_STRING;
17832}
17833#endif /* FEAT_CRYPT */
17834
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017836 * "shellescape({string})" function
17837 */
17838 static void
17839f_shellescape(argvars, rettv)
17840 typval_T *argvars;
17841 typval_T *rettv;
17842{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017843 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017844 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017845 rettv->v_type = VAR_STRING;
17846}
17847
17848/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017849 * shiftwidth() function
17850 */
17851 static void
17852f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017853 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017854 typval_T *rettv;
17855{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017856 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017857}
17858
17859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 */
17862 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 typval_T *argvars;
17865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869 p = get_tv_string(&argvars[0]);
17870 rettv->vval.v_string = vim_strsave(p);
17871 simplify_filename(rettv->vval.v_string); /* simplify in place */
17872 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873}
17874
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017875#ifdef FEAT_FLOAT
17876/*
17877 * "sin()" function
17878 */
17879 static void
17880f_sin(argvars, rettv)
17881 typval_T *argvars;
17882 typval_T *rettv;
17883{
17884 float_T f;
17885
17886 rettv->v_type = VAR_FLOAT;
17887 if (get_float_arg(argvars, &f) == OK)
17888 rettv->vval.v_float = sin(f);
17889 else
17890 rettv->vval.v_float = 0.0;
17891}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017892
17893/*
17894 * "sinh()" function
17895 */
17896 static void
17897f_sinh(argvars, rettv)
17898 typval_T *argvars;
17899 typval_T *rettv;
17900{
17901 float_T f;
17902
17903 rettv->v_type = VAR_FLOAT;
17904 if (get_float_arg(argvars, &f) == OK)
17905 rettv->vval.v_float = sinh(f);
17906 else
17907 rettv->vval.v_float = 0.0;
17908}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017909#endif
17910
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911static int
17912#ifdef __BORLANDC__
17913 _RTLENTRYF
17914#endif
17915 item_compare __ARGS((const void *s1, const void *s2));
17916static int
17917#ifdef __BORLANDC__
17918 _RTLENTRYF
17919#endif
17920 item_compare2 __ARGS((const void *s1, const void *s2));
17921
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017922/* struct used in the array that's given to qsort() */
17923typedef struct
17924{
17925 listitem_T *item;
17926 int idx;
17927} sortItem_T;
17928
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017930static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017932static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017934static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017935static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017936#define ITEM_COMPARE_FAIL 999
17937
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017939 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 static int
17942#ifdef __BORLANDC__
17943_RTLENTRYF
17944#endif
17945item_compare(s1, s2)
17946 const void *s1;
17947 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017949 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017950 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017952 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 int res;
17954 char_u numbuf1[NUMBUFLEN];
17955 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017957 si1 = (sortItem_T *)s1;
17958 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017959 tv1 = &si1->item->li_tv;
17960 tv2 = &si2->item->li_tv;
17961 /* tv2string() puts quotes around a string and allocates memory. Don't do
17962 * that for string variables. Use a single quote when comparing with a
17963 * non-string to do what the docs promise. */
17964 if (tv1->v_type == VAR_STRING)
17965 {
17966 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17967 p1 = (char_u *)"'";
17968 else
17969 p1 = tv1->vval.v_string;
17970 }
17971 else
17972 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17973 if (tv2->v_type == VAR_STRING)
17974 {
17975 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17976 p2 = (char_u *)"'";
17977 else
17978 p2 = tv2->vval.v_string;
17979 }
17980 else
17981 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017982 if (p1 == NULL)
17983 p1 = (char_u *)"";
17984 if (p2 == NULL)
17985 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017986 if (!item_compare_numeric)
17987 {
17988 if (item_compare_ic)
17989 res = STRICMP(p1, p2);
17990 else
17991 res = STRCMP(p1, p2);
17992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017994 {
17995 double n1, n2;
17996 n1 = strtod((char *)p1, (char **)&p1);
17997 n2 = strtod((char *)p2, (char **)&p2);
17998 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17999 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018000
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018001 /* When the result would be zero, compare the item indexes. Makes the
18002 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018003 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018004 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018005
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 vim_free(tofree1);
18007 vim_free(tofree2);
18008 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009}
18010
18011 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012#ifdef __BORLANDC__
18013_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015item_compare2(s1, s2)
18016 const void *s1;
18017 const void *s2;
18018{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018019 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018022 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018025 /* shortcut after failure in previous call; compare all items equal */
18026 if (item_compare_func_err)
18027 return 0;
18028
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018029 si1 = (sortItem_T *)s1;
18030 si2 = (sortItem_T *)s2;
18031
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018032 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018033 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018034 copy_tv(&si1->item->li_tv, &argv[0]);
18035 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036
18037 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018038 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018039 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18040 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041 clear_tv(&argv[0]);
18042 clear_tv(&argv[1]);
18043
18044 if (res == FAIL)
18045 res = ITEM_COMPARE_FAIL;
18046 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018047 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18048 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018049 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018050 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018051
18052 /* When the result would be zero, compare the pointers themselves. Makes
18053 * the sort stable. */
18054 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018055 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018056
Bram Moolenaar0d660222005-01-07 21:51:51 +000018057 return res;
18058}
18059
18060/*
18061 * "sort({list})" function
18062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018064do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018065 typval_T *argvars;
18066 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018067 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068{
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 list_T *l;
18070 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018071 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 long len;
18073 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 else
18078 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018080 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018081 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18082 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 return;
18084 rettv->vval.v_list = l;
18085 rettv->v_type = VAR_LIST;
18086 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 len = list_len(l);
18089 if (len <= 1)
18090 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091
Bram Moolenaar0d660222005-01-07 21:51:51 +000018092 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018093 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018095 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018096 if (argvars[1].v_type != VAR_UNKNOWN)
18097 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018098 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101 else
18102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 int error = FALSE;
18104
18105 i = get_tv_number_chk(&argvars[1], &error);
18106 if (error)
18107 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108 if (i == 1)
18109 item_compare_ic = TRUE;
18110 else
18111 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018112 if (item_compare_func != NULL)
18113 {
18114 if (STRCMP(item_compare_func, "n") == 0)
18115 {
18116 item_compare_func = NULL;
18117 item_compare_numeric = TRUE;
18118 }
18119 else if (STRCMP(item_compare_func, "i") == 0)
18120 {
18121 item_compare_func = NULL;
18122 item_compare_ic = TRUE;
18123 }
18124 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018125 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018126
18127 if (argvars[2].v_type != VAR_UNKNOWN)
18128 {
18129 /* optional third argument: {dict} */
18130 if (argvars[2].v_type != VAR_DICT)
18131 {
18132 EMSG(_(e_dictreq));
18133 return;
18134 }
18135 item_compare_selfdict = argvars[2].vval.v_dict;
18136 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138
Bram Moolenaar0d660222005-01-07 21:51:51 +000018139 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018140 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018141 if (ptrs == NULL)
18142 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143
Bram Moolenaar327aa022014-03-25 18:24:23 +010018144 i = 0;
18145 if (sort)
18146 {
18147 /* sort(): ptrs will be the list to sort */
18148 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018149 {
18150 ptrs[i].item = li;
18151 ptrs[i].idx = i;
18152 ++i;
18153 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018154
18155 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018156 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018157 /* test the compare function */
18158 if (item_compare_func != NULL
18159 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018161 EMSG(_("E702: Sort compare function failed"));
18162 else
18163 {
18164 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018165 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018166 item_compare_func == NULL ? item_compare : item_compare2);
18167
18168 if (!item_compare_func_err)
18169 {
18170 /* Clear the List and append the items in sorted order. */
18171 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18172 l->lv_len = 0;
18173 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018174 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018175 }
18176 }
18177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018180 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18181
18182 /* f_uniq(): ptrs will be a stack of items to remove */
18183 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018184 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018185 item_compare_func_ptr = item_compare_func
18186 ? item_compare2 : item_compare;
18187
18188 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18189 li = li->li_next)
18190 {
18191 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18192 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018193 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018194 if (item_compare_func_err)
18195 {
18196 EMSG(_("E882: Uniq compare function failed"));
18197 break;
18198 }
18199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 if (!item_compare_func_err)
18202 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018203 while (--i >= 0)
18204 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018205 li = ptrs[i].item->li_next;
18206 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018207 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018208 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018209 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018210 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018211 list_fix_watch(l, li);
18212 listitem_free(li);
18213 l->lv_len--;
18214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018215 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018216 }
18217
18218 vim_free(ptrs);
18219 }
18220}
18221
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018222/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018223 * "sort({list})" function
18224 */
18225 static void
18226f_sort(argvars, rettv)
18227 typval_T *argvars;
18228 typval_T *rettv;
18229{
18230 do_sort_uniq(argvars, rettv, TRUE);
18231}
18232
18233/*
18234 * "uniq({list})" function
18235 */
18236 static void
18237f_uniq(argvars, rettv)
18238 typval_T *argvars;
18239 typval_T *rettv;
18240{
18241 do_sort_uniq(argvars, rettv, FALSE);
18242}
18243
18244/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018245 * "soundfold({word})" function
18246 */
18247 static void
18248f_soundfold(argvars, rettv)
18249 typval_T *argvars;
18250 typval_T *rettv;
18251{
18252 char_u *s;
18253
18254 rettv->v_type = VAR_STRING;
18255 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018256#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018257 rettv->vval.v_string = eval_soundfold(s);
18258#else
18259 rettv->vval.v_string = vim_strsave(s);
18260#endif
18261}
18262
18263/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018264 * "spellbadword()" function
18265 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018266 static void
18267f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018268 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018269 typval_T *rettv;
18270{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018271 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018272 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018273 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018274
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018275 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018276 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018277
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018278#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018279 if (argvars[0].v_type == VAR_UNKNOWN)
18280 {
18281 /* Find the start and length of the badly spelled word. */
18282 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18283 if (len != 0)
18284 word = ml_get_cursor();
18285 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018286 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018287 {
18288 char_u *str = get_tv_string_chk(&argvars[0]);
18289 int capcol = -1;
18290
18291 if (str != NULL)
18292 {
18293 /* Check the argument for spelling. */
18294 while (*str != NUL)
18295 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018296 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018297 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018298 {
18299 word = str;
18300 break;
18301 }
18302 str += len;
18303 }
18304 }
18305 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018306#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018308 list_append_string(rettv->vval.v_list, word, len);
18309 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018310 attr == HLF_SPB ? "bad" :
18311 attr == HLF_SPR ? "rare" :
18312 attr == HLF_SPL ? "local" :
18313 attr == HLF_SPC ? "caps" :
18314 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018315}
18316
18317/*
18318 * "spellsuggest()" function
18319 */
18320 static void
18321f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018322 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018323 typval_T *rettv;
18324{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018325#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018326 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018327 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018328 int maxcount;
18329 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018330 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018331 listitem_T *li;
18332 int need_capital = FALSE;
18333#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018334
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018335 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018336 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018337
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018338#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018339 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018340 {
18341 str = get_tv_string(&argvars[0]);
18342 if (argvars[1].v_type != VAR_UNKNOWN)
18343 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018344 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018345 if (maxcount <= 0)
18346 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018347 if (argvars[2].v_type != VAR_UNKNOWN)
18348 {
18349 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18350 if (typeerr)
18351 return;
18352 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018353 }
18354 else
18355 maxcount = 25;
18356
Bram Moolenaar4770d092006-01-12 23:22:24 +000018357 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018358
18359 for (i = 0; i < ga.ga_len; ++i)
18360 {
18361 str = ((char_u **)ga.ga_data)[i];
18362
18363 li = listitem_alloc();
18364 if (li == NULL)
18365 vim_free(str);
18366 else
18367 {
18368 li->li_tv.v_type = VAR_STRING;
18369 li->li_tv.v_lock = 0;
18370 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018371 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018372 }
18373 }
18374 ga_clear(&ga);
18375 }
18376#endif
18377}
18378
Bram Moolenaar0d660222005-01-07 21:51:51 +000018379 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018380f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018381 typval_T *argvars;
18382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383{
18384 char_u *str;
18385 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018386 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018387 regmatch_T regmatch;
18388 char_u patbuf[NUMBUFLEN];
18389 char_u *save_cpo;
18390 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018391 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018392 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018393 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394
18395 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18396 save_cpo = p_cpo;
18397 p_cpo = (char_u *)"";
18398
18399 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018400 if (argvars[1].v_type != VAR_UNKNOWN)
18401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018402 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18403 if (pat == NULL)
18404 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018406 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018407 }
18408 if (pat == NULL || *pat == NUL)
18409 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018410
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018411 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018413 if (typeerr)
18414 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18417 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018419 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018420 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018422 if (*str == NUL)
18423 match = FALSE; /* empty item at the end */
18424 else
18425 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426 if (match)
18427 end = regmatch.startp[0];
18428 else
18429 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018430 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18431 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018432 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018433 if (list_append_string(rettv->vval.v_list, str,
18434 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018435 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018436 }
18437 if (!match)
18438 break;
18439 /* Advance to just after the match. */
18440 if (regmatch.endp[0] > str)
18441 col = 0;
18442 else
18443 {
18444 /* Don't get stuck at the same match. */
18445#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018446 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447#else
18448 col = 1;
18449#endif
18450 }
18451 str = regmatch.endp[0];
18452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453
Bram Moolenaar473de612013-06-08 18:19:48 +020018454 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458}
18459
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018460#ifdef FEAT_FLOAT
18461/*
18462 * "sqrt()" function
18463 */
18464 static void
18465f_sqrt(argvars, rettv)
18466 typval_T *argvars;
18467 typval_T *rettv;
18468{
18469 float_T f;
18470
18471 rettv->v_type = VAR_FLOAT;
18472 if (get_float_arg(argvars, &f) == OK)
18473 rettv->vval.v_float = sqrt(f);
18474 else
18475 rettv->vval.v_float = 0.0;
18476}
18477
18478/*
18479 * "str2float()" function
18480 */
18481 static void
18482f_str2float(argvars, rettv)
18483 typval_T *argvars;
18484 typval_T *rettv;
18485{
18486 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18487
18488 if (*p == '+')
18489 p = skipwhite(p + 1);
18490 (void)string2float(p, &rettv->vval.v_float);
18491 rettv->v_type = VAR_FLOAT;
18492}
18493#endif
18494
Bram Moolenaar2c932302006-03-18 21:42:09 +000018495/*
18496 * "str2nr()" function
18497 */
18498 static void
18499f_str2nr(argvars, rettv)
18500 typval_T *argvars;
18501 typval_T *rettv;
18502{
18503 int base = 10;
18504 char_u *p;
18505 long n;
18506
18507 if (argvars[1].v_type != VAR_UNKNOWN)
18508 {
18509 base = get_tv_number(&argvars[1]);
18510 if (base != 8 && base != 10 && base != 16)
18511 {
18512 EMSG(_(e_invarg));
18513 return;
18514 }
18515 }
18516
18517 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018518 if (*p == '+')
18519 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018520 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018521 rettv->vval.v_number = n;
18522}
18523
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524#ifdef HAVE_STRFTIME
18525/*
18526 * "strftime({format}[, {time}])" function
18527 */
18528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018530 typval_T *argvars;
18531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532{
18533 char_u result_buf[256];
18534 struct tm *curtime;
18535 time_t seconds;
18536 char_u *p;
18537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018540 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018541 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 seconds = time(NULL);
18543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018544 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 curtime = localtime(&seconds);
18546 /* MSVC returns NULL for an invalid value of seconds. */
18547 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 else
18550 {
18551# ifdef FEAT_MBYTE
18552 vimconv_T conv;
18553 char_u *enc;
18554
18555 conv.vc_type = CONV_NONE;
18556 enc = enc_locale();
18557 convert_setup(&conv, p_enc, enc);
18558 if (conv.vc_type != CONV_NONE)
18559 p = string_convert(&conv, p, NULL);
18560# endif
18561 if (p != NULL)
18562 (void)strftime((char *)result_buf, sizeof(result_buf),
18563 (char *)p, curtime);
18564 else
18565 result_buf[0] = NUL;
18566
18567# ifdef FEAT_MBYTE
18568 if (conv.vc_type != CONV_NONE)
18569 vim_free(p);
18570 convert_setup(&conv, enc, p_enc);
18571 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 else
18574# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576
18577# ifdef FEAT_MBYTE
18578 /* Release conversion descriptors */
18579 convert_setup(&conv, NULL, NULL);
18580 vim_free(enc);
18581# endif
18582 }
18583}
18584#endif
18585
18586/*
18587 * "stridx()" function
18588 */
18589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018591 typval_T *argvars;
18592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593{
18594 char_u buf[NUMBUFLEN];
18595 char_u *needle;
18596 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018597 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018599 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018601 needle = get_tv_string_chk(&argvars[1]);
18602 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018603 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 if (needle == NULL || haystack == NULL)
18605 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606
Bram Moolenaar33570922005-01-25 22:26:29 +000018607 if (argvars[2].v_type != VAR_UNKNOWN)
18608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 int error = FALSE;
18610
18611 start_idx = get_tv_number_chk(&argvars[2], &error);
18612 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018614 if (start_idx >= 0)
18615 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018616 }
18617
18618 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18619 if (pos != NULL)
18620 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621}
18622
18623/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018624 * "string()" function
18625 */
18626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 typval_T *argvars;
18629 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018630{
18631 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018632 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018635 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018636 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018637 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639}
18640
18641/*
18642 * "strlen()" function
18643 */
18644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018646 typval_T *argvars;
18647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 rettv->vval.v_number = (varnumber_T)(STRLEN(
18650 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651}
18652
18653/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018654 * "strchars()" function
18655 */
18656 static void
18657f_strchars(argvars, rettv)
18658 typval_T *argvars;
18659 typval_T *rettv;
18660{
18661 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018662 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018663#ifdef FEAT_MBYTE
18664 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018665 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018666#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018667
18668 if (argvars[1].v_type != VAR_UNKNOWN)
18669 skipcc = get_tv_number_chk(&argvars[1], NULL);
18670 if (skipcc < 0 || skipcc > 1)
18671 EMSG(_(e_invarg));
18672 else
18673 {
18674#ifdef FEAT_MBYTE
18675 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18676 while (*s != NUL)
18677 {
18678 func_mb_ptr2char_adv(&s);
18679 ++len;
18680 }
18681 rettv->vval.v_number = len;
18682#else
18683 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18684#endif
18685 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018686}
18687
18688/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018689 * "strdisplaywidth()" function
18690 */
18691 static void
18692f_strdisplaywidth(argvars, rettv)
18693 typval_T *argvars;
18694 typval_T *rettv;
18695{
18696 char_u *s = get_tv_string(&argvars[0]);
18697 int col = 0;
18698
18699 if (argvars[1].v_type != VAR_UNKNOWN)
18700 col = get_tv_number(&argvars[1]);
18701
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018702 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018703}
18704
18705/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018706 * "strwidth()" function
18707 */
18708 static void
18709f_strwidth(argvars, rettv)
18710 typval_T *argvars;
18711 typval_T *rettv;
18712{
18713 char_u *s = get_tv_string(&argvars[0]);
18714
18715 rettv->vval.v_number = (varnumber_T)(
18716#ifdef FEAT_MBYTE
18717 mb_string2cells(s, -1)
18718#else
18719 STRLEN(s)
18720#endif
18721 );
18722}
18723
18724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * "strpart()" function
18726 */
18727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018729 typval_T *argvars;
18730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
18732 char_u *p;
18733 int n;
18734 int len;
18735 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 slen = (int)STRLEN(p);
18740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018741 n = get_tv_number_chk(&argvars[1], &error);
18742 if (error)
18743 len = 0;
18744 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 else
18747 len = slen - n; /* default len: all bytes that are available. */
18748
18749 /*
18750 * Only return the overlap between the specified part and the actual
18751 * string.
18752 */
18753 if (n < 0)
18754 {
18755 len += n;
18756 n = 0;
18757 }
18758 else if (n > slen)
18759 n = slen;
18760 if (len < 0)
18761 len = 0;
18762 else if (n + len > slen)
18763 len = slen - n;
18764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018765 rettv->v_type = VAR_STRING;
18766 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767}
18768
18769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018770 * "strridx()" function
18771 */
18772 static void
18773f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 typval_T *argvars;
18775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018776{
18777 char_u buf[NUMBUFLEN];
18778 char_u *needle;
18779 char_u *haystack;
18780 char_u *rest;
18781 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018782 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018784 needle = get_tv_string_chk(&argvars[1]);
18785 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018786
18787 rettv->vval.v_number = -1;
18788 if (needle == NULL || haystack == NULL)
18789 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018790
18791 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018792 if (argvars[2].v_type != VAR_UNKNOWN)
18793 {
18794 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018795 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018796 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018797 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018798 }
18799 else
18800 end_idx = haystack_len;
18801
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018803 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018804 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018805 lastmatch = haystack + end_idx;
18806 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018807 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018808 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018809 for (rest = haystack; *rest != '\0'; ++rest)
18810 {
18811 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018812 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813 break;
18814 lastmatch = rest;
18815 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018816 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018817
18818 if (lastmatch == NULL)
18819 rettv->vval.v_number = -1;
18820 else
18821 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18822}
18823
18824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 * "strtrans()" function
18826 */
18827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018828f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018829 typval_T *argvars;
18830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018832 rettv->v_type = VAR_STRING;
18833 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834}
18835
18836/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018837 * "submatch()" function
18838 */
18839 static void
18840f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 typval_T *argvars;
18842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018843{
Bram Moolenaar41571762014-04-02 19:00:58 +020018844 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018845 int no;
18846 int retList = 0;
18847
18848 no = (int)get_tv_number_chk(&argvars[0], &error);
18849 if (error)
18850 return;
18851 error = FALSE;
18852 if (argvars[1].v_type != VAR_UNKNOWN)
18853 retList = get_tv_number_chk(&argvars[1], &error);
18854 if (error)
18855 return;
18856
18857 if (retList == 0)
18858 {
18859 rettv->v_type = VAR_STRING;
18860 rettv->vval.v_string = reg_submatch(no);
18861 }
18862 else
18863 {
18864 rettv->v_type = VAR_LIST;
18865 rettv->vval.v_list = reg_submatch_list(no);
18866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018867}
18868
18869/*
18870 * "substitute()" function
18871 */
18872 static void
18873f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 typval_T *argvars;
18875 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876{
18877 char_u patbuf[NUMBUFLEN];
18878 char_u subbuf[NUMBUFLEN];
18879 char_u flagsbuf[NUMBUFLEN];
18880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018881 char_u *str = get_tv_string_chk(&argvars[0]);
18882 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18883 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18884 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18885
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018887 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18888 rettv->vval.v_string = NULL;
18889 else
18890 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891}
18892
18893/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018894 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900{
18901 int id = 0;
18902#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018903 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 long col;
18905 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018906 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018908 lnum = get_tv_lnum(argvars); /* -1 on type error */
18909 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18910 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018912 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018913 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018914 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915#endif
18916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018917 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918}
18919
18920/*
18921 * "synIDattr(id, what [, mode])" function
18922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927{
18928 char_u *p = NULL;
18929#ifdef FEAT_SYN_HL
18930 int id;
18931 char_u *what;
18932 char_u *mode;
18933 char_u modebuf[NUMBUFLEN];
18934 int modec;
18935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018936 id = get_tv_number(&argvars[0]);
18937 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018942 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 modec = 0; /* replace invalid with current */
18944 }
18945 else
18946 {
18947#ifdef FEAT_GUI
18948 if (gui.in_use)
18949 modec = 'g';
18950 else
18951#endif
18952 if (t_colors > 1)
18953 modec = 'c';
18954 else
18955 modec = 't';
18956 }
18957
18958
18959 switch (TOLOWER_ASC(what[0]))
18960 {
18961 case 'b':
18962 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18963 p = highlight_color(id, what, modec);
18964 else /* bold */
18965 p = highlight_has_attr(id, HL_BOLD, modec);
18966 break;
18967
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018968 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 p = highlight_color(id, what, modec);
18970 break;
18971
18972 case 'i':
18973 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18974 p = highlight_has_attr(id, HL_INVERSE, modec);
18975 else /* italic */
18976 p = highlight_has_attr(id, HL_ITALIC, modec);
18977 break;
18978
18979 case 'n': /* name */
18980 p = get_highlight_name(NULL, id - 1);
18981 break;
18982
18983 case 'r': /* reverse */
18984 p = highlight_has_attr(id, HL_INVERSE, modec);
18985 break;
18986
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018987 case 's':
18988 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18989 p = highlight_color(id, what, modec);
18990 else /* standout */
18991 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 break;
18993
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018994 case 'u':
18995 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18996 /* underline */
18997 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18998 else
18999 /* undercurl */
19000 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 break;
19002 }
19003
19004 if (p != NULL)
19005 p = vim_strsave(p);
19006#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019007 rettv->v_type = VAR_STRING;
19008 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009}
19010
19011/*
19012 * "synIDtrans(id)" function
19013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019015f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018{
19019 int id;
19020
19021#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019022 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023
19024 if (id > 0)
19025 id = syn_get_final_id(id);
19026 else
19027#endif
19028 id = 0;
19029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019030 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031}
19032
19033/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019034 * "synconcealed(lnum, col)" function
19035 */
19036 static void
19037f_synconcealed(argvars, rettv)
19038 typval_T *argvars UNUSED;
19039 typval_T *rettv;
19040{
19041#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19042 long lnum;
19043 long col;
19044 int syntax_flags = 0;
19045 int cchar;
19046 int matchid = 0;
19047 char_u str[NUMBUFLEN];
19048#endif
19049
19050 rettv->v_type = VAR_LIST;
19051 rettv->vval.v_list = NULL;
19052
19053#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19054 lnum = get_tv_lnum(argvars); /* -1 on type error */
19055 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19056
19057 vim_memset(str, NUL, sizeof(str));
19058
19059 if (rettv_list_alloc(rettv) != FAIL)
19060 {
19061 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19062 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19063 && curwin->w_p_cole > 0)
19064 {
19065 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19066 syntax_flags = get_syntax_info(&matchid);
19067
19068 /* get the conceal character */
19069 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19070 {
19071 cchar = syn_get_sub_char();
19072 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19073 cchar = lcs_conceal;
19074 if (cchar != NUL)
19075 {
19076# ifdef FEAT_MBYTE
19077 if (has_mbyte)
19078 (*mb_char2bytes)(cchar, str);
19079 else
19080# endif
19081 str[0] = cchar;
19082 }
19083 }
19084 }
19085
19086 list_append_number(rettv->vval.v_list,
19087 (syntax_flags & HL_CONCEAL) != 0);
19088 /* -1 to auto-determine strlen */
19089 list_append_string(rettv->vval.v_list, str, -1);
19090 list_append_number(rettv->vval.v_list, matchid);
19091 }
19092#endif
19093}
19094
19095/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019096 * "synstack(lnum, col)" function
19097 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019098 static void
19099f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019100 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019101 typval_T *rettv;
19102{
19103#ifdef FEAT_SYN_HL
19104 long lnum;
19105 long col;
19106 int i;
19107 int id;
19108#endif
19109
19110 rettv->v_type = VAR_LIST;
19111 rettv->vval.v_list = NULL;
19112
19113#ifdef FEAT_SYN_HL
19114 lnum = get_tv_lnum(argvars); /* -1 on type error */
19115 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19116
19117 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019118 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019119 && rettv_list_alloc(rettv) != FAIL)
19120 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019121 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019122 for (i = 0; ; ++i)
19123 {
19124 id = syn_get_stack_item(i);
19125 if (id < 0)
19126 break;
19127 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19128 break;
19129 }
19130 }
19131#endif
19132}
19133
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019135get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *argvars;
19137 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019138 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019140 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019142 char_u *infile = NULL;
19143 char_u buf[NUMBUFLEN];
19144 int err = FALSE;
19145 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019146 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019147 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019149 rettv->v_type = VAR_STRING;
19150 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019151 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019152 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019154 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019155 {
19156 /*
19157 * Write the string to a temp file, to be used for input of the shell
19158 * command.
19159 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019160 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019161 {
19162 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019163 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019164 }
19165
19166 fd = mch_fopen((char *)infile, WRITEBIN);
19167 if (fd == NULL)
19168 {
19169 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019170 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019171 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019172 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019173 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019174 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19175 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019176 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019177 else
19178 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019179 size_t len;
19180
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019181 p = get_tv_string_buf_chk(&argvars[1], buf);
19182 if (p == NULL)
19183 {
19184 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019185 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019186 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019187 len = STRLEN(p);
19188 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019189 err = TRUE;
19190 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019191 if (fclose(fd) != 0)
19192 err = TRUE;
19193 if (err)
19194 {
19195 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019196 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019197 }
19198 }
19199
Bram Moolenaar52a72462014-08-29 15:53:52 +020019200 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19201 * echoes typeahead, that messes up the display. */
19202 if (!msg_silent)
19203 flags += SHELL_COOKED;
19204
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019205 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019207 int len;
19208 listitem_T *li;
19209 char_u *s = NULL;
19210 char_u *start;
19211 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019212 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213
Bram Moolenaar52a72462014-08-29 15:53:52 +020019214 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019215 if (res == NULL)
19216 goto errret;
19217
19218 list = list_alloc();
19219 if (list == NULL)
19220 goto errret;
19221
19222 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019224 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019225 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019226 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019227 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019228
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019229 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019230 if (s == NULL)
19231 goto errret;
19232
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019233 for (p = s; start < end; ++p, ++start)
19234 *p = *start == NUL ? NL : *start;
19235 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019236
19237 li = listitem_alloc();
19238 if (li == NULL)
19239 {
19240 vim_free(s);
19241 goto errret;
19242 }
19243 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019244 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019245 li->li_tv.vval.v_string = s;
19246 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019248
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019249 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019250 rettv->v_type = VAR_LIST;
19251 rettv->vval.v_list = list;
19252 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019254 else
19255 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019256 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019257#ifdef USE_CR
19258 /* translate <CR> into <NL> */
19259 if (res != NULL)
19260 {
19261 char_u *s;
19262
19263 for (s = res; *s; ++s)
19264 {
19265 if (*s == CAR)
19266 *s = NL;
19267 }
19268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269#else
19270# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271 /* translate <CR><NL> into <NL> */
19272 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019274 char_u *s, *d;
19275
19276 d = res;
19277 for (s = res; *s; ++s)
19278 {
19279 if (s[0] == CAR && s[1] == NL)
19280 ++s;
19281 *d++ = *s;
19282 }
19283 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285# endif
19286#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019287 rettv->vval.v_string = res;
19288 res = NULL;
19289 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019290
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019291errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019292 if (infile != NULL)
19293 {
19294 mch_remove(infile);
19295 vim_free(infile);
19296 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019297 if (res != NULL)
19298 vim_free(res);
19299 if (list != NULL)
19300 list_free(list, TRUE);
19301}
19302
19303/*
19304 * "system()" function
19305 */
19306 static void
19307f_system(argvars, rettv)
19308 typval_T *argvars;
19309 typval_T *rettv;
19310{
19311 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19312}
19313
19314/*
19315 * "systemlist()" function
19316 */
19317 static void
19318f_systemlist(argvars, rettv)
19319 typval_T *argvars;
19320 typval_T *rettv;
19321{
19322 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323}
19324
19325/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019326 * "tabpagebuflist()" function
19327 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019328 static void
19329f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019330 typval_T *argvars UNUSED;
19331 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019332{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019333#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019334 tabpage_T *tp;
19335 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019336
19337 if (argvars[0].v_type == VAR_UNKNOWN)
19338 wp = firstwin;
19339 else
19340 {
19341 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19342 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019343 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019344 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019345 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019346 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019347 for (; wp != NULL; wp = wp->w_next)
19348 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019349 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019350 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019351 }
19352#endif
19353}
19354
19355
19356/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019357 * "tabpagenr()" function
19358 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019359 static void
19360f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019361 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019362 typval_T *rettv;
19363{
19364 int nr = 1;
19365#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019366 char_u *arg;
19367
19368 if (argvars[0].v_type != VAR_UNKNOWN)
19369 {
19370 arg = get_tv_string_chk(&argvars[0]);
19371 nr = 0;
19372 if (arg != NULL)
19373 {
19374 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019375 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019376 else
19377 EMSG2(_(e_invexpr2), arg);
19378 }
19379 }
19380 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019381 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019382#endif
19383 rettv->vval.v_number = nr;
19384}
19385
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019386
19387#ifdef FEAT_WINDOWS
19388static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19389
19390/*
19391 * Common code for tabpagewinnr() and winnr().
19392 */
19393 static int
19394get_winnr(tp, argvar)
19395 tabpage_T *tp;
19396 typval_T *argvar;
19397{
19398 win_T *twin;
19399 int nr = 1;
19400 win_T *wp;
19401 char_u *arg;
19402
19403 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19404 if (argvar->v_type != VAR_UNKNOWN)
19405 {
19406 arg = get_tv_string_chk(argvar);
19407 if (arg == NULL)
19408 nr = 0; /* type error; errmsg already given */
19409 else if (STRCMP(arg, "$") == 0)
19410 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19411 else if (STRCMP(arg, "#") == 0)
19412 {
19413 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19414 if (twin == NULL)
19415 nr = 0;
19416 }
19417 else
19418 {
19419 EMSG2(_(e_invexpr2), arg);
19420 nr = 0;
19421 }
19422 }
19423
19424 if (nr > 0)
19425 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19426 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019427 {
19428 if (wp == NULL)
19429 {
19430 /* didn't find it in this tabpage */
19431 nr = 0;
19432 break;
19433 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019434 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019435 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019436 return nr;
19437}
19438#endif
19439
19440/*
19441 * "tabpagewinnr()" function
19442 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019443 static void
19444f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019445 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019446 typval_T *rettv;
19447{
19448 int nr = 1;
19449#ifdef FEAT_WINDOWS
19450 tabpage_T *tp;
19451
19452 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19453 if (tp == NULL)
19454 nr = 0;
19455 else
19456 nr = get_winnr(tp, &argvars[1]);
19457#endif
19458 rettv->vval.v_number = nr;
19459}
19460
19461
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019462/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019463 * "tagfiles()" function
19464 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019465 static void
19466f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019467 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019468 typval_T *rettv;
19469{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019470 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019471 tagname_T tn;
19472 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019473
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019474 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019475 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019476 fname = alloc(MAXPATHL);
19477 if (fname == NULL)
19478 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019480 for (first = TRUE; ; first = FALSE)
19481 if (get_tagfname(&tn, first, fname) == FAIL
19482 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019483 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019484 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019485 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019486}
19487
19488/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019489 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019490 */
19491 static void
19492f_taglist(argvars, rettv)
19493 typval_T *argvars;
19494 typval_T *rettv;
19495{
19496 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019497
19498 tag_pattern = get_tv_string(&argvars[0]);
19499
19500 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019501 if (*tag_pattern == NUL)
19502 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019504 if (rettv_list_alloc(rettv) == OK)
19505 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019506}
19507
19508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 * "tempname()" function
19510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019513 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515{
19516 static int x = 'A';
19517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019519 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520
19521 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19522 * names. Skip 'I' and 'O', they are used for shell redirection. */
19523 do
19524 {
19525 if (x == 'Z')
19526 x = '0';
19527 else if (x == '9')
19528 x = 'A';
19529 else
19530 {
19531#ifdef EBCDIC
19532 if (x == 'I')
19533 x = 'J';
19534 else if (x == 'R')
19535 x = 'S';
19536 else
19537#endif
19538 ++x;
19539 }
19540 } while (x == 'I' || x == 'O');
19541}
19542
19543/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019544 * "test(list)" function: Just checking the walls...
19545 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019546 static void
19547f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019548 typval_T *argvars UNUSED;
19549 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019550{
19551 /* Used for unit testing. Change the code below to your liking. */
19552#if 0
19553 listitem_T *li;
19554 list_T *l;
19555 char_u *bad, *good;
19556
19557 if (argvars[0].v_type != VAR_LIST)
19558 return;
19559 l = argvars[0].vval.v_list;
19560 if (l == NULL)
19561 return;
19562 li = l->lv_first;
19563 if (li == NULL)
19564 return;
19565 bad = get_tv_string(&li->li_tv);
19566 li = li->li_next;
19567 if (li == NULL)
19568 return;
19569 good = get_tv_string(&li->li_tv);
19570 rettv->vval.v_number = test_edit_score(bad, good);
19571#endif
19572}
19573
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019574#ifdef FEAT_FLOAT
19575/*
19576 * "tan()" function
19577 */
19578 static void
19579f_tan(argvars, rettv)
19580 typval_T *argvars;
19581 typval_T *rettv;
19582{
19583 float_T f;
19584
19585 rettv->v_type = VAR_FLOAT;
19586 if (get_float_arg(argvars, &f) == OK)
19587 rettv->vval.v_float = tan(f);
19588 else
19589 rettv->vval.v_float = 0.0;
19590}
19591
19592/*
19593 * "tanh()" function
19594 */
19595 static void
19596f_tanh(argvars, rettv)
19597 typval_T *argvars;
19598 typval_T *rettv;
19599{
19600 float_T f;
19601
19602 rettv->v_type = VAR_FLOAT;
19603 if (get_float_arg(argvars, &f) == OK)
19604 rettv->vval.v_float = tanh(f);
19605 else
19606 rettv->vval.v_float = 0.0;
19607}
19608#endif
19609
Bram Moolenaard52d9742005-08-21 22:20:28 +000019610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 * "tolower(string)" function
19612 */
19613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 typval_T *argvars;
19616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617{
19618 char_u *p;
19619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620 p = vim_strsave(get_tv_string(&argvars[0]));
19621 rettv->v_type = VAR_STRING;
19622 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623
19624 if (p != NULL)
19625 while (*p != NUL)
19626 {
19627#ifdef FEAT_MBYTE
19628 int l;
19629
19630 if (enc_utf8)
19631 {
19632 int c, lc;
19633
19634 c = utf_ptr2char(p);
19635 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019636 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 /* TODO: reallocate string when byte count changes. */
19638 if (utf_char2len(lc) == l)
19639 utf_char2bytes(lc, p);
19640 p += l;
19641 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019642 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 p += l; /* skip multi-byte character */
19644 else
19645#endif
19646 {
19647 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19648 ++p;
19649 }
19650 }
19651}
19652
19653/*
19654 * "toupper(string)" function
19655 */
19656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 typval_T *argvars;
19659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019662 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663}
19664
19665/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019666 * "tr(string, fromstr, tostr)" function
19667 */
19668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019669f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 typval_T *argvars;
19671 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019672{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019673 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019674 char_u *fromstr;
19675 char_u *tostr;
19676 char_u *p;
19677#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019678 int inlen;
19679 int fromlen;
19680 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019681 int idx;
19682 char_u *cpstr;
19683 int cplen;
19684 int first = TRUE;
19685#endif
19686 char_u buf[NUMBUFLEN];
19687 char_u buf2[NUMBUFLEN];
19688 garray_T ga;
19689
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019690 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019691 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19692 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019693
19694 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 rettv->v_type = VAR_STRING;
19696 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019697 if (fromstr == NULL || tostr == NULL)
19698 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019699 ga_init2(&ga, (int)sizeof(char), 80);
19700
19701#ifdef FEAT_MBYTE
19702 if (!has_mbyte)
19703#endif
19704 /* not multi-byte: fromstr and tostr must be the same length */
19705 if (STRLEN(fromstr) != STRLEN(tostr))
19706 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019707#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019708error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019709#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019710 EMSG2(_(e_invarg2), fromstr);
19711 ga_clear(&ga);
19712 return;
19713 }
19714
19715 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019716 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019717 {
19718#ifdef FEAT_MBYTE
19719 if (has_mbyte)
19720 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019721 inlen = (*mb_ptr2len)(in_str);
19722 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019723 cplen = inlen;
19724 idx = 0;
19725 for (p = fromstr; *p != NUL; p += fromlen)
19726 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019727 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019728 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019729 {
19730 for (p = tostr; *p != NUL; p += tolen)
19731 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019732 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019733 if (idx-- == 0)
19734 {
19735 cplen = tolen;
19736 cpstr = p;
19737 break;
19738 }
19739 }
19740 if (*p == NUL) /* tostr is shorter than fromstr */
19741 goto error;
19742 break;
19743 }
19744 ++idx;
19745 }
19746
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019747 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019748 {
19749 /* Check that fromstr and tostr have the same number of
19750 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019751 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019752 first = FALSE;
19753 for (p = tostr; *p != NUL; p += tolen)
19754 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019755 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019756 --idx;
19757 }
19758 if (idx != 0)
19759 goto error;
19760 }
19761
Bram Moolenaarcde88542015-08-11 19:14:00 +020019762 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019763 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019764 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019765
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019766 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019767 }
19768 else
19769#endif
19770 {
19771 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019772 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019773 if (p != NULL)
19774 ga_append(&ga, tostr[p - fromstr]);
19775 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019776 ga_append(&ga, *in_str);
19777 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019778 }
19779 }
19780
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019781 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019782 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019783 ga_append(&ga, NUL);
19784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019786}
19787
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019788#ifdef FEAT_FLOAT
19789/*
19790 * "trunc({float})" function
19791 */
19792 static void
19793f_trunc(argvars, rettv)
19794 typval_T *argvars;
19795 typval_T *rettv;
19796{
19797 float_T f;
19798
19799 rettv->v_type = VAR_FLOAT;
19800 if (get_float_arg(argvars, &f) == OK)
19801 /* trunc() is not in C90, use floor() or ceil() instead. */
19802 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19803 else
19804 rettv->vval.v_float = 0.0;
19805}
19806#endif
19807
Bram Moolenaar8299df92004-07-10 09:47:34 +000019808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 * "type(expr)" function
19810 */
19811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 typval_T *argvars;
19814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019816 int n;
19817
19818 switch (argvars[0].v_type)
19819 {
19820 case VAR_NUMBER: n = 0; break;
19821 case VAR_STRING: n = 1; break;
19822 case VAR_FUNC: n = 2; break;
19823 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019824 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019825#ifdef FEAT_FLOAT
19826 case VAR_FLOAT: n = 5; break;
19827#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019828 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19829 }
19830 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831}
19832
19833/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019834 * "undofile(name)" function
19835 */
19836 static void
19837f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019838 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019839 typval_T *rettv;
19840{
19841 rettv->v_type = VAR_STRING;
19842#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019843 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019844 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019845
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019846 if (*fname == NUL)
19847 {
19848 /* If there is no file name there will be no undo file. */
19849 rettv->vval.v_string = NULL;
19850 }
19851 else
19852 {
19853 char_u *ffname = FullName_save(fname, FALSE);
19854
19855 if (ffname != NULL)
19856 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19857 vim_free(ffname);
19858 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019859 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019860#else
19861 rettv->vval.v_string = NULL;
19862#endif
19863}
19864
19865/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019866 * "undotree()" function
19867 */
19868 static void
19869f_undotree(argvars, rettv)
19870 typval_T *argvars UNUSED;
19871 typval_T *rettv;
19872{
19873 if (rettv_dict_alloc(rettv) == OK)
19874 {
19875 dict_T *dict = rettv->vval.v_dict;
19876 list_T *list;
19877
Bram Moolenaar730cde92010-06-27 05:18:54 +020019878 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019879 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019880 dict_add_nr_str(dict, "save_last",
19881 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019882 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19883 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019884 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019885
19886 list = list_alloc();
19887 if (list != NULL)
19888 {
19889 u_eval_tree(curbuf->b_u_oldhead, list);
19890 dict_add_list(dict, "entries", list);
19891 }
19892 }
19893}
19894
19895/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019896 * "values(dict)" function
19897 */
19898 static void
19899f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 typval_T *argvars;
19901 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019902{
19903 dict_list(argvars, rettv, 1);
19904}
19905
19906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 * "virtcol(string)" function
19908 */
19909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019910f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 typval_T *argvars;
19912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913{
19914 colnr_T vcol = 0;
19915 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019916 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019918 fp = var2fpos(&argvars[0], FALSE, &fnum);
19919 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19920 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 {
19922 getvvcol(curwin, fp, NULL, NULL, &vcol);
19923 ++vcol;
19924 }
19925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927}
19928
19929/*
19930 * "visualmode()" function
19931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019934 typval_T *argvars UNUSED;
19935 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 char_u str[2];
19938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 str[0] = curbuf->b_visual_mode_eval;
19941 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943
19944 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019945 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947}
19948
19949/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019950 * "wildmenumode()" function
19951 */
19952 static void
19953f_wildmenumode(argvars, rettv)
19954 typval_T *argvars UNUSED;
19955 typval_T *rettv UNUSED;
19956{
19957#ifdef FEAT_WILDMENU
19958 if (wild_menu_showing)
19959 rettv->vval.v_number = 1;
19960#endif
19961}
19962
19963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 * "winbufnr(nr)" function
19965 */
19966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019968 typval_T *argvars;
19969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
19971 win_T *wp;
19972
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019973 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978}
19979
19980/*
19981 * "wincol()" function
19982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019985 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987{
19988 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019989 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990}
19991
19992/*
19993 * "winheight(nr)" function
19994 */
19995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 typval_T *argvars;
19998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
20000 win_T *wp;
20001
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020002 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007}
20008
20009/*
20010 * "winline()" function
20011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016{
20017 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019}
20020
20021/*
20022 * "winnr()" function
20023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028{
20029 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020030
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020032 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035}
20036
20037/*
20038 * "winrestcmd()" function
20039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
20045#ifdef FEAT_WINDOWS
20046 win_T *wp;
20047 int winnr = 1;
20048 garray_T ga;
20049 char_u buf[50];
20050
20051 ga_init2(&ga, (int)sizeof(char), 70);
20052 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20053 {
20054 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20055 ga_concat(&ga, buf);
20056# ifdef FEAT_VERTSPLIT
20057 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20058 ga_concat(&ga, buf);
20059# endif
20060 ++winnr;
20061 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020062 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020072 * "winrestview()" function
20073 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020074 static void
20075f_winrestview(argvars, rettv)
20076 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020077 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020078{
20079 dict_T *dict;
20080
20081 if (argvars[0].v_type != VAR_DICT
20082 || (dict = argvars[0].vval.v_dict) == NULL)
20083 EMSG(_(e_invarg));
20084 else
20085 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020086 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20087 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20088 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20089 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020090#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020091 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20092 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020093#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020094 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20095 {
20096 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20097 curwin->w_set_curswant = FALSE;
20098 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020099
Bram Moolenaar82c25852014-05-28 16:47:16 +020020100 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20101 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020102#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020103 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20104 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020105#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020106 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20107 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20108 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20109 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020110
20111 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020112 win_new_height(curwin, curwin->w_height);
20113# ifdef FEAT_VERTSPLIT
20114 win_new_width(curwin, W_WIDTH(curwin));
20115# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020116 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020117
Bram Moolenaarb851a962014-10-31 15:45:52 +010020118 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020119 curwin->w_topline = 1;
20120 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20121 curwin->w_topline = curbuf->b_ml.ml_line_count;
20122#ifdef FEAT_DIFF
20123 check_topfill(curwin, TRUE);
20124#endif
20125 }
20126}
20127
20128/*
20129 * "winsaveview()" function
20130 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020131 static void
20132f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020133 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020134 typval_T *rettv;
20135{
20136 dict_T *dict;
20137
Bram Moolenaara800b422010-06-27 01:15:55 +020020138 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020139 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020140 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020141
20142 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20143 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20144#ifdef FEAT_VIRTUALEDIT
20145 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20146#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020147 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020148 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20149
20150 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20151#ifdef FEAT_DIFF
20152 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20153#endif
20154 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20155 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20156}
20157
20158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 * "winwidth(nr)" function
20160 */
20161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 typval_T *argvars;
20164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165{
20166 win_T *wp;
20167
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020168 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 else
20172#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020173 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176#endif
20177}
20178
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020180 * Write list of strings to file
20181 */
20182 static int
20183write_list(fd, list, binary)
20184 FILE *fd;
20185 list_T *list;
20186 int binary;
20187{
20188 listitem_T *li;
20189 int c;
20190 int ret = OK;
20191 char_u *s;
20192
20193 for (li = list->lv_first; li != NULL; li = li->li_next)
20194 {
20195 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20196 {
20197 if (*s == '\n')
20198 c = putc(NUL, fd);
20199 else
20200 c = putc(*s, fd);
20201 if (c == EOF)
20202 {
20203 ret = FAIL;
20204 break;
20205 }
20206 }
20207 if (!binary || li->li_next != NULL)
20208 if (putc('\n', fd) == EOF)
20209 {
20210 ret = FAIL;
20211 break;
20212 }
20213 if (ret == FAIL)
20214 {
20215 EMSG(_(e_write));
20216 break;
20217 }
20218 }
20219 return ret;
20220}
20221
20222/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020223 * "writefile()" function
20224 */
20225 static void
20226f_writefile(argvars, rettv)
20227 typval_T *argvars;
20228 typval_T *rettv;
20229{
20230 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020231 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020232 char_u *fname;
20233 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020234 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020235
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020236 if (check_restricted() || check_secure())
20237 return;
20238
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020239 if (argvars[0].v_type != VAR_LIST)
20240 {
20241 EMSG2(_(e_listarg), "writefile()");
20242 return;
20243 }
20244 if (argvars[0].vval.v_list == NULL)
20245 return;
20246
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020247 if (argvars[2].v_type != VAR_UNKNOWN)
20248 {
20249 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20250 binary = TRUE;
20251 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20252 append = TRUE;
20253 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020254
20255 /* Always open the file in binary mode, library functions have a mind of
20256 * their own about CR-LF conversion. */
20257 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020258 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20259 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020260 {
20261 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20262 ret = -1;
20263 }
20264 else
20265 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020266 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20267 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020268 fclose(fd);
20269 }
20270
20271 rettv->vval.v_number = ret;
20272}
20273
20274/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020275 * "xor(expr, expr)" function
20276 */
20277 static void
20278f_xor(argvars, rettv)
20279 typval_T *argvars;
20280 typval_T *rettv;
20281{
20282 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20283 ^ get_tv_number_chk(&argvars[1], NULL);
20284}
20285
20286
20287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020289 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 */
20291 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020292var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020294 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020295 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020297 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020299 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300
Bram Moolenaara5525202006-03-02 22:52:09 +000020301 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020302 if (varp->v_type == VAR_LIST)
20303 {
20304 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020305 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020306 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020307 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020308
20309 l = varp->vval.v_list;
20310 if (l == NULL)
20311 return NULL;
20312
20313 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020314 pos.lnum = list_find_nr(l, 0L, &error);
20315 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020316 return NULL; /* invalid line number */
20317
20318 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020319 pos.col = list_find_nr(l, 1L, &error);
20320 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020321 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020322 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020323
20324 /* We accept "$" for the column number: last column. */
20325 li = list_find(l, 1L);
20326 if (li != NULL && li->li_tv.v_type == VAR_STRING
20327 && li->li_tv.vval.v_string != NULL
20328 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20329 pos.col = len + 1;
20330
Bram Moolenaara5525202006-03-02 22:52:09 +000020331 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020332 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020333 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020334 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020335
Bram Moolenaara5525202006-03-02 22:52:09 +000020336#ifdef FEAT_VIRTUALEDIT
20337 /* Get the virtual offset. Defaults to zero. */
20338 pos.coladd = list_find_nr(l, 2L, &error);
20339 if (error)
20340 pos.coladd = 0;
20341#endif
20342
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020343 return &pos;
20344 }
20345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020346 name = get_tv_string_chk(varp);
20347 if (name == NULL)
20348 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020349 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020351 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20352 {
20353 if (VIsual_active)
20354 return &VIsual;
20355 return &curwin->w_cursor;
20356 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020357 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020359 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20361 return NULL;
20362 return pp;
20363 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020364
20365#ifdef FEAT_VIRTUALEDIT
20366 pos.coladd = 0;
20367#endif
20368
Bram Moolenaar477933c2007-07-17 14:32:23 +000020369 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020370 {
20371 pos.col = 0;
20372 if (name[1] == '0') /* "w0": first visible line */
20373 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020374 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020375 pos.lnum = curwin->w_topline;
20376 return &pos;
20377 }
20378 else if (name[1] == '$') /* "w$": last visible line */
20379 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020380 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020381 pos.lnum = curwin->w_botline - 1;
20382 return &pos;
20383 }
20384 }
20385 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020387 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 {
20389 pos.lnum = curbuf->b_ml.ml_line_count;
20390 pos.col = 0;
20391 }
20392 else
20393 {
20394 pos.lnum = curwin->w_cursor.lnum;
20395 pos.col = (colnr_T)STRLEN(ml_get_curline());
20396 }
20397 return &pos;
20398 }
20399 return NULL;
20400}
20401
20402/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020403 * Convert list in "arg" into a position and optional file number.
20404 * When "fnump" is NULL there is no file number, only 3 items.
20405 * Note that the column is passed on as-is, the caller may want to decrement
20406 * it to use 1 for the first column.
20407 * Return FAIL when conversion is not possible, doesn't check the position for
20408 * validity.
20409 */
20410 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020411list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020412 typval_T *arg;
20413 pos_T *posp;
20414 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020415 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020416{
20417 list_T *l = arg->vval.v_list;
20418 long i = 0;
20419 long n;
20420
Bram Moolenaar493c1782014-05-28 14:34:46 +020020421 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20422 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020423 if (arg->v_type != VAR_LIST
20424 || l == NULL
20425 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020426 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020427 return FAIL;
20428
20429 if (fnump != NULL)
20430 {
20431 n = list_find_nr(l, i++, NULL); /* fnum */
20432 if (n < 0)
20433 return FAIL;
20434 if (n == 0)
20435 n = curbuf->b_fnum; /* current buffer */
20436 *fnump = n;
20437 }
20438
20439 n = list_find_nr(l, i++, NULL); /* lnum */
20440 if (n < 0)
20441 return FAIL;
20442 posp->lnum = n;
20443
20444 n = list_find_nr(l, i++, NULL); /* col */
20445 if (n < 0)
20446 return FAIL;
20447 posp->col = n;
20448
20449#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020450 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020451 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020452 posp->coladd = 0;
20453 else
20454 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020455#endif
20456
Bram Moolenaar493c1782014-05-28 14:34:46 +020020457 if (curswantp != NULL)
20458 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20459
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020460 return OK;
20461}
20462
20463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 * Get the length of an environment variable name.
20465 * Advance "arg" to the first character after the name.
20466 * Return 0 for error.
20467 */
20468 static int
20469get_env_len(arg)
20470 char_u **arg;
20471{
20472 char_u *p;
20473 int len;
20474
20475 for (p = *arg; vim_isIDc(*p); ++p)
20476 ;
20477 if (p == *arg) /* no name found */
20478 return 0;
20479
20480 len = (int)(p - *arg);
20481 *arg = p;
20482 return len;
20483}
20484
20485/*
20486 * Get the length of the name of a function or internal variable.
20487 * "arg" is advanced to the first non-white character after the name.
20488 * Return 0 if something is wrong.
20489 */
20490 static int
20491get_id_len(arg)
20492 char_u **arg;
20493{
20494 char_u *p;
20495 int len;
20496
20497 /* Find the end of the name. */
20498 for (p = *arg; eval_isnamec(*p); ++p)
20499 ;
20500 if (p == *arg) /* no name found */
20501 return 0;
20502
20503 len = (int)(p - *arg);
20504 *arg = skipwhite(p);
20505
20506 return len;
20507}
20508
20509/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020510 * Get the length of the name of a variable or function.
20511 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020513 * Return -1 if curly braces expansion failed.
20514 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 * If the name contains 'magic' {}'s, expand them and return the
20516 * expanded name in an allocated string via 'alias' - caller must free.
20517 */
20518 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020519get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 char_u **arg;
20521 char_u **alias;
20522 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020523 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524{
20525 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 char_u *p;
20527 char_u *expr_start;
20528 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529
20530 *alias = NULL; /* default to no alias */
20531
20532 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20533 && (*arg)[2] == (int)KE_SNR)
20534 {
20535 /* hard coded <SNR>, already translated */
20536 *arg += 3;
20537 return get_id_len(arg) + 3;
20538 }
20539 len = eval_fname_script(*arg);
20540 if (len > 0)
20541 {
20542 /* literal "<SID>", "s:" or "<SNR>" */
20543 *arg += len;
20544 }
20545
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020549 p = find_name_end(*arg, &expr_start, &expr_end,
20550 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 if (expr_start != NULL)
20552 {
20553 char_u *temp_string;
20554
20555 if (!evaluate)
20556 {
20557 len += (int)(p - *arg);
20558 *arg = skipwhite(p);
20559 return len;
20560 }
20561
20562 /*
20563 * Include any <SID> etc in the expanded string:
20564 * Thus the -len here.
20565 */
20566 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20567 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020568 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 *alias = temp_string;
20570 *arg = skipwhite(p);
20571 return (int)STRLEN(temp_string);
20572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573
20574 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020575 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 EMSG2(_(e_invexpr2), *arg);
20577
20578 return len;
20579}
20580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020581/*
20582 * Find the end of a variable or function name, taking care of magic braces.
20583 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20584 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020585 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020586 * Return a pointer to just after the name. Equal to "arg" if there is no
20587 * valid name.
20588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020590find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 char_u *arg;
20592 char_u **expr_start;
20593 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020594 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020596 int mb_nest = 0;
20597 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 char_u *p;
20599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020602 *expr_start = NULL;
20603 *expr_end = NULL;
20604 }
20605
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020606 /* Quick check for valid starting character. */
20607 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20608 return arg;
20609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020610 for (p = arg; *p != NUL
20611 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020613 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020615 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020616 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020617 if (*p == '\'')
20618 {
20619 /* skip over 'string' to avoid counting [ and ] inside it. */
20620 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20621 ;
20622 if (*p == NUL)
20623 break;
20624 }
20625 else if (*p == '"')
20626 {
20627 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20628 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20629 if (*p == '\\' && p[1] != NUL)
20630 ++p;
20631 if (*p == NUL)
20632 break;
20633 }
20634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020635 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637 if (*p == '[')
20638 ++br_nest;
20639 else if (*p == ']')
20640 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020643 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020645 if (*p == '{')
20646 {
20647 mb_nest++;
20648 if (expr_start != NULL && *expr_start == NULL)
20649 *expr_start = p;
20650 }
20651 else if (*p == '}')
20652 {
20653 mb_nest--;
20654 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20655 *expr_end = p;
20656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 }
20659
20660 return p;
20661}
20662
20663/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020664 * Expands out the 'magic' {}'s in a variable/function name.
20665 * Note that this can call itself recursively, to deal with
20666 * constructs like foo{bar}{baz}{bam}
20667 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20668 * "in_start" ^
20669 * "expr_start" ^
20670 * "expr_end" ^
20671 * "in_end" ^
20672 *
20673 * Returns a new allocated string, which the caller must free.
20674 * Returns NULL for failure.
20675 */
20676 static char_u *
20677make_expanded_name(in_start, expr_start, expr_end, in_end)
20678 char_u *in_start;
20679 char_u *expr_start;
20680 char_u *expr_end;
20681 char_u *in_end;
20682{
20683 char_u c1;
20684 char_u *retval = NULL;
20685 char_u *temp_result;
20686 char_u *nextcmd = NULL;
20687
20688 if (expr_end == NULL || in_end == NULL)
20689 return NULL;
20690 *expr_start = NUL;
20691 *expr_end = NUL;
20692 c1 = *in_end;
20693 *in_end = NUL;
20694
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020695 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020696 if (temp_result != NULL && nextcmd == NULL)
20697 {
20698 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20699 + (in_end - expr_end) + 1));
20700 if (retval != NULL)
20701 {
20702 STRCPY(retval, in_start);
20703 STRCAT(retval, temp_result);
20704 STRCAT(retval, expr_end + 1);
20705 }
20706 }
20707 vim_free(temp_result);
20708
20709 *in_end = c1; /* put char back for error messages */
20710 *expr_start = '{';
20711 *expr_end = '}';
20712
20713 if (retval != NULL)
20714 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020715 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020716 if (expr_start != NULL)
20717 {
20718 /* Further expansion! */
20719 temp_result = make_expanded_name(retval, expr_start,
20720 expr_end, temp_result);
20721 vim_free(retval);
20722 retval = temp_result;
20723 }
20724 }
20725
20726 return retval;
20727}
20728
20729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020731 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 */
20733 static int
20734eval_isnamec(c)
20735 int c;
20736{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020737 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20738}
20739
20740/*
20741 * Return TRUE if character "c" can be used as the first character in a
20742 * variable or function name (excluding '{' and '}').
20743 */
20744 static int
20745eval_isnamec1(c)
20746 int c;
20747{
20748 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749}
20750
20751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 * Set number v: variable to "val".
20753 */
20754 void
20755set_vim_var_nr(idx, val)
20756 int idx;
20757 long val;
20758{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020759 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760}
20761
20762/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020763 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 */
20765 long
20766get_vim_var_nr(idx)
20767 int idx;
20768{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020769 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770}
20771
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020772/*
20773 * Get string v: variable value. Uses a static buffer, can only be used once.
20774 */
20775 char_u *
20776get_vim_var_str(idx)
20777 int idx;
20778{
20779 return get_tv_string(&vimvars[idx].vv_tv);
20780}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020781
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020783 * Get List v: variable value. Caller must take care of reference count when
20784 * needed.
20785 */
20786 list_T *
20787get_vim_var_list(idx)
20788 int idx;
20789{
20790 return vimvars[idx].vv_list;
20791}
20792
20793/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020794 * Set v:char to character "c".
20795 */
20796 void
20797set_vim_var_char(c)
20798 int c;
20799{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020800 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020801
20802#ifdef FEAT_MBYTE
20803 if (has_mbyte)
20804 buf[(*mb_char2bytes)(c, buf)] = NUL;
20805 else
20806#endif
20807 {
20808 buf[0] = c;
20809 buf[1] = NUL;
20810 }
20811 set_vim_var_string(VV_CHAR, buf, -1);
20812}
20813
20814/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020815 * Set v:count to "count" and v:count1 to "count1".
20816 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 */
20818 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020819set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 long count;
20821 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020822 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020824 if (set_prevcount)
20825 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020826 vimvars[VV_COUNT].vv_nr = count;
20827 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828}
20829
20830/*
20831 * Set string v: variable to a copy of "val".
20832 */
20833 void
20834set_vim_var_string(idx, val, len)
20835 int idx;
20836 char_u *val;
20837 int len; /* length of "val" to use or -1 (whole string) */
20838{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 /* Need to do this (at least) once, since we can't initialize a union.
20840 * Will always be invoked when "v:progname" is set. */
20841 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20842
Bram Moolenaare9a41262005-01-15 22:18:47 +000020843 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020845 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020847 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020849 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850}
20851
20852/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020853 * Set List v: variable to "val".
20854 */
20855 void
20856set_vim_var_list(idx, val)
20857 int idx;
20858 list_T *val;
20859{
20860 list_unref(vimvars[idx].vv_list);
20861 vimvars[idx].vv_list = val;
20862 if (val != NULL)
20863 ++val->lv_refcount;
20864}
20865
20866/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020867 * Set Dictionary v: variable to "val".
20868 */
20869 void
20870set_vim_var_dict(idx, val)
20871 int idx;
20872 dict_T *val;
20873{
20874 int todo;
20875 hashitem_T *hi;
20876
20877 dict_unref(vimvars[idx].vv_dict);
20878 vimvars[idx].vv_dict = val;
20879 if (val != NULL)
20880 {
20881 ++val->dv_refcount;
20882
20883 /* Set readonly */
20884 todo = (int)val->dv_hashtab.ht_used;
20885 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20886 {
20887 if (HASHITEM_EMPTY(hi))
20888 continue;
20889 --todo;
20890 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20891 }
20892 }
20893}
20894
20895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 * Set v:register if needed.
20897 */
20898 void
20899set_reg_var(c)
20900 int c;
20901{
20902 char_u regname;
20903
20904 if (c == 0 || c == ' ')
20905 regname = '"';
20906 else
20907 regname = c;
20908 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020909 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 set_vim_var_string(VV_REG, &regname, 1);
20911}
20912
20913/*
20914 * Get or set v:exception. If "oldval" == NULL, return the current value.
20915 * Otherwise, restore the value to "oldval" and return NULL.
20916 * Must always be called in pairs to save and restore v:exception! Does not
20917 * take care of memory allocations.
20918 */
20919 char_u *
20920v_exception(oldval)
20921 char_u *oldval;
20922{
20923 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020924 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925
Bram Moolenaare9a41262005-01-15 22:18:47 +000020926 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 return NULL;
20928}
20929
20930/*
20931 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20932 * Otherwise, restore the value to "oldval" and return NULL.
20933 * Must always be called in pairs to save and restore v:throwpoint! Does not
20934 * take care of memory allocations.
20935 */
20936 char_u *
20937v_throwpoint(oldval)
20938 char_u *oldval;
20939{
20940 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020941 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942
Bram Moolenaare9a41262005-01-15 22:18:47 +000020943 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 return NULL;
20945}
20946
20947#if defined(FEAT_AUTOCMD) || defined(PROTO)
20948/*
20949 * Set v:cmdarg.
20950 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20951 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20952 * Must always be called in pairs!
20953 */
20954 char_u *
20955set_cmdarg(eap, oldarg)
20956 exarg_T *eap;
20957 char_u *oldarg;
20958{
20959 char_u *oldval;
20960 char_u *newval;
20961 unsigned len;
20962
Bram Moolenaare9a41262005-01-15 22:18:47 +000020963 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020964 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020966 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020968 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 }
20970
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020971 if (eap->force_bin == FORCE_BIN)
20972 len = 6;
20973 else if (eap->force_bin == FORCE_NOBIN)
20974 len = 8;
20975 else
20976 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020977
20978 if (eap->read_edit)
20979 len += 7;
20980
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020981 if (eap->force_ff != 0)
20982 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20983# ifdef FEAT_MBYTE
20984 if (eap->force_enc != 0)
20985 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020986 if (eap->bad_char != 0)
20987 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020988# endif
20989
20990 newval = alloc(len + 1);
20991 if (newval == NULL)
20992 return NULL;
20993
20994 if (eap->force_bin == FORCE_BIN)
20995 sprintf((char *)newval, " ++bin");
20996 else if (eap->force_bin == FORCE_NOBIN)
20997 sprintf((char *)newval, " ++nobin");
20998 else
20999 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021000
21001 if (eap->read_edit)
21002 STRCAT(newval, " ++edit");
21003
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021004 if (eap->force_ff != 0)
21005 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21006 eap->cmd + eap->force_ff);
21007# ifdef FEAT_MBYTE
21008 if (eap->force_enc != 0)
21009 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21010 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021011 if (eap->bad_char == BAD_KEEP)
21012 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21013 else if (eap->bad_char == BAD_DROP)
21014 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21015 else if (eap->bad_char != 0)
21016 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021017# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021019 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020}
21021#endif
21022
21023/*
21024 * Get the value of internal variable "name".
21025 * Return OK or FAIL.
21026 */
21027 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021028get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 char_u *name;
21030 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021031 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021032 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021033 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021034 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035{
21036 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 typval_T *tv = NULL;
21038 typval_T atv;
21039 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041
21042 /* truncate the name, so that we can use strcmp() */
21043 cc = name[len];
21044 name[len] = NUL;
21045
21046 /*
21047 * Check for "b:changedtick".
21048 */
21049 if (STRCMP(name, "b:changedtick") == 0)
21050 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021051 atv.v_type = VAR_NUMBER;
21052 atv.vval.v_number = curbuf->b_changedtick;
21053 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 }
21055
21056 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 * Check for user-defined variables.
21058 */
21059 else
21060 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021061 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021064 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021065 if (dip != NULL)
21066 *dip = v;
21067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 }
21069
Bram Moolenaare9a41262005-01-15 22:18:47 +000021070 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021072 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021073 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 ret = FAIL;
21075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021077 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078
21079 name[len] = cc;
21080
21081 return ret;
21082}
21083
21084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021085 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21086 * Also handle function call with Funcref variable: func(expr)
21087 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21088 */
21089 static int
21090handle_subscript(arg, rettv, evaluate, verbose)
21091 char_u **arg;
21092 typval_T *rettv;
21093 int evaluate; /* do more than finding the end */
21094 int verbose; /* give error messages */
21095{
21096 int ret = OK;
21097 dict_T *selfdict = NULL;
21098 char_u *s;
21099 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021100 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021101
21102 while (ret == OK
21103 && (**arg == '['
21104 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021105 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021106 && !vim_iswhite(*(*arg - 1)))
21107 {
21108 if (**arg == '(')
21109 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021110 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021111 if (evaluate)
21112 {
21113 functv = *rettv;
21114 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021116 /* Invoke the function. Recursive! */
21117 s = functv.vval.v_string;
21118 }
21119 else
21120 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021121 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021122 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21123 &len, evaluate, selfdict);
21124
21125 /* Clear the funcref afterwards, so that deleting it while
21126 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021127 if (evaluate)
21128 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021129
21130 /* Stop the expression evaluation when immediately aborting on
21131 * error, or when an interrupt occurred or an exception was thrown
21132 * but not caught. */
21133 if (aborting())
21134 {
21135 if (ret == OK)
21136 clear_tv(rettv);
21137 ret = FAIL;
21138 }
21139 dict_unref(selfdict);
21140 selfdict = NULL;
21141 }
21142 else /* **arg == '[' || **arg == '.' */
21143 {
21144 dict_unref(selfdict);
21145 if (rettv->v_type == VAR_DICT)
21146 {
21147 selfdict = rettv->vval.v_dict;
21148 if (selfdict != NULL)
21149 ++selfdict->dv_refcount;
21150 }
21151 else
21152 selfdict = NULL;
21153 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21154 {
21155 clear_tv(rettv);
21156 ret = FAIL;
21157 }
21158 }
21159 }
21160 dict_unref(selfdict);
21161 return ret;
21162}
21163
21164/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021165 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021166 * value).
21167 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021170{
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021172}
21173
21174/*
21175 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 * The string "s" must have been allocated, it is consumed.
21177 * Return NULL for out of memory, the variable otherwise.
21178 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021179 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021180alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 char_u *s;
21182{
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 rettv = alloc_tv();
21186 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021188 rettv->v_type = VAR_STRING;
21189 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 }
21191 else
21192 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194}
21195
21196/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021197 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021199 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021200free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202{
21203 if (varp != NULL)
21204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021205 switch (varp->v_type)
21206 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021207 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021208 func_unref(varp->vval.v_string);
21209 /*FALLTHROUGH*/
21210 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021211 vim_free(varp->vval.v_string);
21212 break;
21213 case VAR_LIST:
21214 list_unref(varp->vval.v_list);
21215 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021216 case VAR_DICT:
21217 dict_unref(varp->vval.v_dict);
21218 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021219 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021220#ifdef FEAT_FLOAT
21221 case VAR_FLOAT:
21222#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021223 case VAR_UNKNOWN:
21224 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021225 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021226 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021227 break;
21228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 vim_free(varp);
21230 }
21231}
21232
21233/*
21234 * Free the memory for a variable value and set the value to NULL or 0.
21235 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021236 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021237clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021238 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239{
21240 if (varp != NULL)
21241 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021244 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021245 func_unref(varp->vval.v_string);
21246 /*FALLTHROUGH*/
21247 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021248 vim_free(varp->vval.v_string);
21249 varp->vval.v_string = NULL;
21250 break;
21251 case VAR_LIST:
21252 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021253 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021254 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021255 case VAR_DICT:
21256 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021257 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021258 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021260 varp->vval.v_number = 0;
21261 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021262#ifdef FEAT_FLOAT
21263 case VAR_FLOAT:
21264 varp->vval.v_float = 0.0;
21265 break;
21266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021267 case VAR_UNKNOWN:
21268 break;
21269 default:
21270 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021272 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 }
21274}
21275
21276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021277 * Set the value of a variable to NULL without freeing items.
21278 */
21279 static void
21280init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021282{
21283 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285}
21286
21287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 * Get the number value of a variable.
21289 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021290 * For incompatible types, return 0.
21291 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21292 * caller of incompatible types: it sets *denote to TRUE if "denote"
21293 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 */
21295 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021296get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021297 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021299 int error = FALSE;
21300
21301 return get_tv_number_chk(varp, &error); /* return 0L on error */
21302}
21303
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021304 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021305get_tv_number_chk(varp, denote)
21306 typval_T *varp;
21307 int *denote;
21308{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021311 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021313 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021314 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021315#ifdef FEAT_FLOAT
21316 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021317 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021318 break;
21319#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021320 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021321 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021322 break;
21323 case VAR_STRING:
21324 if (varp->vval.v_string != NULL)
21325 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021326 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021327 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021328 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021329 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021330 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021331 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021332 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021334 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021335 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021336 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021338 if (denote == NULL) /* useful for values that must be unsigned */
21339 n = -1;
21340 else
21341 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021342 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343}
21344
21345/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021346 * Get the lnum from the first argument.
21347 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021348 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 */
21350 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353{
Bram Moolenaar33570922005-01-25 22:26:29 +000021354 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 linenr_T lnum;
21356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021357 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (lnum == 0) /* no valid number, try using line() */
21359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 rettv.v_type = VAR_NUMBER;
21361 f_line(argvars, &rettv);
21362 lnum = rettv.vval.v_number;
21363 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 }
21365 return lnum;
21366}
21367
21368/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021369 * Get the lnum from the first argument.
21370 * Also accepts "$", then "buf" is used.
21371 * Returns 0 on error.
21372 */
21373 static linenr_T
21374get_tv_lnum_buf(argvars, buf)
21375 typval_T *argvars;
21376 buf_T *buf;
21377{
21378 if (argvars[0].v_type == VAR_STRING
21379 && argvars[0].vval.v_string != NULL
21380 && argvars[0].vval.v_string[0] == '$'
21381 && buf != NULL)
21382 return buf->b_ml.ml_line_count;
21383 return get_tv_number_chk(&argvars[0], NULL);
21384}
21385
21386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 * Get the string value of a variable.
21388 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021389 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21390 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 * If the String variable has never been set, return an empty string.
21392 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021393 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21394 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 */
21396 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021397get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021398 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399{
21400 static char_u mybuf[NUMBUFLEN];
21401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403}
21404
21405 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021408 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021410 char_u *res = get_tv_string_buf_chk(varp, buf);
21411
21412 return res != NULL ? res : (char_u *)"";
21413}
21414
Bram Moolenaar7d647822014-04-05 21:28:56 +020021415/*
21416 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21417 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021418 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021419get_tv_string_chk(varp)
21420 typval_T *varp;
21421{
21422 static char_u mybuf[NUMBUFLEN];
21423
21424 return get_tv_string_buf_chk(varp, mybuf);
21425}
21426
21427 static char_u *
21428get_tv_string_buf_chk(varp, buf)
21429 typval_T *varp;
21430 char_u *buf;
21431{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021432 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 case VAR_NUMBER:
21435 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21436 return buf;
21437 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021438 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021439 break;
21440 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021441 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021442 break;
21443 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021444 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021446#ifdef FEAT_FLOAT
21447 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021448 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021449 break;
21450#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021451 case VAR_STRING:
21452 if (varp->vval.v_string != NULL)
21453 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021454 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021459 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460}
21461
21462/*
21463 * Find variable "name" in the list of variables.
21464 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021465 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021466 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021470find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021473 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477
Bram Moolenaara7043832005-01-21 11:56:39 +000021478 ht = find_var_ht(name, &varname);
21479 if (htp != NULL)
21480 *htp = ht;
21481 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021483 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484}
21485
21486/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021487 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021488 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021491find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021493 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021494 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021495 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021496{
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 hashitem_T *hi;
21498
21499 if (*varname == NUL)
21500 {
21501 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021502 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021503 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021504 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 case 'g': return &globvars_var;
21506 case 'v': return &vimvars_var;
21507 case 'b': return &curbuf->b_bufvar;
21508 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021509#ifdef FEAT_WINDOWS
21510 case 't': return &curtab->tp_winvar;
21511#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021512 case 'l': return current_funccal == NULL
21513 ? NULL : &current_funccal->l_vars_var;
21514 case 'a': return current_funccal == NULL
21515 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021516 }
21517 return NULL;
21518 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021519
21520 hi = hash_find(ht, varname);
21521 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021522 {
21523 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021524 * worked find the variable again. Don't auto-load a script if it was
21525 * loaded already, otherwise it would be loaded every time when
21526 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021527 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021528 {
21529 /* Note: script_autoload() may make "hi" invalid. It must either
21530 * be obtained again or not used. */
21531 if (!script_autoload(varname, FALSE) || aborting())
21532 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021533 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021535 if (HASHITEM_EMPTY(hi))
21536 return NULL;
21537 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021538 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021539}
21540
21541/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021543 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021544 * Set "varname" to the start of name without ':'.
21545 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021547find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 char_u *name;
21549 char_u **varname;
21550{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021551 hashitem_T *hi;
21552
Bram Moolenaar73627d02015-08-11 15:46:09 +020021553 if (name[0] == NUL)
21554 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 if (name[1] != ':')
21556 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021557 /* The name must not start with a colon or #. */
21558 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 return NULL;
21560 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021561
21562 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021563 hi = hash_find(&compat_hashtab, name);
21564 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021565 return &compat_hashtab;
21566
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 return &globvarht; /* global variable */
21569 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 }
21571 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021572 if (*name == 'g') /* global variable */
21573 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021574 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21575 */
21576 if (vim_strchr(name + 2, ':') != NULL
21577 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021578 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021580 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021582 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021583#ifdef FEAT_WINDOWS
21584 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021585 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021586#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 if (*name == 'v') /* v: variable */
21588 return &vimvarht;
21589 if (*name == 'a' && current_funccal != NULL) /* function argument */
21590 return &current_funccal->l_avars.dv_hashtab;
21591 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21592 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 if (*name == 's' /* script variable */
21594 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21595 return &SCRIPT_VARS(current_SID);
21596 return NULL;
21597}
21598
21599/*
21600 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021601 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 * Returns NULL when it doesn't exist.
21603 */
21604 char_u *
21605get_var_value(name)
21606 char_u *name;
21607{
Bram Moolenaar33570922005-01-25 22:26:29 +000021608 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021610 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 if (v == NULL)
21612 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614}
21615
21616/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 * sourcing this script and when executing functions defined in the script.
21619 */
21620 void
21621new_script_vars(id)
21622 scid_T id;
21623{
Bram Moolenaara7043832005-01-21 11:56:39 +000021624 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021625 hashtab_T *ht;
21626 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021627
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21629 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021630 /* Re-allocating ga_data means that an ht_array pointing to
21631 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021633 for (i = 1; i <= ga_scripts.ga_len; ++i)
21634 {
21635 ht = &SCRIPT_VARS(i);
21636 if (ht->ht_mask == HT_INIT_SIZE - 1)
21637 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021638 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021640 }
21641
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 while (ga_scripts.ga_len < id)
21643 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021644 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021645 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021646 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 }
21649 }
21650}
21651
21652/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21654 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 */
21656 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021657init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 dict_T *dict;
21659 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021660 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661{
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021663 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021664 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021665 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021666 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 dict_var->di_tv.vval.v_dict = dict;
21668 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021669 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21671 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672}
21673
21674/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021675 * Unreference a dictionary initialized by init_var_dict().
21676 */
21677 void
21678unref_var_dict(dict)
21679 dict_T *dict;
21680{
21681 /* Now the dict needs to be freed if no one else is using it, go back to
21682 * normal reference counting. */
21683 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21684 dict_unref(dict);
21685}
21686
21687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 * Frees all allocated variables and the value they contain.
21690 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 */
21692 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021693vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021694 hashtab_T *ht;
21695{
21696 vars_clear_ext(ht, TRUE);
21697}
21698
21699/*
21700 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21701 */
21702 static void
21703vars_clear_ext(ht, free_val)
21704 hashtab_T *ht;
21705 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706{
Bram Moolenaara7043832005-01-21 11:56:39 +000021707 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 hashitem_T *hi;
21709 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021712 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021713 for (hi = ht->ht_array; todo > 0; ++hi)
21714 {
21715 if (!HASHITEM_EMPTY(hi))
21716 {
21717 --todo;
21718
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021720 * ht_array might change then. hash_clear() takes care of it
21721 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 v = HI2DI(hi);
21723 if (free_val)
21724 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021725 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021726 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021727 }
21728 }
21729 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021730 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731}
21732
Bram Moolenaara7043832005-01-21 11:56:39 +000021733/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021734 * Delete a variable from hashtab "ht" at item "hi".
21735 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021738delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021739 hashtab_T *ht;
21740 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741{
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021743
21744 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021745 clear_tv(&di->di_tv);
21746 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747}
21748
21749/*
21750 * List the value of one internal variable.
21751 */
21752 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021753list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021756 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021758 char_u *tofree;
21759 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021760 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021761
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021762 current_copyID += COPYID_INC;
21763 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021765 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021766 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767}
21768
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021770list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 char_u *prefix;
21772 char_u *name;
21773 int type;
21774 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021775 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776{
Bram Moolenaar31859182007-08-14 20:41:13 +000021777 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21778 msg_start();
21779 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 if (name != NULL) /* "a:" vars don't have a name stored */
21781 msg_puts(name);
21782 msg_putchar(' ');
21783 msg_advance(22);
21784 if (type == VAR_NUMBER)
21785 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 else if (type == VAR_FUNC)
21787 msg_putchar('*');
21788 else if (type == VAR_LIST)
21789 {
21790 msg_putchar('[');
21791 if (*string == '[')
21792 ++string;
21793 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021794 else if (type == VAR_DICT)
21795 {
21796 msg_putchar('{');
21797 if (*string == '{')
21798 ++string;
21799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 else
21801 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021802
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021804
21805 if (type == VAR_FUNC)
21806 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021807 if (*first)
21808 {
21809 msg_clr_eos();
21810 *first = FALSE;
21811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812}
21813
21814/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021815 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 * If the variable already exists, the value is updated.
21817 * Otherwise the variable is created.
21818 */
21819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021820set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021822 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824{
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021829 ht = find_var_ht(name, &varname);
21830 if (ht == NULL || *varname == NUL)
21831 {
21832 EMSG2(_(e_illvar), name);
21833 return;
21834 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021835 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021836
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021837 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21838 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021839
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021843 if (var_check_ro(v->di_flags, name, FALSE)
21844 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 return;
21846 if (v->di_tv.v_type != tv->v_type
21847 && !((v->di_tv.v_type == VAR_STRING
21848 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021849 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021850 || tv->v_type == VAR_NUMBER))
21851#ifdef FEAT_FLOAT
21852 && !((v->di_tv.v_type == VAR_NUMBER
21853 || v->di_tv.v_type == VAR_FLOAT)
21854 && (tv->v_type == VAR_NUMBER
21855 || tv->v_type == VAR_FLOAT))
21856#endif
21857 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021858 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021859 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021860 return;
21861 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021862
21863 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021864 * Handle setting internal v: variables separately where needed to
21865 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 */
21867 if (ht == &vimvarht)
21868 {
21869 if (v->di_tv.v_type == VAR_STRING)
21870 {
21871 vim_free(v->di_tv.vval.v_string);
21872 if (copy || tv->v_type != VAR_STRING)
21873 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21874 else
21875 {
21876 /* Take over the string to avoid an extra alloc/free. */
21877 v->di_tv.vval.v_string = tv->vval.v_string;
21878 tv->vval.v_string = NULL;
21879 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021880 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021881 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021882 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021883 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021885 if (STRCMP(varname, "searchforward") == 0)
21886 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021887#ifdef FEAT_SEARCH_EXTRA
21888 else if (STRCMP(varname, "hlsearch") == 0)
21889 {
21890 no_hlsearch = !v->di_tv.vval.v_number;
21891 redraw_all_later(SOME_VALID);
21892 }
21893#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021894 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021895 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021896 else if (v->di_tv.v_type != tv->v_type)
21897 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021898 }
21899
21900 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 }
21902 else /* add a new variable */
21903 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021904 /* Can't add "v:" variable. */
21905 if (ht == &vimvarht)
21906 {
21907 EMSG2(_(e_illvar), name);
21908 return;
21909 }
21910
Bram Moolenaar92124a32005-06-17 22:03:40 +000021911 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021912 if (!valid_varname(varname))
21913 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021914
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021915 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21916 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021917 if (v == NULL)
21918 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021920 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021922 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 return;
21924 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021925 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021927
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021928 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021930 else
21931 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021933 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936}
21937
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021939 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 * Also give an error message.
21941 */
21942 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021943var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021944 int flags;
21945 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021946 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021947{
21948 if (flags & DI_FLAGS_RO)
21949 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021950 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 return TRUE;
21952 }
21953 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21954 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021955 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021956 return TRUE;
21957 }
21958 return FALSE;
21959}
21960
21961/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021962 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21963 * Also give an error message.
21964 */
21965 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021966var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021967 int flags;
21968 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021969 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021970{
21971 if (flags & DI_FLAGS_FIX)
21972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021973 EMSG2(_("E795: Cannot delete variable %s"),
21974 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021975 return TRUE;
21976 }
21977 return FALSE;
21978}
21979
21980/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021981 * Check if a funcref is assigned to a valid variable name.
21982 * Return TRUE and give an error if not.
21983 */
21984 static int
21985var_check_func_name(name, new_var)
21986 char_u *name; /* points to start of variable name */
21987 int new_var; /* TRUE when creating the variable */
21988{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021989 /* Allow for w: b: s: and t:. */
21990 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021991 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21992 ? name[2] : name[0]))
21993 {
21994 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21995 name);
21996 return TRUE;
21997 }
21998 /* Don't allow hiding a function. When "v" is not NULL we might be
21999 * assigning another function to the same var, the type is checked
22000 * below. */
22001 if (new_var && function_exists(name))
22002 {
22003 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22004 name);
22005 return TRUE;
22006 }
22007 return FALSE;
22008}
22009
22010/*
22011 * Check if a variable name is valid.
22012 * Return FALSE and give an error if not.
22013 */
22014 static int
22015valid_varname(varname)
22016 char_u *varname;
22017{
22018 char_u *p;
22019
22020 for (p = varname; *p != NUL; ++p)
22021 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22022 && *p != AUTOLOAD_CHAR)
22023 {
22024 EMSG2(_(e_illvar), varname);
22025 return FALSE;
22026 }
22027 return TRUE;
22028}
22029
22030/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022031 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022032 * Also give an error message, using "name" or _("name") when use_gettext is
22033 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022034 */
22035 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022036tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022037 int lock;
22038 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022039 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022040{
22041 if (lock & VAR_LOCKED)
22042 {
22043 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022044 name == NULL ? (char_u *)_("Unknown")
22045 : use_gettext ? (char_u *)_(name)
22046 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022047 return TRUE;
22048 }
22049 if (lock & VAR_FIXED)
22050 {
22051 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022052 name == NULL ? (char_u *)_("Unknown")
22053 : use_gettext ? (char_u *)_(name)
22054 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022055 return TRUE;
22056 }
22057 return FALSE;
22058}
22059
22060/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022062 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022063 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022064 * It is OK for "from" and "to" to point to the same item. This is used to
22065 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022066 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022067 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022068copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022069 typval_T *from;
22070 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022073 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 switch (from->v_type)
22075 {
22076 case VAR_NUMBER:
22077 to->vval.v_number = from->vval.v_number;
22078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022079#ifdef FEAT_FLOAT
22080 case VAR_FLOAT:
22081 to->vval.v_float = from->vval.v_float;
22082 break;
22083#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 case VAR_STRING:
22085 case VAR_FUNC:
22086 if (from->vval.v_string == NULL)
22087 to->vval.v_string = NULL;
22088 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022090 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022091 if (from->v_type == VAR_FUNC)
22092 func_ref(to->vval.v_string);
22093 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022094 break;
22095 case VAR_LIST:
22096 if (from->vval.v_list == NULL)
22097 to->vval.v_list = NULL;
22098 else
22099 {
22100 to->vval.v_list = from->vval.v_list;
22101 ++to->vval.v_list->lv_refcount;
22102 }
22103 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022104 case VAR_DICT:
22105 if (from->vval.v_dict == NULL)
22106 to->vval.v_dict = NULL;
22107 else
22108 {
22109 to->vval.v_dict = from->vval.v_dict;
22110 ++to->vval.v_dict->dv_refcount;
22111 }
22112 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022113 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022114 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 break;
22116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117}
22118
22119/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022120 * Make a copy of an item.
22121 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022122 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22123 * reference to an already copied list/dict can be used.
22124 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022125 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022126 static int
22127item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022128 typval_T *from;
22129 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022130 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022131 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022132{
22133 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022134 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022135
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022137 {
22138 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022139 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022140 }
22141 ++recurse;
22142
22143 switch (from->v_type)
22144 {
22145 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146#ifdef FEAT_FLOAT
22147 case VAR_FLOAT:
22148#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022149 case VAR_STRING:
22150 case VAR_FUNC:
22151 copy_tv(from, to);
22152 break;
22153 case VAR_LIST:
22154 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022155 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022156 if (from->vval.v_list == NULL)
22157 to->vval.v_list = NULL;
22158 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22159 {
22160 /* use the copy made earlier */
22161 to->vval.v_list = from->vval.v_list->lv_copylist;
22162 ++to->vval.v_list->lv_refcount;
22163 }
22164 else
22165 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22166 if (to->vval.v_list == NULL)
22167 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022168 break;
22169 case VAR_DICT:
22170 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022171 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022172 if (from->vval.v_dict == NULL)
22173 to->vval.v_dict = NULL;
22174 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22175 {
22176 /* use the copy made earlier */
22177 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22178 ++to->vval.v_dict->dv_refcount;
22179 }
22180 else
22181 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22182 if (to->vval.v_dict == NULL)
22183 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022184 break;
22185 default:
22186 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022187 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 }
22189 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022190 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022191}
22192
22193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 * ":echo expr1 ..." print each argument separated with a space, add a
22195 * newline at the end.
22196 * ":echon expr1 ..." print each argument plain.
22197 */
22198 void
22199ex_echo(eap)
22200 exarg_T *eap;
22201{
22202 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022204 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 char_u *p;
22206 int needclr = TRUE;
22207 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022208 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209
22210 if (eap->skip)
22211 ++emsg_skip;
22212 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22213 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022214 /* If eval1() causes an error message the text from the command may
22215 * still need to be cleared. E.g., "echo 22,44". */
22216 need_clr_eos = needclr;
22217
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 {
22221 /*
22222 * Report the invalid expression unless the expression evaluation
22223 * has been cancelled due to an aborting error, an interrupt, or an
22224 * exception.
22225 */
22226 if (!aborting())
22227 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022228 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 break;
22230 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022231 need_clr_eos = FALSE;
22232
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 if (!eap->skip)
22234 {
22235 if (atstart)
22236 {
22237 atstart = FALSE;
22238 /* Call msg_start() after eval1(), evaluating the expression
22239 * may cause a message to appear. */
22240 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022241 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022242 /* Mark the saved text as finishing the line, so that what
22243 * follows is displayed on a new line when scrolling back
22244 * at the more prompt. */
22245 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249 else if (eap->cmdidx == CMD_echo)
22250 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022251 current_copyID += COPYID_INC;
22252 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022253 if (p != NULL)
22254 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022256 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022258 if (*p != TAB && needclr)
22259 {
22260 /* remove any text still there from the command */
22261 msg_clr_eos();
22262 needclr = FALSE;
22263 }
22264 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 }
22266 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022267 {
22268#ifdef FEAT_MBYTE
22269 if (has_mbyte)
22270 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022271 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022272
22273 (void)msg_outtrans_len_attr(p, i, echo_attr);
22274 p += i - 1;
22275 }
22276 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022278 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022281 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022283 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 arg = skipwhite(arg);
22285 }
22286 eap->nextcmd = check_nextcmd(arg);
22287
22288 if (eap->skip)
22289 --emsg_skip;
22290 else
22291 {
22292 /* remove text that may still be there from the command */
22293 if (needclr)
22294 msg_clr_eos();
22295 if (eap->cmdidx == CMD_echo)
22296 msg_end();
22297 }
22298}
22299
22300/*
22301 * ":echohl {name}".
22302 */
22303 void
22304ex_echohl(eap)
22305 exarg_T *eap;
22306{
22307 int id;
22308
22309 id = syn_name2id(eap->arg);
22310 if (id == 0)
22311 echo_attr = 0;
22312 else
22313 echo_attr = syn_id2attr(id);
22314}
22315
22316/*
22317 * ":execute expr1 ..." execute the result of an expression.
22318 * ":echomsg expr1 ..." Print a message
22319 * ":echoerr expr1 ..." Print an error
22320 * Each gets spaces around each argument and a newline at the end for
22321 * echo commands
22322 */
22323 void
22324ex_execute(eap)
22325 exarg_T *eap;
22326{
22327 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022328 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 int ret = OK;
22330 char_u *p;
22331 garray_T ga;
22332 int len;
22333 int save_did_emsg;
22334
22335 ga_init2(&ga, 1, 80);
22336
22337 if (eap->skip)
22338 ++emsg_skip;
22339 while (*arg != NUL && *arg != '|' && *arg != '\n')
22340 {
22341 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022342 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 {
22344 /*
22345 * Report the invalid expression unless the expression evaluation
22346 * has been cancelled due to an aborting error, an interrupt, or an
22347 * exception.
22348 */
22349 if (!aborting())
22350 EMSG2(_(e_invexpr2), p);
22351 ret = FAIL;
22352 break;
22353 }
22354
22355 if (!eap->skip)
22356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022357 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 len = (int)STRLEN(p);
22359 if (ga_grow(&ga, len + 2) == FAIL)
22360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022361 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 ret = FAIL;
22363 break;
22364 }
22365 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 ga.ga_len += len;
22369 }
22370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022371 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 arg = skipwhite(arg);
22373 }
22374
22375 if (ret != FAIL && ga.ga_data != NULL)
22376 {
22377 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022380 out_flush();
22381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 else if (eap->cmdidx == CMD_echoerr)
22383 {
22384 /* We don't want to abort following commands, restore did_emsg. */
22385 save_did_emsg = did_emsg;
22386 EMSG((char_u *)ga.ga_data);
22387 if (!force_abort)
22388 did_emsg = save_did_emsg;
22389 }
22390 else if (eap->cmdidx == CMD_execute)
22391 do_cmdline((char_u *)ga.ga_data,
22392 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22393 }
22394
22395 ga_clear(&ga);
22396
22397 if (eap->skip)
22398 --emsg_skip;
22399
22400 eap->nextcmd = check_nextcmd(arg);
22401}
22402
22403/*
22404 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22405 * "arg" points to the "&" or '+' when called, to "option" when returning.
22406 * Returns NULL when no option name found. Otherwise pointer to the char
22407 * after the option name.
22408 */
22409 static char_u *
22410find_option_end(arg, opt_flags)
22411 char_u **arg;
22412 int *opt_flags;
22413{
22414 char_u *p = *arg;
22415
22416 ++p;
22417 if (*p == 'g' && p[1] == ':')
22418 {
22419 *opt_flags = OPT_GLOBAL;
22420 p += 2;
22421 }
22422 else if (*p == 'l' && p[1] == ':')
22423 {
22424 *opt_flags = OPT_LOCAL;
22425 p += 2;
22426 }
22427 else
22428 *opt_flags = 0;
22429
22430 if (!ASCII_ISALPHA(*p))
22431 return NULL;
22432 *arg = p;
22433
22434 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22435 p += 4; /* termcap option */
22436 else
22437 while (ASCII_ISALPHA(*p))
22438 ++p;
22439 return p;
22440}
22441
22442/*
22443 * ":function"
22444 */
22445 void
22446ex_function(eap)
22447 exarg_T *eap;
22448{
22449 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022450 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 int j;
22452 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022454 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 char_u *name = NULL;
22456 char_u *p;
22457 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022458 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 garray_T newargs;
22460 garray_T newlines;
22461 int varargs = FALSE;
22462 int mustend = FALSE;
22463 int flags = 0;
22464 ufunc_T *fp;
22465 int indent;
22466 int nesting;
22467 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 dictitem_T *v;
22469 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022470 static int func_nr = 0; /* number for nameless function */
22471 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 hashtab_T *ht;
22473 int todo;
22474 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022475 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476
22477 /*
22478 * ":function" without argument: list functions.
22479 */
22480 if (ends_excmd(*eap->arg))
22481 {
22482 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022483 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022484 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022485 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022486 {
22487 if (!HASHITEM_EMPTY(hi))
22488 {
22489 --todo;
22490 fp = HI2UF(hi);
22491 if (!isdigit(*fp->uf_name))
22492 list_func_head(fp, FALSE);
22493 }
22494 }
22495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 eap->nextcmd = check_nextcmd(eap->arg);
22497 return;
22498 }
22499
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022500 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022501 * ":function /pat": list functions matching pattern.
22502 */
22503 if (*eap->arg == '/')
22504 {
22505 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22506 if (!eap->skip)
22507 {
22508 regmatch_T regmatch;
22509
22510 c = *p;
22511 *p = NUL;
22512 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22513 *p = c;
22514 if (regmatch.regprog != NULL)
22515 {
22516 regmatch.rm_ic = p_ic;
22517
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022518 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022519 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22520 {
22521 if (!HASHITEM_EMPTY(hi))
22522 {
22523 --todo;
22524 fp = HI2UF(hi);
22525 if (!isdigit(*fp->uf_name)
22526 && vim_regexec(&regmatch, fp->uf_name, 0))
22527 list_func_head(fp, FALSE);
22528 }
22529 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022530 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022531 }
22532 }
22533 if (*p == '/')
22534 ++p;
22535 eap->nextcmd = check_nextcmd(p);
22536 return;
22537 }
22538
22539 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 * Get the function name. There are these situations:
22541 * func normal function name
22542 * "name" == func, "fudi.fd_dict" == NULL
22543 * dict.func new dictionary entry
22544 * "name" == NULL, "fudi.fd_dict" set,
22545 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22546 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022547 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022548 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22549 * dict.func existing dict entry that's not a Funcref
22550 * "name" == NULL, "fudi.fd_dict" set,
22551 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022552 * s:func script-local function name
22553 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 name = trans_function_name(&p, eap->skip, 0, &fudi);
22557 paren = (vim_strchr(p, '(') != NULL);
22558 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 {
22560 /*
22561 * Return on an invalid expression in braces, unless the expression
22562 * evaluation has been cancelled due to an aborting error, an
22563 * interrupt, or an exception.
22564 */
22565 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022567 if (!eap->skip && fudi.fd_newkey != NULL)
22568 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022569 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 else
22573 eap->skip = TRUE;
22574 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022575
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 /* An error in a function call during evaluation of an expression in magic
22577 * braces should not cause the function not to be defined. */
22578 saved_did_emsg = did_emsg;
22579 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580
22581 /*
22582 * ":function func" with only function name: list function.
22583 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022584 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 {
22586 if (!ends_excmd(*skipwhite(p)))
22587 {
22588 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022589 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 }
22591 eap->nextcmd = check_nextcmd(p);
22592 if (eap->nextcmd != NULL)
22593 *p = NUL;
22594 if (!eap->skip && !got_int)
22595 {
22596 fp = find_func(name);
22597 if (fp != NULL)
22598 {
22599 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022602 if (FUNCLINE(fp, j) == NULL)
22603 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 msg_putchar('\n');
22605 msg_outnum((long)(j + 1));
22606 if (j < 9)
22607 msg_putchar(' ');
22608 if (j < 99)
22609 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 out_flush(); /* show a line at a time */
22612 ui_breakcheck();
22613 }
22614 if (!got_int)
22615 {
22616 msg_putchar('\n');
22617 msg_puts((char_u *)" endfunction");
22618 }
22619 }
22620 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022621 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022623 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 }
22625
22626 /*
22627 * ":function name(arg1, arg2)" Define function.
22628 */
22629 p = skipwhite(p);
22630 if (*p != '(')
22631 {
22632 if (!eap->skip)
22633 {
22634 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022635 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 }
22637 /* attempt to continue by skipping some text */
22638 if (vim_strchr(p, '(') != NULL)
22639 p = vim_strchr(p, '(');
22640 }
22641 p = skipwhite(p + 1);
22642
22643 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22644 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22645
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022646 if (!eap->skip)
22647 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022648 /* Check the name of the function. Unless it's a dictionary function
22649 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022650 if (name != NULL)
22651 arg = name;
22652 else
22653 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022654 if (arg != NULL && (fudi.fd_di == NULL
22655 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022656 {
22657 if (*arg == K_SPECIAL)
22658 j = 3;
22659 else
22660 j = 0;
22661 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22662 : eval_isnamec(arg[j])))
22663 ++j;
22664 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022665 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022666 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022667 /* Disallow using the g: dict. */
22668 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22669 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022670 }
22671
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 /*
22673 * Isolate the arguments: "arg1, arg2, ...)"
22674 */
22675 while (*p != ')')
22676 {
22677 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22678 {
22679 varargs = TRUE;
22680 p += 3;
22681 mustend = TRUE;
22682 }
22683 else
22684 {
22685 arg = p;
22686 while (ASCII_ISALNUM(*p) || *p == '_')
22687 ++p;
22688 if (arg == p || isdigit(*arg)
22689 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22690 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22691 {
22692 if (!eap->skip)
22693 EMSG2(_("E125: Illegal argument: %s"), arg);
22694 break;
22695 }
22696 if (ga_grow(&newargs, 1) == FAIL)
22697 goto erret;
22698 c = *p;
22699 *p = NUL;
22700 arg = vim_strsave(arg);
22701 if (arg == NULL)
22702 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022703
22704 /* Check for duplicate argument name. */
22705 for (i = 0; i < newargs.ga_len; ++i)
22706 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22707 {
22708 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022709 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022710 goto erret;
22711 }
22712
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22714 *p = c;
22715 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 if (*p == ',')
22717 ++p;
22718 else
22719 mustend = TRUE;
22720 }
22721 p = skipwhite(p);
22722 if (mustend && *p != ')')
22723 {
22724 if (!eap->skip)
22725 EMSG2(_(e_invarg2), eap->arg);
22726 break;
22727 }
22728 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022729 if (*p != ')')
22730 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 ++p; /* skip the ')' */
22732
Bram Moolenaare9a41262005-01-15 22:18:47 +000022733 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 for (;;)
22735 {
22736 p = skipwhite(p);
22737 if (STRNCMP(p, "range", 5) == 0)
22738 {
22739 flags |= FC_RANGE;
22740 p += 5;
22741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022742 else if (STRNCMP(p, "dict", 4) == 0)
22743 {
22744 flags |= FC_DICT;
22745 p += 4;
22746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 else if (STRNCMP(p, "abort", 5) == 0)
22748 {
22749 flags |= FC_ABORT;
22750 p += 5;
22751 }
22752 else
22753 break;
22754 }
22755
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022756 /* When there is a line break use what follows for the function body.
22757 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22758 if (*p == '\n')
22759 line_arg = p + 1;
22760 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 EMSG(_(e_trailing));
22762
22763 /*
22764 * Read the body of the function, until ":endfunction" is found.
22765 */
22766 if (KeyTyped)
22767 {
22768 /* Check if the function already exists, don't let the user type the
22769 * whole function before telling him it doesn't work! For a script we
22770 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 if (!eap->skip && !eap->forceit)
22772 {
22773 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22774 EMSG(_(e_funcdict));
22775 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022776 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022779 if (!eap->skip && did_emsg)
22780 goto erret;
22781
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 msg_putchar('\n'); /* don't overwrite the function name */
22783 cmdline_row = msg_row;
22784 }
22785
22786 indent = 2;
22787 nesting = 0;
22788 for (;;)
22789 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022790 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022791 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022792 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022793 saved_wait_return = FALSE;
22794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022796 sourcing_lnum_off = sourcing_lnum;
22797
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022798 if (line_arg != NULL)
22799 {
22800 /* Use eap->arg, split up in parts by line breaks. */
22801 theline = line_arg;
22802 p = vim_strchr(theline, '\n');
22803 if (p == NULL)
22804 line_arg += STRLEN(line_arg);
22805 else
22806 {
22807 *p = NUL;
22808 line_arg = p + 1;
22809 }
22810 }
22811 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 theline = getcmdline(':', 0L, indent);
22813 else
22814 theline = eap->getline(':', eap->cookie, indent);
22815 if (KeyTyped)
22816 lines_left = Rows - 1;
22817 if (theline == NULL)
22818 {
22819 EMSG(_("E126: Missing :endfunction"));
22820 goto erret;
22821 }
22822
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022823 /* Detect line continuation: sourcing_lnum increased more than one. */
22824 if (sourcing_lnum > sourcing_lnum_off + 1)
22825 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22826 else
22827 sourcing_lnum_off = 0;
22828
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 if (skip_until != NULL)
22830 {
22831 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22832 * don't check for ":endfunc". */
22833 if (STRCMP(theline, skip_until) == 0)
22834 {
22835 vim_free(skip_until);
22836 skip_until = NULL;
22837 }
22838 }
22839 else
22840 {
22841 /* skip ':' and blanks*/
22842 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22843 ;
22844
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022845 /* Check for "endfunction". */
22846 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022848 if (line_arg == NULL)
22849 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 break;
22851 }
22852
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022853 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 * at "end". */
22855 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22856 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022857 else if (STRNCMP(p, "if", 2) == 0
22858 || STRNCMP(p, "wh", 2) == 0
22859 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 || STRNCMP(p, "try", 3) == 0)
22861 indent += 2;
22862
22863 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022864 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022866 if (*p == '!')
22867 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022869 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22870 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022872 ++nesting;
22873 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
22875 }
22876
22877 /* Check for ":append" or ":insert". */
22878 p = skip_range(p, NULL);
22879 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22880 || (p[0] == 'i'
22881 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22882 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22883 skip_until = vim_strsave((char_u *)".");
22884
22885 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22886 arg = skipwhite(skiptowhite(p));
22887 if (arg[0] == '<' && arg[1] =='<'
22888 && ((p[0] == 'p' && p[1] == 'y'
22889 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22890 || (p[0] == 'p' && p[1] == 'e'
22891 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22892 || (p[0] == 't' && p[1] == 'c'
22893 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022894 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22895 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22897 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022898 || (p[0] == 'm' && p[1] == 'z'
22899 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 ))
22901 {
22902 /* ":python <<" continues until a dot, like ":append" */
22903 p = skipwhite(arg + 2);
22904 if (*p == NUL)
22905 skip_until = vim_strsave((char_u *)".");
22906 else
22907 skip_until = vim_strsave(p);
22908 }
22909 }
22910
22911 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022912 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022913 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022914 if (line_arg == NULL)
22915 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022917 }
22918
22919 /* Copy the line to newly allocated memory. get_one_sourceline()
22920 * allocates 250 bytes per line, this saves 80% on average. The cost
22921 * is an extra alloc/free. */
22922 p = vim_strsave(theline);
22923 if (p != NULL)
22924 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022925 if (line_arg == NULL)
22926 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022927 theline = p;
22928 }
22929
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022930 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22931
22932 /* Add NULL lines for continuation lines, so that the line count is
22933 * equal to the index in the growarray. */
22934 while (sourcing_lnum_off-- > 0)
22935 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022936
22937 /* Check for end of eap->arg. */
22938 if (line_arg != NULL && *line_arg == NUL)
22939 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 }
22941
22942 /* Don't define the function when skipping commands or when an error was
22943 * detected. */
22944 if (eap->skip || did_emsg)
22945 goto erret;
22946
22947 /*
22948 * If there are no errors, add the function
22949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022951 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022952 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022955 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022956 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022957 goto erret;
22958 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022959
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022960 fp = find_func(name);
22961 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022963 if (!eap->forceit)
22964 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022965 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022966 goto erret;
22967 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022968 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022969 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022970 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022971 name);
22972 goto erret;
22973 }
22974 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022975 ga_clear_strings(&(fp->uf_args));
22976 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022977 vim_free(name);
22978 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 }
22981 else
22982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022983 char numbuf[20];
22984
22985 fp = NULL;
22986 if (fudi.fd_newkey == NULL && !eap->forceit)
22987 {
22988 EMSG(_(e_funcdict));
22989 goto erret;
22990 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022991 if (fudi.fd_di == NULL)
22992 {
22993 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022994 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022995 goto erret;
22996 }
22997 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022998 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022999 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023000
23001 /* Give the function a sequential number. Can only be used with a
23002 * Funcref! */
23003 vim_free(name);
23004 sprintf(numbuf, "%d", ++func_nr);
23005 name = vim_strsave((char_u *)numbuf);
23006 if (name == NULL)
23007 goto erret;
23008 }
23009
23010 if (fp == NULL)
23011 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023012 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023013 {
23014 int slen, plen;
23015 char_u *scriptname;
23016
23017 /* Check that the autoload name matches the script name. */
23018 j = FAIL;
23019 if (sourcing_name != NULL)
23020 {
23021 scriptname = autoload_name(name);
23022 if (scriptname != NULL)
23023 {
23024 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023025 plen = (int)STRLEN(p);
23026 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023027 if (slen > plen && fnamecmp(p,
23028 sourcing_name + slen - plen) == 0)
23029 j = OK;
23030 vim_free(scriptname);
23031 }
23032 }
23033 if (j == FAIL)
23034 {
23035 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23036 goto erret;
23037 }
23038 }
23039
23040 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 if (fp == NULL)
23042 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023043
23044 if (fudi.fd_dict != NULL)
23045 {
23046 if (fudi.fd_di == NULL)
23047 {
23048 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023049 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023050 if (fudi.fd_di == NULL)
23051 {
23052 vim_free(fp);
23053 goto erret;
23054 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023055 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23056 {
23057 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023058 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023059 goto erret;
23060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023061 }
23062 else
23063 /* overwrite existing dict entry */
23064 clear_tv(&fudi.fd_di->di_tv);
23065 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023066 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023067 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023068 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023069
23070 /* behave like "dict" was used */
23071 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023072 }
23073
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023075 STRCPY(fp->uf_name, name);
23076 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023078 fp->uf_args = newargs;
23079 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023080#ifdef FEAT_PROFILE
23081 fp->uf_tml_count = NULL;
23082 fp->uf_tml_total = NULL;
23083 fp->uf_tml_self = NULL;
23084 fp->uf_profiling = FALSE;
23085 if (prof_def_func())
23086 func_do_profile(fp);
23087#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023088 fp->uf_varargs = varargs;
23089 fp->uf_flags = flags;
23090 fp->uf_calls = 0;
23091 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023092 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093
23094erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 ga_clear_strings(&newargs);
23096 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023097ret_free:
23098 vim_free(skip_until);
23099 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023102 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103}
23104
23105/*
23106 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023107 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023109 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023110 * TFN_INT: internal function name OK
23111 * TFN_QUIET: be quiet
23112 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 * Advances "pp" to just after the function name (if no error).
23114 */
23115 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023116trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 char_u **pp;
23118 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023122 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 char_u *start;
23124 char_u *end;
23125 int lead;
23126 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023129
23130 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023133
23134 /* Check for hard coded <SNR>: already translated function ID (from a user
23135 * command). */
23136 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23137 && (*pp)[2] == (int)KE_SNR)
23138 {
23139 *pp += 3;
23140 len = get_id_len(pp) + 3;
23141 return vim_strnsave(start, len);
23142 }
23143
23144 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23145 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023147 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023149
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023150 /* Note that TFN_ flags use the same values as GLV_ flags. */
23151 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023152 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023153 if (end == start)
23154 {
23155 if (!skip)
23156 EMSG(_("E129: Function name required"));
23157 goto theend;
23158 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023159 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023160 {
23161 /*
23162 * Report an invalid expression in braces, unless the expression
23163 * evaluation has been cancelled due to an aborting error, an
23164 * interrupt, or an exception.
23165 */
23166 if (!aborting())
23167 {
23168 if (end != NULL)
23169 EMSG2(_(e_invarg2), start);
23170 }
23171 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023172 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023173 goto theend;
23174 }
23175
23176 if (lv.ll_tv != NULL)
23177 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023178 if (fdp != NULL)
23179 {
23180 fdp->fd_dict = lv.ll_dict;
23181 fdp->fd_newkey = lv.ll_newkey;
23182 lv.ll_newkey = NULL;
23183 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023184 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023185 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23186 {
23187 name = vim_strsave(lv.ll_tv->vval.v_string);
23188 *pp = end;
23189 }
23190 else
23191 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023192 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23193 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023194 EMSG(_(e_funcref));
23195 else
23196 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023197 name = NULL;
23198 }
23199 goto theend;
23200 }
23201
23202 if (lv.ll_name == NULL)
23203 {
23204 /* Error found, but continue after the function name. */
23205 *pp = end;
23206 goto theend;
23207 }
23208
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023209 /* Check if the name is a Funcref. If so, use the value. */
23210 if (lv.ll_exp_name != NULL)
23211 {
23212 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023213 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023214 if (name == lv.ll_exp_name)
23215 name = NULL;
23216 }
23217 else
23218 {
23219 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023220 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023221 if (name == *pp)
23222 name = NULL;
23223 }
23224 if (name != NULL)
23225 {
23226 name = vim_strsave(name);
23227 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023228 if (STRNCMP(name, "<SNR>", 5) == 0)
23229 {
23230 /* Change "<SNR>" to the byte sequence. */
23231 name[0] = K_SPECIAL;
23232 name[1] = KS_EXTRA;
23233 name[2] = (int)KE_SNR;
23234 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23235 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023236 goto theend;
23237 }
23238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023239 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023240 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023241 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023242 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23243 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23244 {
23245 /* When there was "s:" already or the name expanded to get a
23246 * leading "s:" then remove it. */
23247 lv.ll_name += 2;
23248 len -= 2;
23249 lead = 2;
23250 }
23251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023252 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023254 /* skip over "s:" and "g:" */
23255 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023256 lv.ll_name += 2;
23257 len = (int)(end - lv.ll_name);
23258 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023259
23260 /*
23261 * Copy the function name to allocated memory.
23262 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23263 * Accept <SNR>123_name() outside a script.
23264 */
23265 if (skip)
23266 lead = 0; /* do nothing */
23267 else if (lead > 0)
23268 {
23269 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023270 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23271 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023272 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023273 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023274 if (current_SID <= 0)
23275 {
23276 EMSG(_(e_usingsid));
23277 goto theend;
23278 }
23279 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23280 lead += (int)STRLEN(sid_buf);
23281 }
23282 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023283 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023284 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023285 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023286 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023287 goto theend;
23288 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023289 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023290 {
23291 char_u *cp = vim_strchr(lv.ll_name, ':');
23292
23293 if (cp != NULL && cp < end)
23294 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023295 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023296 goto theend;
23297 }
23298 }
23299
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023300 name = alloc((unsigned)(len + lead + 1));
23301 if (name != NULL)
23302 {
23303 if (lead > 0)
23304 {
23305 name[0] = K_SPECIAL;
23306 name[1] = KS_EXTRA;
23307 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023308 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023309 STRCPY(name + 3, sid_buf);
23310 }
23311 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023312 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023313 }
23314 *pp = end;
23315
23316theend:
23317 clear_lval(&lv);
23318 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319}
23320
23321/*
23322 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23323 * Return 2 if "p" starts with "s:".
23324 * Return 0 otherwise.
23325 */
23326 static int
23327eval_fname_script(p)
23328 char_u *p;
23329{
23330 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23331 || STRNICMP(p + 1, "SNR>", 4) == 0))
23332 return 5;
23333 if (p[0] == 's' && p[1] == ':')
23334 return 2;
23335 return 0;
23336}
23337
23338/*
23339 * Return TRUE if "p" starts with "<SID>" or "s:".
23340 * Only works if eval_fname_script() returned non-zero for "p"!
23341 */
23342 static int
23343eval_fname_sid(p)
23344 char_u *p;
23345{
23346 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23347}
23348
23349/*
23350 * List the head of the function: "name(arg1, arg2)".
23351 */
23352 static void
23353list_func_head(fp, indent)
23354 ufunc_T *fp;
23355 int indent;
23356{
23357 int j;
23358
23359 msg_start();
23360 if (indent)
23361 MSG_PUTS(" ");
23362 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023363 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 {
23365 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023366 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 }
23368 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023369 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023371 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 {
23373 if (j)
23374 MSG_PUTS(", ");
23375 msg_puts(FUNCARG(fp, j));
23376 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023377 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 {
23379 if (j)
23380 MSG_PUTS(", ");
23381 MSG_PUTS("...");
23382 }
23383 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023384 if (fp->uf_flags & FC_ABORT)
23385 MSG_PUTS(" abort");
23386 if (fp->uf_flags & FC_RANGE)
23387 MSG_PUTS(" range");
23388 if (fp->uf_flags & FC_DICT)
23389 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023390 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023391 if (p_verbose > 0)
23392 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393}
23394
23395/*
23396 * Find a function by name, return pointer to it in ufuncs.
23397 * Return NULL for unknown function.
23398 */
23399 static ufunc_T *
23400find_func(name)
23401 char_u *name;
23402{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023403 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023405 hi = hash_find(&func_hashtab, name);
23406 if (!HASHITEM_EMPTY(hi))
23407 return HI2UF(hi);
23408 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409}
23410
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023411#if defined(EXITFREE) || defined(PROTO)
23412 void
23413free_all_functions()
23414{
23415 hashitem_T *hi;
23416
23417 /* Need to start all over every time, because func_free() may change the
23418 * hash table. */
23419 while (func_hashtab.ht_used > 0)
23420 for (hi = func_hashtab.ht_array; ; ++hi)
23421 if (!HASHITEM_EMPTY(hi))
23422 {
23423 func_free(HI2UF(hi));
23424 break;
23425 }
23426}
23427#endif
23428
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023429 int
23430translated_function_exists(name)
23431 char_u *name;
23432{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023433 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023434 return find_internal_func(name) >= 0;
23435 return find_func(name) != NULL;
23436}
23437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023438/*
23439 * Return TRUE if a function "name" exists.
23440 */
23441 static int
23442function_exists(name)
23443 char_u *name;
23444{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023445 char_u *nm = name;
23446 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023447 int n = FALSE;
23448
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023449 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23450 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023451 nm = skipwhite(nm);
23452
23453 /* Only accept "funcname", "funcname ", "funcname (..." and
23454 * "funcname(...", not "funcname!...". */
23455 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023456 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023457 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023458 return n;
23459}
23460
Bram Moolenaara1544c02013-05-30 12:35:52 +020023461 char_u *
23462get_expanded_name(name, check)
23463 char_u *name;
23464 int check;
23465{
23466 char_u *nm = name;
23467 char_u *p;
23468
23469 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23470
23471 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023472 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023473 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023474
Bram Moolenaara1544c02013-05-30 12:35:52 +020023475 vim_free(p);
23476 return NULL;
23477}
23478
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023479/*
23480 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023481 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23482 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023483 */
23484 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023485builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023486 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023487 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023488{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023489 char_u *p;
23490
23491 if (!ASCII_ISLOWER(name[0]))
23492 return FALSE;
23493 p = vim_strchr(name, AUTOLOAD_CHAR);
23494 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023495}
23496
Bram Moolenaar05159a02005-02-26 23:04:13 +000023497#if defined(FEAT_PROFILE) || defined(PROTO)
23498/*
23499 * Start profiling function "fp".
23500 */
23501 static void
23502func_do_profile(fp)
23503 ufunc_T *fp;
23504{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023505 int len = fp->uf_lines.ga_len;
23506
23507 if (len == 0)
23508 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023509 fp->uf_tm_count = 0;
23510 profile_zero(&fp->uf_tm_self);
23511 profile_zero(&fp->uf_tm_total);
23512 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023513 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023514 if (fp->uf_tml_total == NULL)
23515 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023516 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517 if (fp->uf_tml_self == NULL)
23518 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023519 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023520 fp->uf_tml_idx = -1;
23521 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23522 || fp->uf_tml_self == NULL)
23523 return; /* out of memory */
23524
23525 fp->uf_profiling = TRUE;
23526}
23527
23528/*
23529 * Dump the profiling results for all functions in file "fd".
23530 */
23531 void
23532func_dump_profile(fd)
23533 FILE *fd;
23534{
23535 hashitem_T *hi;
23536 int todo;
23537 ufunc_T *fp;
23538 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023539 ufunc_T **sorttab;
23540 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023542 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023543 if (todo == 0)
23544 return; /* nothing to dump */
23545
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023546 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023547
Bram Moolenaar05159a02005-02-26 23:04:13 +000023548 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23549 {
23550 if (!HASHITEM_EMPTY(hi))
23551 {
23552 --todo;
23553 fp = HI2UF(hi);
23554 if (fp->uf_profiling)
23555 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023556 if (sorttab != NULL)
23557 sorttab[st_len++] = fp;
23558
Bram Moolenaar05159a02005-02-26 23:04:13 +000023559 if (fp->uf_name[0] == K_SPECIAL)
23560 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23561 else
23562 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23563 if (fp->uf_tm_count == 1)
23564 fprintf(fd, "Called 1 time\n");
23565 else
23566 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23567 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23568 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23569 fprintf(fd, "\n");
23570 fprintf(fd, "count total (s) self (s)\n");
23571
23572 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23573 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023574 if (FUNCLINE(fp, i) == NULL)
23575 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023576 prof_func_line(fd, fp->uf_tml_count[i],
23577 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023578 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23579 }
23580 fprintf(fd, "\n");
23581 }
23582 }
23583 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023584
23585 if (sorttab != NULL && st_len > 0)
23586 {
23587 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23588 prof_total_cmp);
23589 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23590 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23591 prof_self_cmp);
23592 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23593 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023594
23595 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023596}
Bram Moolenaar73830342005-02-28 22:48:19 +000023597
23598 static void
23599prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23600 FILE *fd;
23601 ufunc_T **sorttab;
23602 int st_len;
23603 char *title;
23604 int prefer_self; /* when equal print only self time */
23605{
23606 int i;
23607 ufunc_T *fp;
23608
23609 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23610 fprintf(fd, "count total (s) self (s) function\n");
23611 for (i = 0; i < 20 && i < st_len; ++i)
23612 {
23613 fp = sorttab[i];
23614 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23615 prefer_self);
23616 if (fp->uf_name[0] == K_SPECIAL)
23617 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23618 else
23619 fprintf(fd, " %s()\n", fp->uf_name);
23620 }
23621 fprintf(fd, "\n");
23622}
23623
23624/*
23625 * Print the count and times for one function or function line.
23626 */
23627 static void
23628prof_func_line(fd, count, total, self, prefer_self)
23629 FILE *fd;
23630 int count;
23631 proftime_T *total;
23632 proftime_T *self;
23633 int prefer_self; /* when equal print only self time */
23634{
23635 if (count > 0)
23636 {
23637 fprintf(fd, "%5d ", count);
23638 if (prefer_self && profile_equal(total, self))
23639 fprintf(fd, " ");
23640 else
23641 fprintf(fd, "%s ", profile_msg(total));
23642 if (!prefer_self && profile_equal(total, self))
23643 fprintf(fd, " ");
23644 else
23645 fprintf(fd, "%s ", profile_msg(self));
23646 }
23647 else
23648 fprintf(fd, " ");
23649}
23650
23651/*
23652 * Compare function for total time sorting.
23653 */
23654 static int
23655#ifdef __BORLANDC__
23656_RTLENTRYF
23657#endif
23658prof_total_cmp(s1, s2)
23659 const void *s1;
23660 const void *s2;
23661{
23662 ufunc_T *p1, *p2;
23663
23664 p1 = *(ufunc_T **)s1;
23665 p2 = *(ufunc_T **)s2;
23666 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23667}
23668
23669/*
23670 * Compare function for self time sorting.
23671 */
23672 static int
23673#ifdef __BORLANDC__
23674_RTLENTRYF
23675#endif
23676prof_self_cmp(s1, s2)
23677 const void *s1;
23678 const void *s2;
23679{
23680 ufunc_T *p1, *p2;
23681
23682 p1 = *(ufunc_T **)s1;
23683 p2 = *(ufunc_T **)s2;
23684 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23685}
23686
Bram Moolenaar05159a02005-02-26 23:04:13 +000023687#endif
23688
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023689/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023691 * Return TRUE if a package was loaded.
23692 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023693 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023694script_autoload(name, reload)
23695 char_u *name;
23696 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023697{
23698 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023699 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023700 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023701 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023702
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023703 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023704 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023705 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023706 return FALSE;
23707
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023708 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023709
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023710 /* Find the name in the list of previously loaded package names. Skip
23711 * "autoload/", it's always the same. */
23712 for (i = 0; i < ga_loaded.ga_len; ++i)
23713 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23714 break;
23715 if (!reload && i < ga_loaded.ga_len)
23716 ret = FALSE; /* was loaded already */
23717 else
23718 {
23719 /* Remember the name if it wasn't loaded already. */
23720 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23721 {
23722 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23723 tofree = NULL;
23724 }
23725
23726 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023727 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023728 ret = TRUE;
23729 }
23730
23731 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023732 return ret;
23733}
23734
23735/*
23736 * Return the autoload script name for a function or variable name.
23737 * Returns NULL when out of memory.
23738 */
23739 static char_u *
23740autoload_name(name)
23741 char_u *name;
23742{
23743 char_u *p;
23744 char_u *scriptname;
23745
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023746 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023747 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23748 if (scriptname == NULL)
23749 return FALSE;
23750 STRCPY(scriptname, "autoload/");
23751 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023752 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023753 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023754 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023755 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023757}
23758
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23760
23761/*
23762 * Function given to ExpandGeneric() to obtain the list of user defined
23763 * function names.
23764 */
23765 char_u *
23766get_user_func_name(xp, idx)
23767 expand_T *xp;
23768 int idx;
23769{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023770 static long_u done;
23771 static hashitem_T *hi;
23772 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773
23774 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023776 done = 0;
23777 hi = func_hashtab.ht_array;
23778 }
23779 if (done < func_hashtab.ht_used)
23780 {
23781 if (done++ > 0)
23782 ++hi;
23783 while (HASHITEM_EMPTY(hi))
23784 ++hi;
23785 fp = HI2UF(hi);
23786
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023787 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023788 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023789
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023790 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23791 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792
23793 cat_func_name(IObuff, fp);
23794 if (xp->xp_context != EXPAND_USER_FUNC)
23795 {
23796 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 STRCAT(IObuff, ")");
23799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 return IObuff;
23801 }
23802 return NULL;
23803}
23804
23805#endif /* FEAT_CMDL_COMPL */
23806
23807/*
23808 * Copy the function name of "fp" to buffer "buf".
23809 * "buf" must be able to hold the function name plus three bytes.
23810 * Takes care of script-local function names.
23811 */
23812 static void
23813cat_func_name(buf, fp)
23814 char_u *buf;
23815 ufunc_T *fp;
23816{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023817 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 {
23819 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023820 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 }
23822 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824}
23825
23826/*
23827 * ":delfunction {name}"
23828 */
23829 void
23830ex_delfunction(eap)
23831 exarg_T *eap;
23832{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023833 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 char_u *p;
23835 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023836 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837
23838 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 name = trans_function_name(&p, eap->skip, 0, &fudi);
23840 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023842 {
23843 if (fudi.fd_dict != NULL && !eap->skip)
23844 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 if (!ends_excmd(*skipwhite(p)))
23848 {
23849 vim_free(name);
23850 EMSG(_(e_trailing));
23851 return;
23852 }
23853 eap->nextcmd = check_nextcmd(p);
23854 if (eap->nextcmd != NULL)
23855 *p = NUL;
23856
23857 if (!eap->skip)
23858 fp = find_func(name);
23859 vim_free(name);
23860
23861 if (!eap->skip)
23862 {
23863 if (fp == NULL)
23864 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023865 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 return;
23867 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023868 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 {
23870 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23871 return;
23872 }
23873
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023874 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023876 /* Delete the dict item that refers to the function, it will
23877 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023878 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 else
23881 func_free(fp);
23882 }
23883}
23884
23885/*
23886 * Free a function and remove it from the list of functions.
23887 */
23888 static void
23889func_free(fp)
23890 ufunc_T *fp;
23891{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023892 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023893
23894 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 ga_clear_strings(&(fp->uf_args));
23896 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023897#ifdef FEAT_PROFILE
23898 vim_free(fp->uf_tml_count);
23899 vim_free(fp->uf_tml_total);
23900 vim_free(fp->uf_tml_self);
23901#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023903 /* remove the function from the function hashtable */
23904 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23905 if (HASHITEM_EMPTY(hi))
23906 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023908 hash_remove(&func_hashtab, hi);
23909
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023910 vim_free(fp);
23911}
23912
23913/*
23914 * Unreference a Function: decrement the reference count and free it when it
23915 * becomes zero. Only for numbered functions.
23916 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023917 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023918func_unref(name)
23919 char_u *name;
23920{
23921 ufunc_T *fp;
23922
23923 if (name != NULL && isdigit(*name))
23924 {
23925 fp = find_func(name);
23926 if (fp == NULL)
23927 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023928 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023929 {
23930 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023931 * when "uf_calls" becomes zero. */
23932 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 func_free(fp);
23934 }
23935 }
23936}
23937
23938/*
23939 * Count a reference to a Function.
23940 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023941 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942func_ref(name)
23943 char_u *name;
23944{
23945 ufunc_T *fp;
23946
23947 if (name != NULL && isdigit(*name))
23948 {
23949 fp = find_func(name);
23950 if (fp == NULL)
23951 EMSG2(_(e_intern2), "func_ref()");
23952 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023953 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 }
23955}
23956
23957/*
23958 * Call a user function.
23959 */
23960 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023961call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 ufunc_T *fp; /* pointer to function */
23963 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023964 typval_T *argvars; /* arguments */
23965 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 linenr_T firstline; /* first line of range */
23967 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023968 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969{
Bram Moolenaar33570922005-01-25 22:26:29 +000023970 char_u *save_sourcing_name;
23971 linenr_T save_sourcing_lnum;
23972 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023973 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023974 int save_did_emsg;
23975 static int depth = 0;
23976 dictitem_T *v;
23977 int fixvar_idx = 0; /* index in fixvar[] */
23978 int i;
23979 int ai;
23980 char_u numbuf[NUMBUFLEN];
23981 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023982 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983#ifdef FEAT_PROFILE
23984 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023985 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987
23988 /* If depth of calling is getting too high, don't execute the function */
23989 if (depth >= p_mfd)
23990 {
23991 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023992 rettv->v_type = VAR_NUMBER;
23993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 return;
23995 }
23996 ++depth;
23997
23998 line_breakcheck(); /* check for CTRL-C hit */
23999
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024000 fc = (funccall_T *)alloc(sizeof(funccall_T));
24001 fc->caller = current_funccal;
24002 current_funccal = fc;
24003 fc->func = fp;
24004 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024005 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024006 fc->linenr = 0;
24007 fc->returned = FALSE;
24008 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024010 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24011 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012
Bram Moolenaar33570922005-01-25 22:26:29 +000024013 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024014 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024015 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24016 * each argument variable and saves a lot of time.
24017 */
24018 /*
24019 * Init l: variables.
24020 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024021 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024022 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024023 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024024 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24025 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024026 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024027 name = v->di_key;
24028 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024029 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024030 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024031 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024032 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024033 v->di_tv.vval.v_dict = selfdict;
24034 ++selfdict->dv_refcount;
24035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024036
Bram Moolenaar33570922005-01-25 22:26:29 +000024037 /*
24038 * Init a: variables.
24039 * Set a:0 to "argcount".
24040 * Set a:000 to a list with room for the "..." arguments.
24041 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024042 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024043 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024044 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024045 /* Use "name" to avoid a warning from some compiler that checks the
24046 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024047 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024048 name = v->di_key;
24049 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024050 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024051 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024052 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024053 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024054 v->di_tv.vval.v_list = &fc->l_varlist;
24055 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24056 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24057 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024058
24059 /*
24060 * Set a:firstline to "firstline" and a:lastline to "lastline".
24061 * Set a:name to named arguments.
24062 * Set a:N to the "..." arguments.
24063 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024064 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024065 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024066 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024067 (varnumber_T)lastline);
24068 for (i = 0; i < argcount; ++i)
24069 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024070 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024071 if (ai < 0)
24072 /* named argument a:name */
24073 name = FUNCARG(fp, i);
24074 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024075 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024076 /* "..." argument a:1, a:2, etc. */
24077 sprintf((char *)numbuf, "%d", ai + 1);
24078 name = numbuf;
24079 }
24080 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24081 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024082 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024083 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24084 }
24085 else
24086 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024087 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24088 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 if (v == NULL)
24090 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024091 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 }
24093 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024094 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024095
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024096 /* Note: the values are copied directly to avoid alloc/free.
24097 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024099 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024100
24101 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24102 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024103 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24104 fc->l_listitems[ai].li_tv = argvars[i];
24105 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024106 }
24107 }
24108
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 /* Don't redraw while executing the function. */
24110 ++RedrawingDisabled;
24111 save_sourcing_name = sourcing_name;
24112 save_sourcing_lnum = sourcing_lnum;
24113 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024114 /* need space for function name + ("function " + 3) or "[number]" */
24115 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24116 + STRLEN(fp->uf_name) + 20;
24117 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 if (sourcing_name != NULL)
24119 {
24120 if (save_sourcing_name != NULL
24121 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024122 sprintf((char *)sourcing_name, "%s[%d]..",
24123 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 else
24125 STRCPY(sourcing_name, "function ");
24126 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24127
24128 if (p_verbose >= 12)
24129 {
24130 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024131 verbose_enter_scroll();
24132
Bram Moolenaar555b2802005-05-19 21:08:39 +000024133 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 if (p_verbose >= 14)
24135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024137 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024138 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024139 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140
24141 msg_puts((char_u *)"(");
24142 for (i = 0; i < argcount; ++i)
24143 {
24144 if (i > 0)
24145 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024146 if (argvars[i].v_type == VAR_NUMBER)
24147 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 else
24149 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024150 /* Do not want errors such as E724 here. */
24151 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024152 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024153 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024154 if (s != NULL)
24155 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024156 if (vim_strsize(s) > MSG_BUF_CLEN)
24157 {
24158 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24159 s = buf;
24160 }
24161 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024162 vim_free(tofree);
24163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165 }
24166 msg_puts((char_u *)")");
24167 }
24168 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024169
24170 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 --no_wait_return;
24172 }
24173 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024174#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024175 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024176 {
24177 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24178 func_do_profile(fp);
24179 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024180 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024181 {
24182 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024183 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024184 profile_zero(&fp->uf_tm_children);
24185 }
24186 script_prof_save(&wait_start);
24187 }
24188#endif
24189
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024191 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 save_did_emsg = did_emsg;
24193 did_emsg = FALSE;
24194
24195 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024196 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24198
24199 --RedrawingDisabled;
24200
24201 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024202 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024204 clear_tv(rettv);
24205 rettv->v_type = VAR_NUMBER;
24206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 }
24208
Bram Moolenaar05159a02005-02-26 23:04:13 +000024209#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024210 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024211 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024212 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024213 profile_end(&call_start);
24214 profile_sub_wait(&wait_start, &call_start);
24215 profile_add(&fp->uf_tm_total, &call_start);
24216 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024217 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024218 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024219 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24220 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221 }
24222 }
24223#endif
24224
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 /* when being verbose, mention the return value */
24226 if (p_verbose >= 12)
24227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024229 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024232 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024233 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024234 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024235 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024238 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024239 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024240 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024241 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024242
Bram Moolenaar555b2802005-05-19 21:08:39 +000024243 /* The value may be very long. Skip the middle part, so that we
24244 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024245 * truncate it at the end. Don't want errors such as E724 here. */
24246 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024247 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024248 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024249 if (s != NULL)
24250 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024251 if (vim_strsize(s) > MSG_BUF_CLEN)
24252 {
24253 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24254 s = buf;
24255 }
24256 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024257 vim_free(tofree);
24258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 }
24260 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024261
24262 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 --no_wait_return;
24264 }
24265
24266 vim_free(sourcing_name);
24267 sourcing_name = save_sourcing_name;
24268 sourcing_lnum = save_sourcing_lnum;
24269 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024270#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024271 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024272 script_prof_restore(&wait_start);
24273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274
24275 if (p_verbose >= 12 && sourcing_name != NULL)
24276 {
24277 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024278 verbose_enter_scroll();
24279
Bram Moolenaar555b2802005-05-19 21:08:39 +000024280 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024282
24283 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 --no_wait_return;
24285 }
24286
24287 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024288 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024290
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024291 /* If the a:000 list and the l: and a: dicts are not referenced we can
24292 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024293 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24294 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24295 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24296 {
24297 free_funccal(fc, FALSE);
24298 }
24299 else
24300 {
24301 hashitem_T *hi;
24302 listitem_T *li;
24303 int todo;
24304
24305 /* "fc" is still in use. This can happen when returning "a:000" or
24306 * assigning "l:" to a global variable.
24307 * Link "fc" in the list for garbage collection later. */
24308 fc->caller = previous_funccal;
24309 previous_funccal = fc;
24310
24311 /* Make a copy of the a: variables, since we didn't do that above. */
24312 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24313 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24314 {
24315 if (!HASHITEM_EMPTY(hi))
24316 {
24317 --todo;
24318 v = HI2DI(hi);
24319 copy_tv(&v->di_tv, &v->di_tv);
24320 }
24321 }
24322
24323 /* Make a copy of the a:000 items, since we didn't do that above. */
24324 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24325 copy_tv(&li->li_tv, &li->li_tv);
24326 }
24327}
24328
24329/*
24330 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024331 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024332 */
24333 static int
24334can_free_funccal(fc, copyID)
24335 funccall_T *fc;
24336 int copyID;
24337{
24338 return (fc->l_varlist.lv_copyID != copyID
24339 && fc->l_vars.dv_copyID != copyID
24340 && fc->l_avars.dv_copyID != copyID);
24341}
24342
24343/*
24344 * Free "fc" and what it contains.
24345 */
24346 static void
24347free_funccal(fc, free_val)
24348 funccall_T *fc;
24349 int free_val; /* a: vars were allocated */
24350{
24351 listitem_T *li;
24352
24353 /* The a: variables typevals may not have been allocated, only free the
24354 * allocated variables. */
24355 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24356
24357 /* free all l: variables */
24358 vars_clear(&fc->l_vars.dv_hashtab);
24359
24360 /* Free the a:000 variables if they were allocated. */
24361 if (free_val)
24362 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24363 clear_tv(&li->li_tv);
24364
24365 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366}
24367
24368/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024369 * Add a number variable "name" to dict "dp" with value "nr".
24370 */
24371 static void
24372add_nr_var(dp, v, name, nr)
24373 dict_T *dp;
24374 dictitem_T *v;
24375 char *name;
24376 varnumber_T nr;
24377{
24378 STRCPY(v->di_key, name);
24379 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24380 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24381 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024382 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024383 v->di_tv.vval.v_number = nr;
24384}
24385
24386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 * ":return [expr]"
24388 */
24389 void
24390ex_return(eap)
24391 exarg_T *eap;
24392{
24393 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024394 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 int returning = FALSE;
24396
24397 if (current_funccal == NULL)
24398 {
24399 EMSG(_("E133: :return not inside a function"));
24400 return;
24401 }
24402
24403 if (eap->skip)
24404 ++emsg_skip;
24405
24406 eap->nextcmd = NULL;
24407 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024408 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 {
24410 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024411 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 }
24415 /* It's safer to return also on error. */
24416 else if (!eap->skip)
24417 {
24418 /*
24419 * Return unless the expression evaluation has been cancelled due to an
24420 * aborting error, an interrupt, or an exception.
24421 */
24422 if (!aborting())
24423 returning = do_return(eap, FALSE, TRUE, NULL);
24424 }
24425
24426 /* When skipping or the return gets pending, advance to the next command
24427 * in this line (!returning). Otherwise, ignore the rest of the line.
24428 * Following lines will be ignored by get_func_line(). */
24429 if (returning)
24430 eap->nextcmd = NULL;
24431 else if (eap->nextcmd == NULL) /* no argument */
24432 eap->nextcmd = check_nextcmd(arg);
24433
24434 if (eap->skip)
24435 --emsg_skip;
24436}
24437
24438/*
24439 * Return from a function. Possibly makes the return pending. Also called
24440 * for a pending return at the ":endtry" or after returning from an extra
24441 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024442 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024443 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 * FALSE when the return gets pending.
24445 */
24446 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024447do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 exarg_T *eap;
24449 int reanimate;
24450 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024451 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452{
24453 int idx;
24454 struct condstack *cstack = eap->cstack;
24455
24456 if (reanimate)
24457 /* Undo the return. */
24458 current_funccal->returned = FALSE;
24459
24460 /*
24461 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24462 * not in its finally clause (which then is to be executed next) is found.
24463 * In this case, make the ":return" pending for execution at the ":endtry".
24464 * Otherwise, return normally.
24465 */
24466 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24467 if (idx >= 0)
24468 {
24469 cstack->cs_pending[idx] = CSTP_RETURN;
24470
24471 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024472 /* A pending return again gets pending. "rettv" points to an
24473 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024475 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 else
24477 {
24478 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024479 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024481 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024483 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 {
24485 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024486 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024487 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 else
24489 EMSG(_(e_outofmem));
24490 }
24491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024492 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493
24494 if (reanimate)
24495 {
24496 /* The pending return value could be overwritten by a ":return"
24497 * without argument in a finally clause; reset the default
24498 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024499 current_funccal->rettv->v_type = VAR_NUMBER;
24500 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501 }
24502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024503 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 }
24505 else
24506 {
24507 current_funccal->returned = TRUE;
24508
24509 /* If the return is carried out now, store the return value. For
24510 * a return immediately after reanimation, the value is already
24511 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024512 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024514 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024515 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024517 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 }
24519 }
24520
24521 return idx < 0;
24522}
24523
24524/*
24525 * Free the variable with a pending return value.
24526 */
24527 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024528discard_pending_return(rettv)
24529 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530{
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532}
24533
24534/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024535 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 * is an allocated string. Used by report_pending() for verbose messages.
24537 */
24538 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024539get_return_cmd(rettv)
24540 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024542 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024543 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024546 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024547 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024548 if (s == NULL)
24549 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024550
24551 STRCPY(IObuff, ":return ");
24552 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24553 if (STRLEN(s) + 8 >= IOSIZE)
24554 STRCPY(IObuff + IOSIZE - 4, "...");
24555 vim_free(tofree);
24556 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557}
24558
24559/*
24560 * Get next function line.
24561 * Called by do_cmdline() to get the next line.
24562 * Returns allocated string, or NULL for end of function.
24563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 char_u *
24565get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024566 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024568 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569{
Bram Moolenaar33570922005-01-25 22:26:29 +000024570 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024571 ufunc_T *fp = fcp->func;
24572 char_u *retval;
24573 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 /* If breakpoints have been added/deleted need to check for it. */
24576 if (fcp->dbg_tick != debug_tick)
24577 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024578 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 sourcing_lnum);
24580 fcp->dbg_tick = debug_tick;
24581 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024582#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024583 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024584 func_line_end(cookie);
24585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586
Bram Moolenaar05159a02005-02-26 23:04:13 +000024587 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024588 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24589 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024590 retval = NULL;
24591 else
24592 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024593 /* Skip NULL lines (continuation lines). */
24594 while (fcp->linenr < gap->ga_len
24595 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24596 ++fcp->linenr;
24597 if (fcp->linenr >= gap->ga_len)
24598 retval = NULL;
24599 else
24600 {
24601 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24602 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024604 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024605 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 }
24609
24610 /* Did we encounter a breakpoint? */
24611 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24612 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024613 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024615 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 sourcing_lnum);
24617 fcp->dbg_tick = debug_tick;
24618 }
24619
24620 return retval;
24621}
24622
Bram Moolenaar05159a02005-02-26 23:04:13 +000024623#if defined(FEAT_PROFILE) || defined(PROTO)
24624/*
24625 * Called when starting to read a function line.
24626 * "sourcing_lnum" must be correct!
24627 * When skipping lines it may not actually be executed, but we won't find out
24628 * until later and we need to store the time now.
24629 */
24630 void
24631func_line_start(cookie)
24632 void *cookie;
24633{
24634 funccall_T *fcp = (funccall_T *)cookie;
24635 ufunc_T *fp = fcp->func;
24636
24637 if (fp->uf_profiling && sourcing_lnum >= 1
24638 && sourcing_lnum <= fp->uf_lines.ga_len)
24639 {
24640 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024641 /* Skip continuation lines. */
24642 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24643 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024644 fp->uf_tml_execed = FALSE;
24645 profile_start(&fp->uf_tml_start);
24646 profile_zero(&fp->uf_tml_children);
24647 profile_get_wait(&fp->uf_tml_wait);
24648 }
24649}
24650
24651/*
24652 * Called when actually executing a function line.
24653 */
24654 void
24655func_line_exec(cookie)
24656 void *cookie;
24657{
24658 funccall_T *fcp = (funccall_T *)cookie;
24659 ufunc_T *fp = fcp->func;
24660
24661 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24662 fp->uf_tml_execed = TRUE;
24663}
24664
24665/*
24666 * Called when done with a function line.
24667 */
24668 void
24669func_line_end(cookie)
24670 void *cookie;
24671{
24672 funccall_T *fcp = (funccall_T *)cookie;
24673 ufunc_T *fp = fcp->func;
24674
24675 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24676 {
24677 if (fp->uf_tml_execed)
24678 {
24679 ++fp->uf_tml_count[fp->uf_tml_idx];
24680 profile_end(&fp->uf_tml_start);
24681 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024682 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024683 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24684 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024685 }
24686 fp->uf_tml_idx = -1;
24687 }
24688}
24689#endif
24690
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691/*
24692 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024693 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 */
24695 int
24696func_has_ended(cookie)
24697 void *cookie;
24698{
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700
24701 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24702 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 || fcp->returned);
24705}
24706
24707/*
24708 * return TRUE if cookie indicates a function which "abort"s on errors.
24709 */
24710 int
24711func_has_abort(cookie)
24712 void *cookie;
24713{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024714 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715}
24716
24717#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24718typedef enum
24719{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024720 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24721 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24722 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723} var_flavour_T;
24724
24725static var_flavour_T var_flavour __ARGS((char_u *varname));
24726
24727 static var_flavour_T
24728var_flavour(varname)
24729 char_u *varname;
24730{
24731 char_u *p = varname;
24732
24733 if (ASCII_ISUPPER(*p))
24734 {
24735 while (*(++p))
24736 if (ASCII_ISLOWER(*p))
24737 return VAR_FLAVOUR_SESSION;
24738 return VAR_FLAVOUR_VIMINFO;
24739 }
24740 else
24741 return VAR_FLAVOUR_DEFAULT;
24742}
24743#endif
24744
24745#if defined(FEAT_VIMINFO) || defined(PROTO)
24746/*
24747 * Restore global vars that start with a capital from the viminfo file
24748 */
24749 int
24750read_viminfo_varlist(virp, writing)
24751 vir_T *virp;
24752 int writing;
24753{
24754 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024755 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024756 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757
24758 if (!writing && (find_viminfo_parameter('!') != NULL))
24759 {
24760 tab = vim_strchr(virp->vir_line + 1, '\t');
24761 if (tab != NULL)
24762 {
24763 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024764 switch (*tab)
24765 {
24766 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024767#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024768 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024769#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024770 case 'D': type = VAR_DICT; break;
24771 case 'L': type = VAR_LIST; break;
24772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773
24774 tab = vim_strchr(tab, '\t');
24775 if (tab != NULL)
24776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024777 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024778 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024779 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024781#ifdef FEAT_FLOAT
24782 else if (type == VAR_FLOAT)
24783 (void)string2float(tab + 1, &tv.vval.v_float);
24784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024786 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024787 if (type == VAR_DICT || type == VAR_LIST)
24788 {
24789 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24790
24791 if (etv == NULL)
24792 /* Failed to parse back the dict or list, use it as a
24793 * string. */
24794 tv.v_type = VAR_STRING;
24795 else
24796 {
24797 vim_free(tv.vval.v_string);
24798 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024799 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024800 }
24801 }
24802
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024803 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024804
24805 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024806 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024807 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24808 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 }
24810 }
24811 }
24812
24813 return viminfo_readline(virp);
24814}
24815
24816/*
24817 * Write global vars that start with a capital to the viminfo file
24818 */
24819 void
24820write_viminfo_varlist(fp)
24821 FILE *fp;
24822{
Bram Moolenaar33570922005-01-25 22:26:29 +000024823 hashitem_T *hi;
24824 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024825 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024826 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024827 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024828 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024829 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830
24831 if (find_viminfo_parameter('!') == NULL)
24832 return;
24833
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024834 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024835
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024836 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024837 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024839 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024841 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024842 this_var = HI2DI(hi);
24843 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024844 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024846 {
24847 case VAR_STRING: s = "STR"; break;
24848 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024849#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024850 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024851#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024852 case VAR_DICT: s = "DIC"; break;
24853 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024854 default: continue;
24855 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024856 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024857 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024858 if (p != NULL)
24859 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024860 vim_free(tofree);
24861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 }
24863 }
24864}
24865#endif
24866
24867#if defined(FEAT_SESSION) || defined(PROTO)
24868 int
24869store_session_globals(fd)
24870 FILE *fd;
24871{
Bram Moolenaar33570922005-01-25 22:26:29 +000024872 hashitem_T *hi;
24873 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024874 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 char_u *p, *t;
24876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024877 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024880 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024882 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024883 this_var = HI2DI(hi);
24884 if ((this_var->di_tv.v_type == VAR_NUMBER
24885 || this_var->di_tv.v_type == VAR_STRING)
24886 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024887 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024888 /* Escape special characters with a backslash. Turn a LF and
24889 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024890 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024891 (char_u *)"\\\"\n\r");
24892 if (p == NULL) /* out of memory */
24893 break;
24894 for (t = p; *t != NUL; ++t)
24895 if (*t == '\n')
24896 *t = 'n';
24897 else if (*t == '\r')
24898 *t = 'r';
24899 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024900 this_var->di_key,
24901 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24902 : ' ',
24903 p,
24904 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24905 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024906 || put_eol(fd) == FAIL)
24907 {
24908 vim_free(p);
24909 return FAIL;
24910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 vim_free(p);
24912 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024913#ifdef FEAT_FLOAT
24914 else if (this_var->di_tv.v_type == VAR_FLOAT
24915 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24916 {
24917 float_T f = this_var->di_tv.vval.v_float;
24918 int sign = ' ';
24919
24920 if (f < 0)
24921 {
24922 f = -f;
24923 sign = '-';
24924 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024925 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024926 this_var->di_key, sign, f) < 0)
24927 || put_eol(fd) == FAIL)
24928 return FAIL;
24929 }
24930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 }
24932 }
24933 return OK;
24934}
24935#endif
24936
Bram Moolenaar661b1822005-07-28 22:36:45 +000024937/*
24938 * Display script name where an item was last set.
24939 * Should only be invoked when 'verbose' is non-zero.
24940 */
24941 void
24942last_set_msg(scriptID)
24943 scid_T scriptID;
24944{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024945 char_u *p;
24946
Bram Moolenaar661b1822005-07-28 22:36:45 +000024947 if (scriptID != 0)
24948 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024949 p = home_replace_save(NULL, get_scriptname(scriptID));
24950 if (p != NULL)
24951 {
24952 verbose_enter();
24953 MSG_PUTS(_("\n\tLast set from "));
24954 MSG_PUTS(p);
24955 vim_free(p);
24956 verbose_leave();
24957 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024958 }
24959}
24960
Bram Moolenaard812df62008-11-09 12:46:09 +000024961/*
24962 * List v:oldfiles in a nice way.
24963 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024964 void
24965ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024966 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024967{
24968 list_T *l = vimvars[VV_OLDFILES].vv_list;
24969 listitem_T *li;
24970 int nr = 0;
24971
24972 if (l == NULL)
24973 msg((char_u *)_("No old files"));
24974 else
24975 {
24976 msg_start();
24977 msg_scroll = TRUE;
24978 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24979 {
24980 msg_outnum((long)++nr);
24981 MSG_PUTS(": ");
24982 msg_outtrans(get_tv_string(&li->li_tv));
24983 msg_putchar('\n');
24984 out_flush(); /* output one line at a time */
24985 ui_breakcheck();
24986 }
24987 /* Assume "got_int" was set to truncate the listing. */
24988 got_int = FALSE;
24989
24990#ifdef FEAT_BROWSE_CMD
24991 if (cmdmod.browse)
24992 {
24993 quit_more = FALSE;
24994 nr = prompt_for_number(FALSE);
24995 msg_starthere();
24996 if (nr > 0)
24997 {
24998 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24999 (long)nr);
25000
25001 if (p != NULL)
25002 {
25003 p = expand_env_save(p);
25004 eap->arg = p;
25005 eap->cmdidx = CMD_edit;
25006 cmdmod.browse = FALSE;
25007 do_exedit(eap, NULL);
25008 vim_free(p);
25009 }
25010 }
25011 }
25012#endif
25013 }
25014}
25015
Bram Moolenaar53744302015-07-17 17:38:22 +020025016/* reset v:option_new, v:option_old and v:option_type */
25017 void
25018reset_v_option_vars()
25019{
25020 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25021 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25022 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25023}
25024
25025
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026#endif /* FEAT_EVAL */
25027
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025029#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030
25031#ifdef WIN3264
25032/*
25033 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25034 */
25035static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25036static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25037static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25038
25039/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025040 * Get the short path (8.3) for the filename in "fnamep".
25041 * Only works for a valid file name.
25042 * When the path gets longer "fnamep" is changed and the allocated buffer
25043 * is put in "bufp".
25044 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25045 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 */
25047 static int
25048get_short_pathname(fnamep, bufp, fnamelen)
25049 char_u **fnamep;
25050 char_u **bufp;
25051 int *fnamelen;
25052{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025053 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 char_u *newbuf;
25055
25056 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057 l = GetShortPathName(*fnamep, *fnamep, len);
25058 if (l > len - 1)
25059 {
25060 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025061 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 newbuf = vim_strnsave(*fnamep, l);
25063 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065
25066 vim_free(*bufp);
25067 *fnamep = *bufp = newbuf;
25068
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025069 /* Really should always succeed, as the buffer is big enough. */
25070 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 }
25072
25073 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025074 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075}
25076
25077/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025078 * Get the short path (8.3) for the filename in "fname". The converted
25079 * path is returned in "bufp".
25080 *
25081 * Some of the directories specified in "fname" may not exist. This function
25082 * will shorten the existing directories at the beginning of the path and then
25083 * append the remaining non-existing path.
25084 *
25085 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025086 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025087 * bufp - Pointer to an allocated buffer for the filename.
25088 * fnamelen - Length of the filename pointed to by fname
25089 *
25090 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 */
25092 static int
25093shortpath_for_invalid_fname(fname, bufp, fnamelen)
25094 char_u **fname;
25095 char_u **bufp;
25096 int *fnamelen;
25097{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025098 char_u *short_fname, *save_fname, *pbuf_unused;
25099 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025101 int old_len, len;
25102 int new_len, sfx_len;
25103 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104
25105 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025106 old_len = *fnamelen;
25107 save_fname = vim_strnsave(*fname, old_len);
25108 pbuf_unused = NULL;
25109 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025111 endp = save_fname + old_len - 1; /* Find the end of the copy */
25112 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025114 /*
25115 * Try shortening the supplied path till it succeeds by removing one
25116 * directory at a time from the tail of the path.
25117 */
25118 len = 0;
25119 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025121 /* go back one path-separator */
25122 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25123 --endp;
25124 if (endp <= save_fname)
25125 break; /* processed the complete path */
25126
25127 /*
25128 * Replace the path separator with a NUL and try to shorten the
25129 * resulting path.
25130 */
25131 ch = *endp;
25132 *endp = 0;
25133 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025134 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025135 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25136 {
25137 retval = FAIL;
25138 goto theend;
25139 }
25140 *endp = ch; /* preserve the string */
25141
25142 if (len > 0)
25143 break; /* successfully shortened the path */
25144
25145 /* failed to shorten the path. Skip the path separator */
25146 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147 }
25148
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025149 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025150 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025151 /*
25152 * Succeeded in shortening the path. Now concatenate the shortened
25153 * path with the remaining path at the tail.
25154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025156 /* Compute the length of the new path. */
25157 sfx_len = (int)(save_endp - endp) + 1;
25158 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025160 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025162 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025164 /* There is not enough space in the currently allocated string,
25165 * copy it to a buffer big enough. */
25166 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025168 {
25169 retval = FAIL;
25170 goto theend;
25171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 }
25173 else
25174 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025175 /* Transfer short_fname to the main buffer (it's big enough),
25176 * unless get_short_pathname() did its work in-place. */
25177 *fname = *bufp = save_fname;
25178 if (short_fname != save_fname)
25179 vim_strncpy(save_fname, short_fname, len);
25180 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182
25183 /* concat the not-shortened part of the path */
25184 vim_strncpy(*fname + len, endp, sfx_len);
25185 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025187
25188theend:
25189 vim_free(pbuf_unused);
25190 vim_free(save_fname);
25191
25192 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193}
25194
25195/*
25196 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025197 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 */
25199 static int
25200shortpath_for_partial(fnamep, bufp, fnamelen)
25201 char_u **fnamep;
25202 char_u **bufp;
25203 int *fnamelen;
25204{
25205 int sepcount, len, tflen;
25206 char_u *p;
25207 char_u *pbuf, *tfname;
25208 int hasTilde;
25209
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025210 /* Count up the path separators from the RHS.. so we know which part
25211 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025213 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 if (vim_ispathsep(*p))
25215 ++sepcount;
25216
25217 /* Need full path first (use expand_env() to remove a "~/") */
25218 hasTilde = (**fnamep == '~');
25219 if (hasTilde)
25220 pbuf = tfname = expand_env_save(*fnamep);
25221 else
25222 pbuf = tfname = FullName_save(*fnamep, FALSE);
25223
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025224 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025226 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228
25229 if (len == 0)
25230 {
25231 /* Don't have a valid filename, so shorten the rest of the
25232 * path if we can. This CAN give us invalid 8.3 filenames, but
25233 * there's not a lot of point in guessing what it might be.
25234 */
25235 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025236 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 }
25239
25240 /* Count the paths backward to find the beginning of the desired string. */
25241 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025242 {
25243#ifdef FEAT_MBYTE
25244 if (has_mbyte)
25245 p -= mb_head_off(tfname, p);
25246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 if (vim_ispathsep(*p))
25248 {
25249 if (sepcount == 0 || (hasTilde && sepcount == 1))
25250 break;
25251 else
25252 sepcount --;
25253 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 if (hasTilde)
25256 {
25257 --p;
25258 if (p >= tfname)
25259 *p = '~';
25260 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 }
25263 else
25264 ++p;
25265
25266 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25267 vim_free(*bufp);
25268 *fnamelen = (int)STRLEN(p);
25269 *bufp = pbuf;
25270 *fnamep = p;
25271
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025272 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273}
25274#endif /* WIN3264 */
25275
25276/*
25277 * Adjust a filename, according to a string of modifiers.
25278 * *fnamep must be NUL terminated when called. When returning, the length is
25279 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025280 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 * When there is an error, *fnamep is set to NULL.
25282 */
25283 int
25284modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25285 char_u *src; /* string with modifiers */
25286 int *usedlen; /* characters after src that are used */
25287 char_u **fnamep; /* file name so far */
25288 char_u **bufp; /* buffer for allocated file name or NULL */
25289 int *fnamelen; /* length of fnamep */
25290{
25291 int valid = 0;
25292 char_u *tail;
25293 char_u *s, *p, *pbuf;
25294 char_u dirname[MAXPATHL];
25295 int c;
25296 int has_fullname = 0;
25297#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025298 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 int has_shortname = 0;
25300#endif
25301
25302repeat:
25303 /* ":p" - full path/file_name */
25304 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25305 {
25306 has_fullname = 1;
25307
25308 valid |= VALID_PATH;
25309 *usedlen += 2;
25310
25311 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25312 if ((*fnamep)[0] == '~'
25313#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25314 && ((*fnamep)[1] == '/'
25315# ifdef BACKSLASH_IN_FILENAME
25316 || (*fnamep)[1] == '\\'
25317# endif
25318 || (*fnamep)[1] == NUL)
25319
25320#endif
25321 )
25322 {
25323 *fnamep = expand_env_save(*fnamep);
25324 vim_free(*bufp); /* free any allocated file name */
25325 *bufp = *fnamep;
25326 if (*fnamep == NULL)
25327 return -1;
25328 }
25329
25330 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025331 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 {
25333 if (vim_ispathsep(*p)
25334 && p[1] == '.'
25335 && (p[2] == NUL
25336 || vim_ispathsep(p[2])
25337 || (p[2] == '.'
25338 && (p[3] == NUL || vim_ispathsep(p[3])))))
25339 break;
25340 }
25341
25342 /* FullName_save() is slow, don't use it when not needed. */
25343 if (*p != NUL || !vim_isAbsName(*fnamep))
25344 {
25345 *fnamep = FullName_save(*fnamep, *p != NUL);
25346 vim_free(*bufp); /* free any allocated file name */
25347 *bufp = *fnamep;
25348 if (*fnamep == NULL)
25349 return -1;
25350 }
25351
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025352#ifdef WIN3264
25353# if _WIN32_WINNT >= 0x0500
25354 if (vim_strchr(*fnamep, '~') != NULL)
25355 {
25356 /* Expand 8.3 filename to full path. Needed to make sure the same
25357 * file does not have two different names.
25358 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25359 p = alloc(_MAX_PATH + 1);
25360 if (p != NULL)
25361 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025362 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025363 {
25364 vim_free(*bufp);
25365 *bufp = *fnamep = p;
25366 }
25367 else
25368 vim_free(p);
25369 }
25370 }
25371# endif
25372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 /* Append a path separator to a directory. */
25374 if (mch_isdir(*fnamep))
25375 {
25376 /* Make room for one or two extra characters. */
25377 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25378 vim_free(*bufp); /* free any allocated file name */
25379 *bufp = *fnamep;
25380 if (*fnamep == NULL)
25381 return -1;
25382 add_pathsep(*fnamep);
25383 }
25384 }
25385
25386 /* ":." - path relative to the current directory */
25387 /* ":~" - path relative to the home directory */
25388 /* ":8" - shortname path - postponed till after */
25389 while (src[*usedlen] == ':'
25390 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25391 {
25392 *usedlen += 2;
25393 if (c == '8')
25394 {
25395#ifdef WIN3264
25396 has_shortname = 1; /* Postpone this. */
25397#endif
25398 continue;
25399 }
25400 pbuf = NULL;
25401 /* Need full path first (use expand_env() to remove a "~/") */
25402 if (!has_fullname)
25403 {
25404 if (c == '.' && **fnamep == '~')
25405 p = pbuf = expand_env_save(*fnamep);
25406 else
25407 p = pbuf = FullName_save(*fnamep, FALSE);
25408 }
25409 else
25410 p = *fnamep;
25411
25412 has_fullname = 0;
25413
25414 if (p != NULL)
25415 {
25416 if (c == '.')
25417 {
25418 mch_dirname(dirname, MAXPATHL);
25419 s = shorten_fname(p, dirname);
25420 if (s != NULL)
25421 {
25422 *fnamep = s;
25423 if (pbuf != NULL)
25424 {
25425 vim_free(*bufp); /* free any allocated file name */
25426 *bufp = pbuf;
25427 pbuf = NULL;
25428 }
25429 }
25430 }
25431 else
25432 {
25433 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25434 /* Only replace it when it starts with '~' */
25435 if (*dirname == '~')
25436 {
25437 s = vim_strsave(dirname);
25438 if (s != NULL)
25439 {
25440 *fnamep = s;
25441 vim_free(*bufp);
25442 *bufp = s;
25443 }
25444 }
25445 }
25446 vim_free(pbuf);
25447 }
25448 }
25449
25450 tail = gettail(*fnamep);
25451 *fnamelen = (int)STRLEN(*fnamep);
25452
25453 /* ":h" - head, remove "/file_name", can be repeated */
25454 /* Don't remove the first "/" or "c:\" */
25455 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25456 {
25457 valid |= VALID_HEAD;
25458 *usedlen += 2;
25459 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025460 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025461 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 *fnamelen = (int)(tail - *fnamep);
25463#ifdef VMS
25464 if (*fnamelen > 0)
25465 *fnamelen += 1; /* the path separator is part of the path */
25466#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025467 if (*fnamelen == 0)
25468 {
25469 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25470 p = vim_strsave((char_u *)".");
25471 if (p == NULL)
25472 return -1;
25473 vim_free(*bufp);
25474 *bufp = *fnamep = tail = p;
25475 *fnamelen = 1;
25476 }
25477 else
25478 {
25479 while (tail > s && !after_pathsep(s, tail))
25480 mb_ptr_back(*fnamep, tail);
25481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483
25484 /* ":8" - shortname */
25485 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25486 {
25487 *usedlen += 2;
25488#ifdef WIN3264
25489 has_shortname = 1;
25490#endif
25491 }
25492
25493#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025494 /*
25495 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 */
25497 if (has_shortname)
25498 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025499 /* Copy the string if it is shortened by :h and when it wasn't copied
25500 * yet, because we are going to change it in place. Avoids changing
25501 * the buffer name for "%:8". */
25502 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 {
25504 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025505 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 return -1;
25507 vim_free(*bufp);
25508 *bufp = *fnamep = p;
25509 }
25510
25511 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025512 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 if (!has_fullname && !vim_isAbsName(*fnamep))
25514 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025515 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 return -1;
25517 }
25518 else
25519 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025520 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521
Bram Moolenaardc935552011-08-17 15:23:23 +020025522 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025524 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 return -1;
25526
25527 if (l == 0)
25528 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025529 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025531 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 return -1;
25533 }
25534 *fnamelen = l;
25535 }
25536 }
25537#endif /* WIN3264 */
25538
25539 /* ":t" - tail, just the basename */
25540 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25541 {
25542 *usedlen += 2;
25543 *fnamelen -= (int)(tail - *fnamep);
25544 *fnamep = tail;
25545 }
25546
25547 /* ":e" - extension, can be repeated */
25548 /* ":r" - root, without extension, can be repeated */
25549 while (src[*usedlen] == ':'
25550 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25551 {
25552 /* find a '.' in the tail:
25553 * - for second :e: before the current fname
25554 * - otherwise: The last '.'
25555 */
25556 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25557 s = *fnamep - 2;
25558 else
25559 s = *fnamep + *fnamelen - 1;
25560 for ( ; s > tail; --s)
25561 if (s[0] == '.')
25562 break;
25563 if (src[*usedlen + 1] == 'e') /* :e */
25564 {
25565 if (s > tail)
25566 {
25567 *fnamelen += (int)(*fnamep - (s + 1));
25568 *fnamep = s + 1;
25569#ifdef VMS
25570 /* cut version from the extension */
25571 s = *fnamep + *fnamelen - 1;
25572 for ( ; s > *fnamep; --s)
25573 if (s[0] == ';')
25574 break;
25575 if (s > *fnamep)
25576 *fnamelen = s - *fnamep;
25577#endif
25578 }
25579 else if (*fnamep <= tail)
25580 *fnamelen = 0;
25581 }
25582 else /* :r */
25583 {
25584 if (s > tail) /* remove one extension */
25585 *fnamelen = (int)(s - *fnamep);
25586 }
25587 *usedlen += 2;
25588 }
25589
25590 /* ":s?pat?foo?" - substitute */
25591 /* ":gs?pat?foo?" - global substitute */
25592 if (src[*usedlen] == ':'
25593 && (src[*usedlen + 1] == 's'
25594 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25595 {
25596 char_u *str;
25597 char_u *pat;
25598 char_u *sub;
25599 int sep;
25600 char_u *flags;
25601 int didit = FALSE;
25602
25603 flags = (char_u *)"";
25604 s = src + *usedlen + 2;
25605 if (src[*usedlen + 1] == 'g')
25606 {
25607 flags = (char_u *)"g";
25608 ++s;
25609 }
25610
25611 sep = *s++;
25612 if (sep)
25613 {
25614 /* find end of pattern */
25615 p = vim_strchr(s, sep);
25616 if (p != NULL)
25617 {
25618 pat = vim_strnsave(s, (int)(p - s));
25619 if (pat != NULL)
25620 {
25621 s = p + 1;
25622 /* find end of substitution */
25623 p = vim_strchr(s, sep);
25624 if (p != NULL)
25625 {
25626 sub = vim_strnsave(s, (int)(p - s));
25627 str = vim_strnsave(*fnamep, *fnamelen);
25628 if (sub != NULL && str != NULL)
25629 {
25630 *usedlen = (int)(p + 1 - src);
25631 s = do_string_sub(str, pat, sub, flags);
25632 if (s != NULL)
25633 {
25634 *fnamep = s;
25635 *fnamelen = (int)STRLEN(s);
25636 vim_free(*bufp);
25637 *bufp = s;
25638 didit = TRUE;
25639 }
25640 }
25641 vim_free(sub);
25642 vim_free(str);
25643 }
25644 vim_free(pat);
25645 }
25646 }
25647 /* after using ":s", repeat all the modifiers */
25648 if (didit)
25649 goto repeat;
25650 }
25651 }
25652
Bram Moolenaar26df0922014-02-23 23:39:13 +010025653 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25654 {
25655 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25656 if (p == NULL)
25657 return -1;
25658 vim_free(*bufp);
25659 *bufp = *fnamep = p;
25660 *fnamelen = (int)STRLEN(p);
25661 *usedlen += 2;
25662 }
25663
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 return valid;
25665}
25666
25667/*
25668 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25669 * "flags" can be "g" to do a global substitute.
25670 * Returns an allocated string, NULL for error.
25671 */
25672 char_u *
25673do_string_sub(str, pat, sub, flags)
25674 char_u *str;
25675 char_u *pat;
25676 char_u *sub;
25677 char_u *flags;
25678{
25679 int sublen;
25680 regmatch_T regmatch;
25681 int i;
25682 int do_all;
25683 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025684 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 garray_T ga;
25686 char_u *ret;
25687 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025688 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689
25690 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25691 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025692 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693
25694 ga_init2(&ga, 1, 200);
25695
25696 do_all = (flags[0] == 'g');
25697
25698 regmatch.rm_ic = p_ic;
25699 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25700 if (regmatch.regprog != NULL)
25701 {
25702 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025703 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25705 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025706 /* Skip empty match except for first match. */
25707 if (regmatch.startp[0] == regmatch.endp[0])
25708 {
25709 if (zero_width == regmatch.startp[0])
25710 {
25711 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025712 i = MB_PTR2LEN(tail);
25713 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25714 (size_t)i);
25715 ga.ga_len += i;
25716 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025717 continue;
25718 }
25719 zero_width = regmatch.startp[0];
25720 }
25721
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722 /*
25723 * Get some space for a temporary buffer to do the substitution
25724 * into. It will contain:
25725 * - The text up to where the match is.
25726 * - The substituted text.
25727 * - The text after the match.
25728 */
25729 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025730 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25732 {
25733 ga_clear(&ga);
25734 break;
25735 }
25736
25737 /* copy the text up to where the match is */
25738 i = (int)(regmatch.startp[0] - tail);
25739 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25740 /* add the substituted text */
25741 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25742 + ga.ga_len + i, TRUE, TRUE, FALSE);
25743 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025744 tail = regmatch.endp[0];
25745 if (*tail == NUL)
25746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 if (!do_all)
25748 break;
25749 }
25750
25751 if (ga.ga_data != NULL)
25752 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25753
Bram Moolenaar473de612013-06-08 18:19:48 +020025754 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 }
25756
25757 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25758 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025759 if (p_cpo == empty_option)
25760 p_cpo = save_cpo;
25761 else
25762 /* Darn, evaluating {sub} expression changed the value. */
25763 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764
25765 return ret;
25766}
25767
25768#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */