blob: 5eddf2368e7ec84c37a1a67c69c826cf675da682 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100478static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100479static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100497static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200531static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533#ifdef FEAT_FLOAT
534static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
535#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000538static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#ifdef FEAT_FLOAT
545static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000548#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000549static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000558static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000560static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200564static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200568static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000576static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000577static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200578static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000579static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000580static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200583static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000584static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100590static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000593static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000607static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100612static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000614static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200627static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200630#ifdef FEAT_LUA
631static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000637static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200638static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000639static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000640static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000646#ifdef vim_mkdir
647static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100650#ifdef FEAT_MZSCHEME
651static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100655static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000656static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000661static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000662static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200663#ifdef FEAT_PYTHON3
664static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
666#ifdef FEAT_PYTHON
667static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000671static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
684static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
685#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200686static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100688static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000691static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000693static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200698static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000701static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000702static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000703static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000704static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200706static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000707static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100709#ifdef FEAT_CRYPT
710static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
711#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000712static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200713static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000715#ifdef FEAT_FLOAT
716static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200717static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000720static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#ifdef FEAT_FLOAT
725static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
727#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000728static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200729static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730#ifdef HAVE_STRFTIME
731static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
733static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200739static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200740static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000746static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200747static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200749static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000751static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000752static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000753static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000754static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000756static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200757#ifdef FEAT_FLOAT
758static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
765static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
766#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200768static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200769static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100770static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100774static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000781static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000784static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100785static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100786static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787
Bram Moolenaar493c1782014-05-28 14:34:46 +0200788static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000789static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static int get_env_len __ARGS((char_u **arg));
791static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000792static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000793static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
794#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
795#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
796 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000797static 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 +0000798static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200800static 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 +0000801static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static typval_T *alloc_tv __ARGS((void));
803static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void init_tv __ARGS((typval_T *varp));
805static long get_tv_number __ARGS((typval_T *varp));
806static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000807static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static char_u *get_tv_string __ARGS((typval_T *varp));
809static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000810static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
812static 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 +0000813static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
814static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
815static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000816static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
817static 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 +0000818static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200819static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
820static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200821static int var_check_func_name __ARGS((char_u *name, int new_var));
822static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200823static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000824static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
826static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
827static int eval_fname_script __ARGS((char_u *p));
828static int eval_fname_sid __ARGS((char_u *p));
829static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static ufunc_T *find_func __ARGS((char_u *name));
831static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200832static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#ifdef FEAT_PROFILE
834static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000835static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
836static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
837static int
838# ifdef __BORLANDC__
839 _RTLENTRYF
840# endif
841 prof_total_cmp __ARGS((const void *s1, const void *s2));
842static int
843# ifdef __BORLANDC__
844 _RTLENTRYF
845# endif
846 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200848static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000850static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000851static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static 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 +0000853static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
854static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000855static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000856static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
857static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000858static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000859static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000860static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200861static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200862static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200864
865#ifdef EBCDIC
866static int compare_func_name __ARGS((const void *s1, const void *s2));
867static void sortFunctions __ARGS(());
868#endif
869
Bram Moolenaar33570922005-01-25 22:26:29 +0000870/*
871 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000872 */
873 void
874eval_init()
875{
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 int i;
877 struct vimvar *p;
878
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200879 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
880 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200881 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000882 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000884
885 for (i = 0; i < VV_LEN; ++i)
886 {
887 p = &vimvars[i];
888 STRCPY(p->vv_di.di_key, p->vv_name);
889 if (p->vv_flags & VV_RO)
890 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
891 else if (p->vv_flags & VV_RO_SBX)
892 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
893 else
894 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000895
896 /* add to v: scope dict, unless the value is not always available */
897 if (p->vv_type != VAR_UNKNOWN)
898 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000900 /* add to compat scope dict */
901 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000902 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000903 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100904 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200905 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100906 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200907 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908
909#ifdef EBCDIC
910 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100911 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912 */
913 sortFunctions();
914#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000915}
916
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917#if defined(EXITFREE) || defined(PROTO)
918 void
919eval_clear()
920{
921 int i;
922 struct vimvar *p;
923
924 for (i = 0; i < VV_LEN; ++i)
925 {
926 p = &vimvars[i];
927 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000929 vim_free(p->vv_str);
930 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000931 }
932 else if (p->vv_di.di_tv.v_type == VAR_LIST)
933 {
934 list_unref(p->vv_list);
935 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000936 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937 }
938 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000939 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000940 hash_clear(&compat_hashtab);
941
Bram Moolenaard9fba312005-06-26 22:34:35 +0000942 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100943# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200944 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100945# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000946
947 /* global variables */
948 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000949
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000950 /* autoloaded script names */
951 ga_clear_strings(&ga_loaded);
952
Bram Moolenaarcca74132013-09-25 21:00:28 +0200953 /* Script-local variables. First clear all the variables and in a second
954 * loop free the scriptvar_T, because a variable in one script might hold
955 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200958 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200959 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200960 ga_clear(&ga_scripts);
961
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000962 /* unreferenced lists and dicts */
963 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000964
965 /* functions */
966 free_all_functions();
967 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968}
969#endif
970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 * Return the name of the executed function.
973 */
974 char_u *
975func_name(cookie)
976 void *cookie;
977{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000978 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Return the address holding the next breakpoint line for a funccall cookie.
983 */
984 linenr_T *
985func_breakpoint(cookie)
986 void *cookie;
987{
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the address holding the debug tick for a funccall cookie.
993 */
994 int *
995func_dbg_tick(cookie)
996 void *cookie;
997{
Bram Moolenaar33570922005-01-25 22:26:29 +0000998 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Return the nesting level for a funccall cookie.
1003 */
1004 int
1005func_level(cookie)
1006 void *cookie;
1007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001012funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001014/* pointer to list of previously used funccal, still around because some
1015 * item in it is still being used. */
1016funccall_T *previous_funccal = NULL;
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018/*
1019 * Return TRUE when a function was ended by a ":return" command.
1020 */
1021 int
1022current_func_returned()
1023{
1024 return current_funccal->returned;
1025}
1026
1027
1028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * Set an internal variable to a string value. Creates the variable if it does
1030 * not already exist.
1031 */
1032 void
1033set_internal_string_var(name, value)
1034 char_u *name;
1035 char_u *value;
1036{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001037 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 val = vim_strsave(value);
1041 if (val != NULL)
1042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001044 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001046 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 }
1050}
1051
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054static char_u *redir_endp = NULL;
1055static char_u *redir_varname = NULL;
1056
1057/*
1058 * Start recording command output to a variable
1059 * Returns OK if successfully completed the setup. FAIL otherwise.
1060 */
1061 int
1062var_redir_start(name, append)
1063 char_u *name;
1064 int append; /* append to an existing variable */
1065{
1066 int save_emsg;
1067 int err;
1068 typval_T tv;
1069
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 {
1073 EMSG(_(e_invarg));
1074 return FAIL;
1075 }
1076
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 redir_varname = vim_strsave(name);
1079 if (redir_varname == NULL)
1080 return FAIL;
1081
1082 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1083 if (redir_lval == NULL)
1084 {
1085 var_redir_stop();
1086 return FAIL;
1087 }
1088
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089 /* The output is stored in growarray "redir_ga" until redirection ends. */
1090 ga_init2(&redir_ga, (int)sizeof(char), 500);
1091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001093 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001094 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1096 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001097 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (redir_endp != NULL && *redir_endp != NUL)
1099 /* Trailing characters are present after the variable name */
1100 EMSG(_(e_trailing));
1101 else
1102 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001103 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 var_redir_stop();
1105 return FAIL;
1106 }
1107
1108 /* check if we can write to the variable: set it to or append an empty
1109 * string */
1110 save_emsg = did_emsg;
1111 did_emsg = FALSE;
1112 tv.v_type = VAR_STRING;
1113 tv.vval.v_string = (char_u *)"";
1114 if (append)
1115 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1116 else
1117 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001118 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001120 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 if (err)
1122 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 var_redir_stop();
1125 return FAIL;
1126 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127
1128 return OK;
1129}
1130
1131/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 * Append "value[value_len]" to the variable set by var_redir_start().
1133 * The actual appending is postponed until redirection ends, because the value
1134 * appended may in fact be the string we write to, changing it may cause freed
1135 * memory to be used:
1136 * :redir => foo
1137 * :let foo
1138 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 */
1140 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 if (redir_lval == NULL)
1148 return;
1149
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 {
1157 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159 }
1160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162}
1163
1164/*
1165 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
1169var_redir_stop()
1170{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 typval_T tv;
1172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_lval != NULL)
1174 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 /* If there was no error: assign the text to the variable. */
1176 if (redir_endp != NULL)
1177 {
1178 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1179 tv.v_type = VAR_STRING;
1180 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 /* Call get_lval() again, if it's inside a Dict or List it may
1182 * have changed. */
1183 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001184 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1186 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1187 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001190 /* free the collected output */
1191 vim_free(redir_ga.ga_data);
1192 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 vim_free(redir_lval);
1195 redir_lval = NULL;
1196 }
1197 vim_free(redir_varname);
1198 redir_varname = NULL;
1199}
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201# if defined(FEAT_MBYTE) || defined(PROTO)
1202 int
1203eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1204 char_u *enc_from;
1205 char_u *enc_to;
1206 char_u *fname_from;
1207 char_u *fname_to;
1208{
1209 int err = FALSE;
1210
1211 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1212 set_vim_var_string(VV_CC_TO, enc_to, -1);
1213 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1214 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1215 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_CC_FROM, NULL, -1);
1218 set_vim_var_string(VV_CC_TO, NULL, -1);
1219 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221
1222 if (err)
1223 return FAIL;
1224 return OK;
1225}
1226# endif
1227
1228# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1229 int
1230eval_printexpr(fname, args)
1231 char_u *fname;
1232 char_u *args;
1233{
1234 int err = FALSE;
1235
1236 set_vim_var_string(VV_FNAME_IN, fname, -1);
1237 set_vim_var_string(VV_CMDARG, args, -1);
1238 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1239 err = TRUE;
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_CMDARG, NULL, -1);
1242
1243 if (err)
1244 {
1245 mch_remove(fname);
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250# endif
1251
1252# if defined(FEAT_DIFF) || defined(PROTO)
1253 void
1254eval_diff(origfile, newfile, outfile)
1255 char_u *origfile;
1256 char_u *newfile;
1257 char_u *outfile;
1258{
1259 int err = FALSE;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269
1270 void
1271eval_patch(origfile, difffile, outfile)
1272 char_u *origfile;
1273 char_u *difffile;
1274 char_u *outfile;
1275{
1276 int err;
1277
1278 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1280 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1281 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285}
1286# endif
1287
1288/*
1289 * Top level evaluation function, returning a boolean.
1290 * Sets "error" to TRUE if there was an error.
1291 * Return TRUE or FALSE.
1292 */
1293 int
1294eval_to_bool(arg, error, nextcmd, skip)
1295 char_u *arg;
1296 int *error;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int retval = FALSE;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else
1308 {
1309 *error = FALSE;
1310 if (!skip)
1311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
1323 * Top level evaluation function, returning a string. If "skip" is TRUE,
1324 * only parsing to "nextcmd" is done, without reporting errors. Return
1325 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1326 */
1327 char_u *
1328eval_to_string_skip(arg, nextcmd, skip)
1329 char_u *arg;
1330 char_u **nextcmd;
1331 int skip; /* only parse, don't execute */
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *retval;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 retval = NULL;
1340 else
1341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 retval = vim_strsave(get_tv_string(&tv));
1343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 if (skip)
1346 --emsg_skip;
1347
1348 return retval;
1349}
1350
1351/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352 * Skip over an expression at "*pp".
1353 * Return FAIL for an error, OK otherwise.
1354 */
1355 int
1356skip_expr(pp)
1357 char_u **pp;
1358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360
1361 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363}
1364
1365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 * When "convert" is TRUE convert a List into a sequence of lines and convert
1368 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Return pointer to allocated memory, or NULL for failure.
1370 */
1371 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *arg;
1374 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 retval = NULL;
1386 else
1387 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001388 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 {
1390 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001393 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001394 if (tv.vval.v_list->lv_len > 0)
1395 ga_append(&ga, NL);
1396 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 ga_append(&ga, NUL);
1398 retval = (char_u *)ga.ga_data;
1399 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001400#ifdef FEAT_FLOAT
1401 else if (convert && tv.v_type == VAR_FLOAT)
1402 {
1403 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1404 retval = vim_strsave(numbuf);
1405 }
1406#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 else
1408 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 * Call eval_to_string() without using current local variables and using
1417 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *arg;
1422 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 char_u *retval;
1426 void *save_funccalp;
1427
1428 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 ++sandbox;
1431 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001433 if (use_sandbox)
1434 --sandbox;
1435 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 restore_funccal(save_funccalp);
1437 return retval;
1438}
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Top level evaluation function, returning a number.
1442 * Evaluates "expr" silently.
1443 * Returns -1 for an error.
1444 */
1445 int
1446eval_to_number(expr)
1447 char_u *expr;
1448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001451 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 ++emsg_off;
1454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 retval = -1;
1457 else
1458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001459 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 --emsg_off;
1463
1464 return retval;
1465}
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467/*
1468 * Prepare v: variable "idx" to be used.
1469 * Save the current typeval in "save_tv".
1470 * When not used yet add the variable to the v: hashtable.
1471 */
1472 static void
1473prepare_vimvar(idx, save_tv)
1474 int idx;
1475 typval_T *save_tv;
1476{
1477 *save_tv = vimvars[idx].vv_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1480}
1481
1482/*
1483 * Restore v: variable "idx" to typeval "save_tv".
1484 * When no longer defined, remove the variable from the v: hashtable.
1485 */
1486 static void
1487restore_vimvar(idx, save_tv)
1488 int idx;
1489 typval_T *save_tv;
1490{
1491 hashitem_T *hi;
1492
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493 vimvars[idx].vv_tv = *save_tv;
1494 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1495 {
1496 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1497 if (HASHITEM_EMPTY(hi))
1498 EMSG2(_(e_intern2), "restore_vimvar()");
1499 else
1500 hash_remove(&vimvarht, hi);
1501 }
1502}
1503
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001504#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505/*
1506 * Evaluate an expression to a list with suggestions.
1507 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001508 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001509 */
1510 list_T *
1511eval_spell_expr(badword, expr)
1512 char_u *badword;
1513 char_u *expr;
1514{
1515 typval_T save_val;
1516 typval_T rettv;
1517 list_T *list = NULL;
1518 char_u *p = skipwhite(expr);
1519
1520 /* Set "v:val" to the bad word. */
1521 prepare_vimvar(VV_VAL, &save_val);
1522 vimvars[VV_VAL].vv_type = VAR_STRING;
1523 vimvars[VV_VAL].vv_str = badword;
1524 if (p_verbose == 0)
1525 ++emsg_off;
1526
1527 if (eval1(&p, &rettv, TRUE) == OK)
1528 {
1529 if (rettv.v_type != VAR_LIST)
1530 clear_tv(&rettv);
1531 else
1532 list = rettv.vval.v_list;
1533 }
1534
1535 if (p_verbose == 0)
1536 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537 restore_vimvar(VV_VAL, &save_val);
1538
1539 return list;
1540}
1541
1542/*
1543 * "list" is supposed to contain two items: a word and a number. Return the
1544 * word in "pp" and the number as the return value.
1545 * Return -1 if anything isn't right.
1546 * Used to get the good word and score from the eval_spell_expr() result.
1547 */
1548 int
1549get_spellword(list, pp)
1550 list_T *list;
1551 char_u **pp;
1552{
1553 listitem_T *li;
1554
1555 li = list->lv_first;
1556 if (li == NULL)
1557 return -1;
1558 *pp = get_tv_string(&li->li_tv);
1559
1560 li = li->li_next;
1561 if (li == NULL)
1562 return -1;
1563 return get_tv_number(&li->li_tv);
1564}
1565#endif
1566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001568 * Top level evaluation function.
1569 * Returns an allocated typval_T with the result.
1570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 */
1572 typval_T *
1573eval_expr(arg, nextcmd)
1574 char_u *arg;
1575 char_u **nextcmd;
1576{
1577 typval_T *tv;
1578
1579 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 {
1582 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 }
1585
1586 return tv;
1587}
1588
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 * Uses argv[argc] for the function arguments. Only Number and String
1593 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001596 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001597call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *func;
1599 int argc;
1600 char_u **argv;
1601 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001602 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long n;
1607 int len;
1608 int i;
1609 int doesrange;
1610 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001613 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 for (i = 0; i < argc; i++)
1618 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001619 /* Pass a NULL or empty argument as an empty string */
1620 if (argv[i] == NULL || *argv[i] == NUL)
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001624 continue;
1625 }
1626
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001627 if (str_arg_only)
1628 len = 0;
1629 else
1630 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001631 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (len != 0 && len == (int)STRLEN(argv[i]))
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_NUMBER;
1635 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 else
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643
1644 if (safe)
1645 {
1646 save_funccalp = save_funccal();
1647 ++sandbox;
1648 }
1649
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1651 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (safe)
1655 {
1656 --sandbox;
1657 restore_funccal(save_funccalp);
1658 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 vim_free(argvars);
1660
1661 if (ret == FAIL)
1662 clear_tv(rettv);
1663
1664 return ret;
1665}
1666
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001667/*
1668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
1682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
1690
1691#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1692 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1693
Bram Moolenaar4f688582007-07-24 12:34:30 +00001694# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001696 * Call vimL function "func" and return the result as a string.
1697 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 * Uses argv[argc] for the function arguments.
1699 */
1700 void *
1701call_func_retstr(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001710 /* All arguments are passed as strings, no conversion to number. */
1711 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 return NULL;
1713
1714 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return retval;
1717}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 */
1725 void *
1726call_func_retlist(func, argc, argv, safe)
1727 char_u *func;
1728 int argc;
1729 char_u **argv;
1730 int safe; /* use the sandbox */
1731{
1732 typval_T rettv;
1733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 if (rettv.v_type != VAR_LIST)
1739 {
1740 clear_tv(&rettv);
1741 return NULL;
1742 }
1743
1744 return rettv.vval.v_list;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747
1748/*
1749 * Save the current function call pointer, and set it to NULL.
1750 * Used when executing autocommands and for ":source".
1751 */
1752 void *
1753save_funccal()
1754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 current_funccal = NULL;
1758 return (void *)fc;
1759}
1760
1761 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762restore_funccal(vfc)
1763 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765 funccall_T *fc = (funccall_T *)vfc;
1766
1767 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770#if defined(FEAT_PROFILE) || defined(PROTO)
1771/*
1772 * Prepare profiling for entering a child or something else that is not
1773 * counted for the script/function itself.
1774 * Should always be called in pair with prof_child_exit().
1775 */
1776 void
1777prof_child_enter(tm)
1778 proftime_T *tm; /* place to store waittime */
1779{
1780 funccall_T *fc = current_funccal;
1781
1782 if (fc != NULL && fc->func->uf_profiling)
1783 profile_start(&fc->prof_child);
1784 script_prof_save(tm);
1785}
1786
1787/*
1788 * Take care of time spent in a child.
1789 * Should always be called after prof_child_enter().
1790 */
1791 void
1792prof_child_exit(tm)
1793 proftime_T *tm; /* where waittime was stored */
1794{
1795 funccall_T *fc = current_funccal;
1796
1797 if (fc != NULL && fc->func->uf_profiling)
1798 {
1799 profile_end(&fc->prof_child);
1800 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1801 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1802 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1803 }
1804 script_prof_restore(tm);
1805}
1806#endif
1807
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810/*
1811 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1812 * it in "*cp". Doesn't give error messages.
1813 */
1814 int
1815eval_foldexpr(arg, cp)
1816 char_u *arg;
1817 int *cp;
1818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int retval;
1821 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001822 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1823 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 ++sandbox;
1828 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 if (tv.v_type == VAR_NUMBER)
1836 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001837 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 retval = 0;
1839 else
1840 {
1841 /* If the result is a string, check if there is a non-digit before
1842 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (!VIM_ISDIGIT(*s) && *s != '-')
1845 *cp = *s++;
1846 retval = atol((char *)s);
1847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001851 if (use_sandbox)
1852 --sandbox;
1853 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 return retval;
1856}
1857#endif
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 * ":let" list all variable values
1861 * ":let var1 var2" list variable values
1862 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 * ":let var += expr" assignment command.
1864 * ":let var -= expr" assignment command.
1865 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 void
1869ex_let(eap)
1870 exarg_T *eap;
1871{
1872 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 int var_count = 0;
1877 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 argend = skip_var_list(arg, &var_count, &semicolon);
1883 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001885 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1886 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001887 expr = skipwhite(argend);
1888 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1889 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 /*
1892 * ":let" without "=": list variables
1893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 if (*arg == '[')
1895 EMSG(_(e_invarg));
1896 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_glob_vars(&first);
1903 list_buf_vars(&first);
1904 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001907#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 list_script_vars(&first);
1909 list_func_vars(&first);
1910 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 eap->nextcmd = check_nextcmd(arg);
1913 }
1914 else
1915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op[0] = '=';
1917 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1921 op[0] = *expr; /* +=, -= or .= */
1922 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 else
1925 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 {
1932 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --emsg_skip;
1935 }
1936 else if (i != FAIL)
1937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
1943}
1944
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945/*
1946 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1947 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 * When "nextchars" is not NULL it points to a string with characters that
1949 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1950 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 * Returns OK or FAIL;
1952 */
1953 static int
1954ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1955 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 int copy; /* copy values from "tv", don't move */
1958 int semicolon; /* from skip_var_list() */
1959 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961{
1962 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 listitem_T *item;
1966 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
1968 if (*arg != '[')
1969 {
1970 /*
1971 * ":let var = expr" or ":for var in list"
1972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 return OK;
1976 }
1977
1978 /*
1979 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1980 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001981 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 EMSG(_(e_listreq));
1984 return FAIL;
1985 }
1986
1987 i = list_len(l);
1988 if (semicolon == 0 && var_count < i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993 if (var_count - semicolon > i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998
1999 item = l->lv_first;
2000 while (*arg != ']')
2001 {
2002 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 item = item->li_next;
2005 if (arg == NULL)
2006 return FAIL;
2007
2008 arg = skipwhite(arg);
2009 if (*arg == ';')
2010 {
2011 /* Put the rest of the list (may be empty) in the var after ';'.
2012 * Create a new list for this. */
2013 l = list_alloc();
2014 if (l == NULL)
2015 return FAIL;
2016 while (item != NULL)
2017 {
2018 list_append_tv(l, &item->li_tv);
2019 item = item->li_next;
2020 }
2021
2022 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002023 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 ltv.vval.v_list = l;
2025 l->lv_refcount = 1;
2026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2028 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 clear_tv(&ltv);
2030 if (arg == NULL)
2031 return FAIL;
2032 break;
2033 }
2034 else if (*arg != ',' && *arg != ']')
2035 {
2036 EMSG2(_(e_intern2), "ex_let_vars()");
2037 return FAIL;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
2044/*
2045 * Skip over assignable variable "var" or list of variables "[var, var]".
2046 * Used for ":let varvar = expr" and ":for varvar in expr".
2047 * For "[var, var]" increment "*var_count" for each variable.
2048 * for "[var, var; var]" set "semicolon".
2049 * Return NULL for an error.
2050 */
2051 static char_u *
2052skip_var_list(arg, var_count, semicolon)
2053 char_u *arg;
2054 int *var_count;
2055 int *semicolon;
2056{
2057 char_u *p, *s;
2058
2059 if (*arg == '[')
2060 {
2061 /* "[var, var]": find the matching ']'. */
2062 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002063 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 {
2065 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2066 s = skip_var_one(p);
2067 if (s == p)
2068 {
2069 EMSG2(_(e_invarg2), p);
2070 return NULL;
2071 }
2072 ++*var_count;
2073
2074 p = skipwhite(s);
2075 if (*p == ']')
2076 break;
2077 else if (*p == ';')
2078 {
2079 if (*semicolon == 1)
2080 {
2081 EMSG(_("Double ; in list of variables"));
2082 return NULL;
2083 }
2084 *semicolon = 1;
2085 }
2086 else if (*p != ',')
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 }
2092 return p + 1;
2093 }
2094 else
2095 return skip_var_one(arg);
2096}
2097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002099 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 static char_u *
2103skip_var_one(arg)
2104 char_u *arg;
2105{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002106 if (*arg == '@' && arg[1] != NUL)
2107 return arg + 2;
2108 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2109 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110}
2111
Bram Moolenaara7043832005-01-21 11:56:39 +00002112/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 * List variables for hashtab "ht" with prefix "prefix".
2114 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 hashitem_T *hi;
2124 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 int todo;
2126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002127 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2129 {
2130 if (!HASHITEM_EMPTY(hi))
2131 {
2132 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 di = HI2DI(hi);
2134 if (empty || di->di_tv.v_type != VAR_STRING
2135 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 }
2138 }
2139}
2140
2141/*
2142 * List global variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_glob_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List buffer variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_buf_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 char_u numbuf[NUMBUFLEN];
2159
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162
2163 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2165 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List window variables.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_win_vars(first)
2173 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002175 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002177}
2178
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_WINDOWS
2180/*
2181 * List tab page variables.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_tab_vars(first)
2185 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189}
2190#endif
2191
Bram Moolenaara7043832005-01-21 11:56:39 +00002192/*
2193 * List Vim variables.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_vim_vars(first)
2197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200}
2201
2202/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203 * List script-local variables, if there is a script.
2204 */
2205 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206list_script_vars(first)
2207 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208{
2209 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2211 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212}
2213
2214/*
2215 * List function variables, if there is a function.
2216 */
2217 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218list_func_vars(first)
2219 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 exarg_T *eap;
2232 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234{
2235 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 char_u *name_start;
2239 char_u *arg_subsc;
2240 char_u *tofree;
2241 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242
2243 while (!ends_excmd(*arg) && !got_int)
2244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002247 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2249 {
2250 emsg_severe = TRUE;
2251 EMSG(_(e_trailing));
2252 break;
2253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* get_name_len() takes care of expanding curly braces */
2258 name_start = name = arg;
2259 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2260 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* This is mainly to keep test 49 working: when expanding
2263 * curly braces fails overrule the exception error message. */
2264 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 emsg_severe = TRUE;
2267 EMSG2(_(e_invarg2), arg);
2268 break;
2269 }
2270 error = TRUE;
2271 }
2272 else
2273 {
2274 if (tofree != NULL)
2275 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002276 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 else
2279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 /* handle d.key, l[idx], f(expr) */
2281 arg_subsc = arg;
2282 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 'g': list_glob_vars(first); break;
2291 case 'b': list_buf_vars(first); break;
2292 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'v': list_vim_vars(first); break;
2297 case 's': list_script_vars(first); break;
2298 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 default:
2300 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
2303 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 {
2305 char_u numbuf[NUMBUFLEN];
2306 char_u *tf;
2307 int c;
2308 char_u *s;
2309
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002310 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 c = *arg;
2312 *arg = NUL;
2313 list_one_var_a((char_u *)"",
2314 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002315 tv.v_type,
2316 s == NULL ? (char_u *)"" : s,
2317 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 *arg = c;
2319 vim_free(tf);
2320 }
2321 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
2324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328
2329 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331
2332 return arg;
2333}
2334
2335/*
2336 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2337 * Returns a pointer to the char just after the var name.
2338 * Returns NULL if there is an error.
2339 */
2340 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347{
2348 int c1;
2349 char_u *name;
2350 char_u *p;
2351 char_u *arg_end = NULL;
2352 int len;
2353 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355
2356 /*
2357 * ":let $VAR = expr": Set environment variable.
2358 */
2359 if (*arg == '$')
2360 {
2361 /* Find the end of the name. */
2362 ++arg;
2363 name = arg;
2364 len = get_env_len(&arg);
2365 if (len == 0)
2366 EMSG2(_(e_invarg2), name - 1);
2367 else
2368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 if (op != NULL && (*op == '+' || *op == '-'))
2370 EMSG2(_(e_letwrong), op);
2371 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002374 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 {
2376 c1 = name[len];
2377 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 p = get_tv_string_chk(tv);
2379 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 int mustfree = FALSE;
2382 char_u *s = vim_getenv(name, &mustfree);
2383
2384 if (s != NULL)
2385 {
2386 p = tofree = concat_str(s, p);
2387 if (mustfree)
2388 vim_free(s);
2389 }
2390 }
2391 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (STRICMP(name, "HOME") == 0)
2395 init_homedir();
2396 else if (didset_vim && STRICMP(name, "VIM") == 0)
2397 didset_vim = FALSE;
2398 else if (didset_vimruntime
2399 && STRICMP(name, "VIMRUNTIME") == 0)
2400 didset_vimruntime = FALSE;
2401 arg_end = arg;
2402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406 }
2407 }
2408
2409 /*
2410 * ":let &option = expr": Set option value.
2411 * ":let &l:option = expr": Set local option value.
2412 * ":let &g:option = expr": Set global option value.
2413 */
2414 else if (*arg == '&')
2415 {
2416 /* Find the end of the name. */
2417 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 if (p == NULL || (endchars != NULL
2419 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
2421 else
2422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 long n;
2424 int opt_type;
2425 long numval;
2426 char_u *stringval = NULL;
2427 char_u *s;
2428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 c1 = *p;
2430 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431
2432 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 s = get_tv_string_chk(tv); /* != NULL if number or string */
2434 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
2436 opt_type = get_option_value(arg, &numval,
2437 &stringval, opt_flags);
2438 if ((opt_type == 1 && *op == '.')
2439 || (opt_type == 0 && *op != '.'))
2440 EMSG2(_(e_letwrong), op);
2441 else
2442 {
2443 if (opt_type == 1) /* number */
2444 {
2445 if (*op == '+')
2446 n = numval + n;
2447 else
2448 n = numval - n;
2449 }
2450 else if (opt_type == 0 && stringval != NULL) /* string */
2451 {
2452 s = concat_str(stringval, s);
2453 vim_free(stringval);
2454 stringval = s;
2455 }
2456 }
2457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 if (s != NULL)
2459 {
2460 set_option_value(arg, n, s, opt_flags);
2461 arg_end = p;
2462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466 }
2467
2468 /*
2469 * ":let @r = expr": Set register contents.
2470 */
2471 else if (*arg == '@')
2472 {
2473 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 if (op != NULL && (*op == '+' || *op == '-'))
2475 EMSG2(_(e_letwrong), op);
2476 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 EMSG(_(e_letunexp));
2479 else
2480 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002481 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 char_u *s;
2483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 p = get_tv_string_chk(tv);
2485 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002487 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (s != NULL)
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 vim_free(s);
2492 }
2493 }
2494 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 arg_end = arg + 1;
2498 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
2502
2503 /*
2504 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002507 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002511 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 arg_end = p;
2520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
2524
2525 else
2526 EMSG2(_(e_invarg2), arg);
2527
2528 return arg_end;
2529}
2530
2531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2533 */
2534 static int
2535check_changedtick(arg)
2536 char_u *arg;
2537{
2538 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2539 {
2540 EMSG2(_(e_readonlyvar), arg);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Get an lval: variable, Dict item or List item that can be assigned a value
2548 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2549 * "name.key", "name.key[expr]" etc.
2550 * Indexing only works if "name" is an existing List or Dictionary.
2551 * "name" points to the start of the name.
2552 * If "rettv" is not NULL it points to the value to be assigned.
2553 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2554 * wrong; must end in space or cmd separator.
2555 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002556 * flags:
2557 * GLV_QUIET: do not give error messages
2558 * GLV_NO_AUTOLOAD: do not use script autoloading
2559 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002561 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 */
2564 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T *rettv;
2568 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 int unlet;
2570 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 char_u *expr_start, *expr_end;
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 dictitem_T *v;
2578 typval_T var1;
2579 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002585 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589
2590 if (skip)
2591 {
2592 /* When skipping just find the end of the name. */
2593 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
2596
2597 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (expr_start != NULL)
2600 {
2601 /* Don't expand the name when we already know there is an error. */
2602 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2603 && *p != '[' && *p != '.')
2604 {
2605 EMSG(_(e_trailing));
2606 return NULL;
2607 }
2608
2609 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2610 if (lp->ll_exp_name == NULL)
2611 {
2612 /* Report an invalid expression in braces, unless the
2613 * expression evaluation has been cancelled due to an
2614 * aborting error, an interrupt, or an exception. */
2615 if (!aborting() && !quiet)
2616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002617 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 EMSG2(_(e_invarg2), name);
2619 return NULL;
2620 }
2621 }
2622 lp->ll_name = lp->ll_exp_name;
2623 }
2624 else
2625 lp->ll_name = name;
2626
2627 /* Without [idx] or .key we are done. */
2628 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2629 return p;
2630
2631 cc = *p;
2632 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (v == NULL && !quiet)
2635 EMSG2(_(e_undefvar), lp->ll_name);
2636 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 if (v == NULL)
2638 return NULL;
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /*
2641 * Loop until no more [idx] or .key is following.
2642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2647 && !(lp->ll_tv->v_type == VAR_DICT
2648 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
2651 EMSG(_("E689: Can only index a List or Dictionary"));
2652 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E708: [:] must come last"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 len = -1;
2662 if (*p == '.')
2663 {
2664 key = p + 1;
2665 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2666 ;
2667 if (len == 0)
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_(e_emptykey));
2671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = key + len;
2674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 else
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (*p == ':')
2680 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 empty1 = FALSE;
2684 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var1) == NULL)
2687 {
2688 /* not a number or string */
2689 clear_tv(&var1);
2690 return NULL;
2691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Optionally get the second index [ :expr]. */
2695 if (*p == ':')
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (rettv != NULL && (rettv->v_type != VAR_LIST
2706 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 }
2714 p = skipwhite(p + 1);
2715 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 else
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (get_tv_string_chk(&var2) == NULL)
2727 {
2728 /* not a number or string */
2729 if (!empty1)
2730 clear_tv(&var1);
2731 clear_tv(&var2);
2732 return NULL;
2733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
2751 /* Skip to past ']'. */
2752 ++p;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
2757 if (len == -1)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (*key == NUL)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
2764 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 lp->ll_list = NULL;
2770 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002771 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 /* When assigning to a scope dictionary check that a function and
2774 * variable name is valid (only variable name unless it is l: or
2775 * g: dictionary). Disallow overwriting a builtin function. */
2776 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 int prevval;
2779 int wrong;
2780
2781 if (len != -1)
2782 {
2783 prevval = key[len];
2784 key[len] = NUL;
2785 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002786 else
2787 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2789 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 || !valid_varname(key);
2792 if (len != -1)
2793 key[len] = prevval;
2794 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 return NULL;
2796 }
2797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* Can't add "v:" variable. */
2801 if (lp->ll_dict == &vimvardict)
2802 {
2803 EMSG2(_(e_illvar), name);
2804 return NULL;
2805 }
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 p = NULL;
2824 break;
2825 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 return NULL;
2829
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 else
2835 {
2836 /*
2837 * Get the number and item for the only or first index of the List.
2838 */
2839 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
2842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var1);
2845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_list = lp->ll_tv->vval.v_list;
2848 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2849 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 if (lp->ll_n1 < 0)
2852 {
2853 lp->ll_n1 = 0;
2854 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2855 }
2856 }
2857 if (lp->ll_li == NULL)
2858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
2866 /*
2867 * May need to find the item or absolute index for the second
2868 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 * When no index given: "lp->ll_empty2" is TRUE.
2870 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 {
2881 if (!quiet)
2882 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 }
2887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2889 if (lp->ll_n1 < 0)
2890 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2891 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 {
2893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return p;
2904}
2905
2906/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
2910clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 vim_free(lp->ll_exp_name);
2914 vim_free(lp->ll_newkey);
2915}
2916
2917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 listitem_T *ri;
2932 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933
2934 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 cc = *endp;
2939 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 if (op != NULL && *op != '=')
2941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002944 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 if ((di == NULL
2950 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2951 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2952 FALSE)))
2953 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 set_var(lp->ll_name, &tv, FALSE);
2955 clear_tv(&tv);
2956 }
2957 }
2958 else
2959 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 else if (tv_check_lock(lp->ll_newkey == NULL
2964 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002965 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 else if (lp->ll_range)
2968 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 listitem_T *ll_li = lp->ll_li;
2970 int ll_n1 = lp->ll_n1;
2971
2972 /*
2973 * Check whether any of the list items is locked
2974 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002975 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 return;
2979 ri = ri->li_next;
2980 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2981 break;
2982 ll_li = ll_li->li_next;
2983 ++ll_n1;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the List values to the list items.
2988 */
2989 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 if (op != NULL && *op != '=')
2992 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2993 else
2994 {
2995 clear_tv(&lp->ll_li->li_tv);
2996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = ri->li_next;
2999 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3000 break;
3001 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003004 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = NULL;
3007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 lp->ll_li = lp->ll_li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 if (ri != NULL)
3014 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 else if (lp->ll_empty2
3016 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 : lp->ll_n1 != lp->ll_n2)
3018 EMSG(_("E711: List value has not enough items"));
3019 }
3020 else
3021 {
3022 /*
3023 * Assign to a List or Dictionary item.
3024 */
3025 if (lp->ll_newkey != NULL)
3026 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (op != NULL && *op != '=')
3028 {
3029 EMSG2(_(e_letwrong), op);
3030 return;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 if (di == NULL)
3036 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3038 {
3039 vim_free(di);
3040 return;
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (op != NULL && *op != '=')
3045 {
3046 tv_op(lp->ll_tv, rettv, op);
3047 return;
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /*
3053 * Assign the value to the variable or list item.
3054 */
3055 if (copy)
3056 copy_tv(rettv, lp->ll_tv);
3057 else
3058 {
3059 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 }
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064}
3065
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3068 * Returns OK or FAIL.
3069 */
3070 static int
3071tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 typval_T *tv1;
3073 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 char_u *op;
3075{
3076 long n;
3077 char_u numbuf[NUMBUFLEN];
3078 char_u *s;
3079
3080 /* Can't do anything with a Funcref or a Dict on the right. */
3081 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3082 {
3083 switch (tv1->v_type)
3084 {
3085 case VAR_DICT:
3086 case VAR_FUNC:
3087 break;
3088
3089 case VAR_LIST:
3090 if (*op != '+' || tv2->v_type != VAR_LIST)
3091 break;
3092 /* List += List */
3093 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3094 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3095 return OK;
3096
3097 case VAR_NUMBER:
3098 case VAR_STRING:
3099 if (tv2->v_type == VAR_LIST)
3100 break;
3101 if (*op == '+' || *op == '-')
3102 {
3103 /* nr += nr or nr -= nr*/
3104 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 float_T f = n;
3109
3110 if (*op == '+')
3111 f += tv2->vval.v_float;
3112 else
3113 f -= tv2->vval.v_float;
3114 clear_tv(tv1);
3115 tv1->v_type = VAR_FLOAT;
3116 tv1->vval.v_float = f;
3117 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
3120 {
3121 if (*op == '+')
3122 n += get_tv_number(tv2);
3123 else
3124 n -= get_tv_number(tv2);
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_NUMBER;
3127 tv1->vval.v_number = n;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 }
3130 else
3131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (tv2->v_type == VAR_FLOAT)
3133 break;
3134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 /* str .= str */
3136 s = get_tv_string(tv1);
3137 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3138 clear_tv(tv1);
3139 tv1->v_type = VAR_STRING;
3140 tv1->vval.v_string = s;
3141 }
3142 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143
3144#ifdef FEAT_FLOAT
3145 case VAR_FLOAT:
3146 {
3147 float_T f;
3148
3149 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3150 && tv2->v_type != VAR_NUMBER
3151 && tv2->v_type != VAR_STRING))
3152 break;
3153 if (tv2->v_type == VAR_FLOAT)
3154 f = tv2->vval.v_float;
3155 else
3156 f = get_tv_number(tv2);
3157 if (*op == '+')
3158 tv1->vval.v_float += f;
3159 else
3160 tv1->vval.v_float -= f;
3161 }
3162 return OK;
3163#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 }
3165 }
3166
3167 EMSG2(_(e_letwrong), op);
3168 return FAIL;
3169}
3170
3171/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * Add a watcher to a list.
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 list_T *l;
3177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
3179 lw->lw_next = l->lv_watch;
3180 l->lv_watch = lw;
3181}
3182
3183/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003184 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 * No warning when it isn't found...
3186 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 list_T *l;
3190 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
3211list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 list_T *l;
3213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
3229eval_for_line(arg, errp, nextcmdp, skip)
3230 char_u *arg;
3231 int *errp;
3232 char_u **nextcmdp;
3233 int skip;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
3293next_for_item(fi_void, arg)
3294 void *fi_void;
3295 char_u *arg;
3296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
3317free_for_info(fi_void)
3318 void *fi_void;
3319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
3333set_context_for_expression(xp, arg, cmdidx)
3334 expand_T *xp;
3335 char_u *arg;
3336 cmdidx_T cmdidx;
3337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
3446ex_call(eap)
3447 exarg_T *eap;
3448{
3449 char_u *arg = eap->arg;
3450 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003452 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 linenr_T lnum;
3456 int doesrange;
3457 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003460 if (eap->skip)
3461 {
3462 /* trans_function_name() doesn't work well when skipping, use eval0()
3463 * instead to skip to any following command, e.g. for:
3464 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003465 ++emsg_skip;
3466 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3467 clear_tv(&rettv);
3468 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 return;
3470 }
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003473 if (fudi.fd_newkey != NULL)
3474 {
3475 /* Still need to give an error message for missing key. */
3476 EMSG2(_(e_dictkey), fudi.fd_newkey);
3477 vim_free(fudi.fd_newkey);
3478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 if (tofree == NULL)
3480 return;
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 /* Increase refcount on dictionary, it could get deleted when evaluating
3483 * the arguments. */
3484 if (fudi.fd_dict != NULL)
3485 ++fudi.fd_dict->dv_refcount;
3486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003489 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 /* Skip white space to allow ":call func ()". Not good, but required for
3492 * backward compatibility. */
3493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (*startarg != '(')
3497 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003498 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 goto end;
3500 }
3501
3502 /*
3503 * When skipping, evaluate the function once, to find the end of the
3504 * arguments.
3505 * When the function takes a range, this is discovered after the first
3506 * call, and the loop is broken.
3507 */
3508 if (eap->skip)
3509 {
3510 ++emsg_skip;
3511 lnum = eap->line2; /* do it once, also with an invalid range */
3512 }
3513 else
3514 lnum = eap->line1;
3515 for ( ; lnum <= eap->line2; ++lnum)
3516 {
3517 if (!eap->skip && eap->addr_count > 0)
3518 {
3519 curwin->w_cursor.lnum = lnum;
3520 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003521#ifdef FEAT_VIRTUALEDIT
3522 curwin->w_cursor.coladd = 0;
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 eap->line1, eap->line2, &doesrange,
3528 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003533
3534 /* Handle a function returning a Funcref, Dictionary or List. */
3535 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3536 {
3537 failed = TRUE;
3538 break;
3539 }
3540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (doesrange || eap->skip)
3543 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (aborting())
3550 break;
3551 }
3552 if (eap->skip)
3553 --emsg_skip;
3554
3555 if (!failed)
3556 {
3557 /* Check for trailing illegal characters and a following command. */
3558 if (!ends_excmd(*arg))
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 else
3564 eap->nextcmd = check_nextcmd(arg);
3565 }
3566
3567end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * ":unlet[!] var1 ... " command.
3574 */
3575 void
3576ex_unlet(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
3586ex_lockvar(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
3607ex_unletlock(eap, argstart, deep)
3608 exarg_T *eap;
3609 char_u *argstart;
3610 int deep;
3611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 int forceit;
3666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
3691 int ll_n1 = lp->ll_n1;
3692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 hashtab_T *ht;
3734 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003735 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003737 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 ht = find_var_ht(name, &varname);
3740 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003742 if (ht == &globvarht)
3743 d = &globvardict;
3744 else if (current_funccal != NULL
3745 && ht == &current_funccal->l_vars.dv_hashtab)
3746 d = &current_funccal->l_vars;
3747 else if (ht == &compat_hashtab)
3748 d = &vimvardict;
3749 else
3750 {
3751 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3752 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3753 }
3754 if (d == NULL)
3755 {
3756 EMSG2(_(e_intern2), "do_unlet()");
3757 return FAIL;
3758 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hi = hash_find(ht, varname);
3760 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003762 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003764 || var_check_ro(di->di_flags, name, FALSE)
3765 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003766 return FAIL;
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 delete_var(ht, hi);
3769 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 if (forceit)
3773 return OK;
3774 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return FAIL;
3776}
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778/*
3779 * Lock or unlock variable indicated by "lp".
3780 * "deep" is the levels to go (-1 for unlimited);
3781 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3782 */
3783 static int
3784do_lock_var(lp, name_end, deep, lock)
3785 lval_T *lp;
3786 char_u *name_end;
3787 int deep;
3788 int lock;
3789{
3790 int ret = OK;
3791 int cc;
3792 dictitem_T *di;
3793
3794 if (deep == 0) /* nothing to do */
3795 return OK;
3796
3797 if (lp->ll_tv == NULL)
3798 {
3799 cc = *name_end;
3800 *name_end = NUL;
3801
3802 /* Normal name or expanded name. */
3803 if (check_changedtick(lp->ll_name))
3804 ret = FAIL;
3805 else
3806 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003807 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 if (di == NULL)
3809 ret = FAIL;
3810 else
3811 {
3812 if (lock)
3813 di->di_flags |= DI_FLAGS_LOCK;
3814 else
3815 di->di_flags &= ~DI_FLAGS_LOCK;
3816 item_lock(&di->di_tv, deep, lock);
3817 }
3818 }
3819 *name_end = cc;
3820 }
3821 else if (lp->ll_range)
3822 {
3823 listitem_T *li = lp->ll_li;
3824
3825 /* (un)lock a range of List items. */
3826 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3827 {
3828 item_lock(&li->li_tv, deep, lock);
3829 li = li->li_next;
3830 ++lp->ll_n1;
3831 }
3832 }
3833 else if (lp->ll_list != NULL)
3834 /* (un)lock a List item. */
3835 item_lock(&lp->ll_li->li_tv, deep, lock);
3836 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003837 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838 item_lock(&lp->ll_di->di_tv, deep, lock);
3839
3840 return ret;
3841}
3842
3843/*
3844 * Lock or unlock an item. "deep" is nr of levels to go.
3845 */
3846 static void
3847item_lock(tv, deep, lock)
3848 typval_T *tv;
3849 int deep;
3850 int lock;
3851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
3876 case VAR_LIST:
3877 if ((l = tv->vval.v_list) != NULL)
3878 {
3879 if (lock)
3880 l->lv_lock |= VAR_LOCKED;
3881 else
3882 l->lv_lock &= ~VAR_LOCKED;
3883 if (deep < 0 || deep > 1)
3884 /* recursive: lock/unlock the items the List contains */
3885 for (li = l->lv_first; li != NULL; li = li->li_next)
3886 item_lock(&li->li_tv, deep - 1, lock);
3887 }
3888 break;
3889 case VAR_DICT:
3890 if ((d = tv->vval.v_dict) != NULL)
3891 {
3892 if (lock)
3893 d->dv_lock |= VAR_LOCKED;
3894 else
3895 d->dv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 {
3898 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3901 {
3902 if (!HASHITEM_EMPTY(hi))
3903 {
3904 --todo;
3905 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3906 }
3907 }
3908 }
3909 }
3910 }
3911 --recurse;
3912}
3913
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003915 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3916 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917 */
3918 static int
3919tv_islocked(tv)
3920 typval_T *tv;
3921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
3936del_menutrans_vars()
3937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
3964static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
3973cat_prefix_varname(prefix, name)
3974 int prefix;
3975 char_u *name;
3976{
3977 int len;
3978
3979 len = (int)STRLEN(name) + 3;
3980 if (len > varnamebuflen)
3981 {
3982 vim_free(varnamebuf);
3983 len += 10; /* some additional space */
3984 varnamebuf = alloc(len);
3985 if (varnamebuf == NULL)
3986 {
3987 varnamebuflen = 0;
3988 return NULL;
3989 }
3990 varnamebuflen = len;
3991 }
3992 *varnamebuf = prefix;
3993 varnamebuf[1] = ':';
3994 STRCPY(varnamebuf + 2, name);
3995 return varnamebuf;
3996}
3997
3998/*
3999 * Function given to ExpandGeneric() to obtain the list of user defined
4000 * (global/buffer/window/built-in) variable names.
4001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *
4003get_user_var_name(xp, idx)
4004 expand_T *xp;
4005 int idx;
4006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u **nextcmd;
4131 int evaluate;
4132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int evaluate;
4173{
4174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /*
4178 * Get the first variable.
4179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 if ((*arg)[0] == '?')
4184 {
4185 result = FALSE;
4186 if (evaluate)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 int error = FALSE;
4189
4190 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 /*
4198 * Get the second variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203
4204 /*
4205 * Check for the ":".
4206 */
4207 if ((*arg)[0] != ':')
4208 {
4209 EMSG(_("E109: Missing ':' after '?'"));
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
4215 /*
4216 * Get the third variable.
4217 */
4218 *arg = skipwhite(*arg + 1);
4219 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4220 {
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228
4229 return OK;
4230}
4231
4232/*
4233 * Handle first level expression:
4234 * expr2 || expr2 || expr2 logical OR
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int evaluate;
4246{
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long result;
4249 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Repeat until there is no following "||".
4260 */
4261 first = TRUE;
4262 result = FALSE;
4263 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4264 {
4265 if (evaluate && first)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (error)
4271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first = FALSE;
4273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 2);
4279 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4280 return FAIL;
4281
4282 /*
4283 * Compute the result.
4284 */
4285 if (evaluate && !result)
4286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (error)
4291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (evaluate)
4294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 rettv->v_type = VAR_NUMBER;
4296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle second level expression:
4305 * expr3 && expr3 && expr3 logical AND
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int evaluate;
4317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "&&".
4331 */
4332 first = TRUE;
4333 result = TRUE;
4334 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval4(arg, &var2, evaluate && result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle third level expression:
4376 * var1 == var2
4377 * var1 =~ var2
4378 * var1 != var2
4379 * var1 !~ var2
4380 * var1 > var2
4381 * var1 >= var2
4382 * var1 < var2
4383 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 * var1 is var2
4385 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
4388 * "arg" is advanced to the next non-white after the recognized expression.
4389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int evaluate;
4397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 char_u *p;
4400 int i;
4401 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int len = 2;
4404 long n1, n2;
4405 char_u *s1, *s2;
4406 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4407 regmatch_T regmatch;
4408 int ic;
4409 char_u *save_cpo;
4410
4411 /*
4412 * Get the first variable.
4413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416
4417 p = *arg;
4418 switch (p[0])
4419 {
4420 case '=': if (p[1] == '=')
4421 type = TYPE_EQUAL;
4422 else if (p[1] == '~')
4423 type = TYPE_MATCH;
4424 break;
4425 case '!': if (p[1] == '=')
4426 type = TYPE_NEQUAL;
4427 else if (p[1] == '~')
4428 type = TYPE_NOMATCH;
4429 break;
4430 case '>': if (p[1] != '=')
4431 {
4432 type = TYPE_GREATER;
4433 len = 1;
4434 }
4435 else
4436 type = TYPE_GEQUAL;
4437 break;
4438 case '<': if (p[1] != '=')
4439 {
4440 type = TYPE_SMALLER;
4441 len = 1;
4442 }
4443 else
4444 type = TYPE_SEQUAL;
4445 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 case 'i': if (p[1] == 's')
4447 {
4448 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4449 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004450 i = p[len];
4451 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 {
4453 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4454 type_is = TRUE;
4455 }
4456 }
4457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
4463 if (type != TYPE_UNKNOWN)
4464 {
4465 /* extra question mark appended: ignore case */
4466 if (p[len] == '?')
4467 {
4468 ic = TRUE;
4469 ++len;
4470 }
4471 /* extra '#' appended: match case */
4472 else if (p[len] == '#')
4473 {
4474 ic = FALSE;
4475 ++len;
4476 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else
4479 ic = p_ic;
4480
4481 /*
4482 * Get the second variable.
4483 */
4484 *arg = skipwhite(p + len);
4485 if (eval5(arg, &var2, evaluate) == FAIL)
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return FAIL;
4489 }
4490
4491 if (evaluate)
4492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 if (type_is && rettv->v_type != var2.v_type)
4494 {
4495 /* For "is" a different type always means FALSE, for "notis"
4496 * it means TRUE. */
4497 n1 = (type == TYPE_NEQUAL);
4498 }
4499 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_list == var2.vval.v_list);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004514 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4523 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4530 {
4531 if (type_is)
4532 {
4533 n1 = (rettv->v_type == var2.v_type
4534 && rettv->vval.v_dict == var2.vval.v_dict);
4535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 else if (rettv->v_type != var2.v_type
4539 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4540 {
4541 if (rettv->v_type != var2.v_type)
4542 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4543 else
4544 EMSG(_("E736: Invalid operation for Dictionary"));
4545 clear_tv(rettv);
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
4549 else
4550 {
4551 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004552 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4553 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004554 if (type == TYPE_NEQUAL)
4555 n1 = !n1;
4556 }
4557 }
4558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4560 {
4561 if (rettv->v_type != var2.v_type
4562 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4563 {
4564 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004565 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004567 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 clear_tv(rettv);
4569 clear_tv(&var2);
4570 return FAIL;
4571 }
4572 else
4573 {
4574 /* Compare two Funcrefs for being equal or unequal. */
4575 if (rettv->vval.v_string == NULL
4576 || var2.vval.v_string == NULL)
4577 n1 = FALSE;
4578 else
4579 n1 = STRCMP(rettv->vval.v_string,
4580 var2.vval.v_string) == 0;
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 }
4585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586#ifdef FEAT_FLOAT
4587 /*
4588 * If one of the two variables is a float, compare as a float.
4589 * When using "=~" or "!~", always compare as string.
4590 */
4591 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4592 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4593 {
4594 float_T f1, f2;
4595
4596 if (rettv->v_type == VAR_FLOAT)
4597 f1 = rettv->vval.v_float;
4598 else
4599 f1 = get_tv_number(rettv);
4600 if (var2.v_type == VAR_FLOAT)
4601 f2 = var2.vval.v_float;
4602 else
4603 f2 = get_tv_number(&var2);
4604 n1 = FALSE;
4605 switch (type)
4606 {
4607 case TYPE_EQUAL: n1 = (f1 == f2); break;
4608 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4609 case TYPE_GREATER: n1 = (f1 > f2); break;
4610 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4611 case TYPE_SMALLER: n1 = (f1 < f2); break;
4612 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4613 case TYPE_UNKNOWN:
4614 case TYPE_MATCH:
4615 case TYPE_NOMATCH: break; /* avoid gcc warning */
4616 }
4617 }
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * If one of the two variables is a number, compare as a number.
4622 * When using "=~" or "!~", always compare as string.
4623 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 n1 = get_tv_number(rettv);
4628 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (n1 == n2); break;
4632 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4633 case TYPE_GREATER: n1 = (n1 > n2); break;
4634 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4635 case TYPE_SMALLER: n1 = (n1 < n2); break;
4636 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4637 case TYPE_UNKNOWN:
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH: break; /* avoid gcc warning */
4640 }
4641 }
4642 else
4643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 s1 = get_tv_string_buf(rettv, buf1);
4645 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4647 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4648 else
4649 i = 0;
4650 n1 = FALSE;
4651 switch (type)
4652 {
4653 case TYPE_EQUAL: n1 = (i == 0); break;
4654 case TYPE_NEQUAL: n1 = (i != 0); break;
4655 case TYPE_GREATER: n1 = (i > 0); break;
4656 case TYPE_GEQUAL: n1 = (i >= 0); break;
4657 case TYPE_SMALLER: n1 = (i < 0); break;
4658 case TYPE_SEQUAL: n1 = (i <= 0); break;
4659
4660 case TYPE_MATCH:
4661 case TYPE_NOMATCH:
4662 /* avoid 'l' flag in 'cpoptions' */
4663 save_cpo = p_cpo;
4664 p_cpo = (char_u *)"";
4665 regmatch.regprog = vim_regcomp(s2,
4666 RE_MAGIC + RE_STRING);
4667 regmatch.rm_ic = ic;
4668 if (regmatch.regprog != NULL)
4669 {
4670 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004671 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (type == TYPE_NOMATCH)
4673 n1 = !n1;
4674 }
4675 p_cpo = save_cpo;
4676 break;
4677
4678 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4679 }
4680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 rettv->v_type = VAR_NUMBER;
4684 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 }
4687
4688 return OK;
4689}
4690
4691/*
4692 * Handle fourth level expression:
4693 * + number addition
4694 * - number subtraction
4695 * . string concatenation
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
4707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
4709 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 float_T f1 = 0, f2 = 0;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 char_u *s1, *s2;
4716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4717 char_u *p;
4718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '+', '-' or '.' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '+' && op != '-' && op != '.')
4732 break;
4733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if ((op != '+' || rettv->v_type != VAR_LIST)
4735#ifdef FEAT_FLOAT
4736 && (op == '.' || rettv->v_type != VAR_FLOAT)
4737#endif
4738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
4740 /* For "list + ...", an illegal use of the first operand as
4741 * a number cannot be determined before evaluating the 2nd
4742 * operand: if this is also a list, all is ok.
4743 * For "something . ...", "something - ..." or "non-list + ...",
4744 * we know that the first operand needs to be a string or number
4745 * without evaluating the 2nd operand. So check before to avoid
4746 * side effects after an error. */
4747 if (evaluate && get_tv_string_chk(rettv) == NULL)
4748 {
4749 clear_tv(rettv);
4750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /*
4755 * Get the second variable.
4756 */
4757 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762 }
4763
4764 if (evaluate)
4765 {
4766 /*
4767 * Compute the result.
4768 */
4769 if (op == '.')
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4772 s2 = get_tv_string_buf_chk(&var2, buf2);
4773 if (s2 == NULL) /* type error ? */
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004784 else if (op == '+' && rettv->v_type == VAR_LIST
4785 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 {
4787 /* concatenate Lists */
4788 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4789 &var3) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 clear_tv(rettv);
4796 *rettv = var3;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
4799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f1 = rettv->vval.v_float;
4806 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 else
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 n1 = get_tv_number_chk(rettv, &error);
4812 if (error)
4813 {
4814 /* This can only happen for "list + non-list". For
4815 * "non-list + ..." or "something - ...", we returned
4816 * before evaluating the 2nd operand. */
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 f1 = n1;
4823#endif
4824 }
4825#ifdef FEAT_FLOAT
4826 if (var2.v_type == VAR_FLOAT)
4827 {
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 if (error)
4836 {
4837 clear_tv(rettv);
4838 clear_tv(&var2);
4839 return FAIL;
4840 }
4841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 f2 = n2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847
4848#ifdef FEAT_FLOAT
4849 /* If there is a float on either side the result is a float. */
4850 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4851 {
4852 if (op == '+')
4853 f1 = f1 + f2;
4854 else
4855 f1 = f1 - f2;
4856 rettv->v_type = VAR_FLOAT;
4857 rettv->vval.v_float = f1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#endif
4861 {
4862 if (op == '+')
4863 n1 = n1 + n2;
4864 else
4865 n1 = n1 - n2;
4866 rettv->v_type = VAR_NUMBER;
4867 rettv->vval.v_number = n1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 return OK;
4874}
4875
4876/*
4877 * Handle fifth level expression:
4878 * * number multiplication
4879 * / number division
4880 * % number modulo
4881 *
4882 * "arg" must point to the first non-white of the expression.
4883 * "arg" is advanced to the next non-white after the recognized expression.
4884 *
4885 * Return OK or FAIL.
4886 */
4887 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004892 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int op;
4896 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 int use_float = FALSE;
4899 float_T f1 = 0, f2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 /*
4904 * Get the first variable.
4905 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 /*
4910 * Repeat computing, until no '*', '/' or '%' is following.
4911 */
4912 for (;;)
4913 {
4914 op = **arg;
4915 if (op != '*' && op != '/' && op != '%')
4916 break;
4917
4918 if (evaluate)
4919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#ifdef FEAT_FLOAT
4921 if (rettv->v_type == VAR_FLOAT)
4922 {
4923 f1 = rettv->vval.v_float;
4924 use_float = TRUE;
4925 n1 = 0;
4926 }
4927 else
4928#endif
4929 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 if (error)
4932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
4935 n1 = 0;
4936
4937 /*
4938 * Get the second variable.
4939 */
4940 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 if (evaluate)
4945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (var2.v_type == VAR_FLOAT)
4948 {
4949 if (!use_float)
4950 {
4951 f1 = n1;
4952 use_float = TRUE;
4953 }
4954 f2 = var2.vval.v_float;
4955 n2 = 0;
4956 }
4957 else
4958#endif
4959 {
4960 n2 = get_tv_number_chk(&var2, &error);
4961 clear_tv(&var2);
4962 if (error)
4963 return FAIL;
4964#ifdef FEAT_FLOAT
4965 if (use_float)
4966 f2 = n2;
4967#endif
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 if (op == '*')
4978 f1 = f1 * f2;
4979 else if (op == '/')
4980 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# ifdef VMS
4982 /* VMS crashes on divide by zero, work around it */
4983 if (f2 == 0.0)
4984 {
4985 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004990 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991 }
4992 else
4993 f1 = f1 / f2;
4994# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 /* We rely on the floating point library to handle divide
4996 * by zero to result in "inf" and not a crash. */
4997 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005002 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 return FAIL;
5004 }
5005 rettv->v_type = VAR_FLOAT;
5006 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 n1 = n1 * n2;
5013 else if (op == '/')
5014 {
5015 if (n2 == 0) /* give an error message? */
5016 {
5017 if (n1 == 0)
5018 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5019 else if (n1 < 0)
5020 n1 = -0x7fffffffL;
5021 else
5022 n1 = 0x7fffffffL;
5023 }
5024 else
5025 n1 = n1 / n2;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029 if (n2 == 0) /* give an error message? */
5030 n1 = 0;
5031 else
5032 n1 = n1 % n2;
5033 }
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 return OK;
5041}
5042
5043/*
5044 * Handle sixth level expression:
5045 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005046 * "string" string constant
5047 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * &option-name option value
5049 * @r register contents
5050 * identifier variable value
5051 * function() function call
5052 * $VAR environment variable
5053 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005054 * [expr, expr] List
5055 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * Also handle:
5058 * ! in front logical NOT
5059 * - in front unary minus
5060 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * trailing [] subscript in String or List
5062 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * "arg" must point to the first non-white of the expression.
5065 * "arg" is advanced to the next non-white after the recognized expression.
5066 *
5067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005070eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005074 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 long n;
5077 int len;
5078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *start_leader, *end_leader;
5080 int ret = OK;
5081 char_u *alias;
5082
5083 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * Skip '!' and '-' characters. They are handled later.
5091 */
5092 start_leader = *arg;
5093 while (**arg == '!' || **arg == '-' || **arg == '+')
5094 *arg = skipwhite(*arg + 1);
5095 end_leader = *arg;
5096
5097 switch (**arg)
5098 {
5099 /*
5100 * Number constant.
5101 */
5102 case '0':
5103 case '1':
5104 case '2':
5105 case '3':
5106 case '4':
5107 case '5':
5108 case '6':
5109 case '7':
5110 case '8':
5111 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 {
5113#ifdef FEAT_FLOAT
5114 char_u *p = skipdigits(*arg + 1);
5115 int get_float = FALSE;
5116
5117 /* We accept a float when the format matches
5118 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005119 * strict to avoid backwards compatibility problems.
5120 * Don't look for a float after the "." operator, so that
5121 * ":let vers = 1.2.3" doesn't fail. */
5122 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 get_float = TRUE;
5125 p = skipdigits(p + 2);
5126 if (*p == 'e' || *p == 'E')
5127 {
5128 ++p;
5129 if (*p == '-' || *p == '+')
5130 ++p;
5131 if (!vim_isdigit(*p))
5132 get_float = FALSE;
5133 else
5134 p = skipdigits(p + 1);
5135 }
5136 if (ASCII_ISALPHA(*p) || *p == '.')
5137 get_float = FALSE;
5138 }
5139 if (get_float)
5140 {
5141 float_T f;
5142
5143 *arg += string2float(*arg, &f);
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_FLOAT;
5147 rettv->vval.v_float = f;
5148 }
5149 }
5150 else
5151#endif
5152 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005153 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *arg += len;
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = n;
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * String constant: "string".
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 break;
5175
5176 /*
5177 * List: [expr, expr]
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Dictionary: {key: val, key: val}
5184 */
5185 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5186 break;
5187
5188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Environment variable: $VAR.
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Register contents: @r.
5202 */
5203 case '@': ++*arg;
5204 if (evaluate)
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005207 rettv->vval.v_string = get_reg_contents(**arg,
5208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (**arg != NUL)
5211 ++*arg;
5212 break;
5213
5214 /*
5215 * nested expression: (expression).
5216 */
5217 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (**arg == ')')
5220 ++*arg;
5221 else if (ret == OK)
5222 {
5223 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ret = FAIL;
5226 }
5227 break;
5228
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 if (ret == NOTDONE)
5234 {
5235 /*
5236 * Must be a variable or function name.
5237 * Can also be a curly-braces kind of name: {expr}.
5238 */
5239 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (alias != NULL)
5242 s = alias;
5243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 ret = FAIL;
5246 else
5247 {
5248 if (**arg == '(') /* recursive! */
5249 {
5250 /* If "s" is the name of a variable of type VAR_FUNC
5251 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005252 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253
5254 /* Invoke the function. */
5255 ret = get_func_tv(s, len, rettv, arg,
5256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258
5259 /* If evaluate is FALSE rettv->v_type was not set in
5260 * get_func_tv, but it's needed in handle_subscript() to parse
5261 * what follows. So set it here. */
5262 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5263 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005264 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005265 rettv->v_type = VAR_FUNC;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* Stop the expression evaluation when immediately
5269 * aborting on error, or when an interrupt occurred or
5270 * an exception was thrown but not caught. */
5271 if (aborting())
5272 {
5273 if (ret == OK)
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005279 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 else
5281 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg = skipwhite(*arg);
5287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5289 * expr(expr). */
5290 if (ret == OK)
5291 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 /*
5294 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5295 */
5296 if (ret == OK && evaluate && end_leader > start_leader)
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 int val = 0;
5300#ifdef FEAT_FLOAT
5301 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 if (rettv->v_type == VAR_FLOAT)
5304 f = rettv->vval.v_float;
5305 else
5306#endif
5307 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 clear_tv(rettv);
5311 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else
5314 {
5315 while (end_leader > start_leader)
5316 {
5317 --end_leader;
5318 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 {
5320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 f = !f;
5323 else
5324#endif
5325 val = !val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 {
5329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 f = -f;
5332 else
5333#endif
5334 val = -val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 {
5340 clear_tv(rettv);
5341 rettv->vval.v_float = f;
5342 }
5343 else
5344#endif
5345 {
5346 clear_tv(rettv);
5347 rettv->v_type = VAR_NUMBER;
5348 rettv->vval.v_number = val;
5349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
5353 return ret;
5354}
5355
5356/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5358 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5360 */
5361 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
5368 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 long len = -1;
5372 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005376 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 if (verbose)
5379 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 return FAIL;
5381 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005382#ifdef FEAT_FLOAT
5383 else if (rettv->v_type == VAR_FLOAT)
5384 {
5385 if (verbose)
5386 EMSG(_(e_float_as_string));
5387 return FAIL;
5388 }
5389#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
5483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int evaluate;
5629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int evaluate;
5708{
5709 char_u *p;
5710 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int extra = 0;
5712
5713 /*
5714 * Find the end of the string, skipping backslashed characters.
5715 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 if (*p == '\\' && p[1] != NUL)
5719 {
5720 ++p;
5721 /* A "\<x>" form occupies at least 4 characters, and produces up
5722 * to 6 characters: reserve space for 2 extra */
5723 if (*p == '<')
5724 extra += 2;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 if (*p != '"')
5729 {
5730 EMSG2(_("E114: Missing quote: %s"), *arg);
5731 return FAIL;
5732 }
5733
5734 /* If only parsing, set *arg and return here */
5735 if (!evaluate)
5736 {
5737 *arg = p + 1;
5738 return OK;
5739 }
5740
5741 /*
5742 * Copy the string into allocated memory, handling backslashed
5743 * characters.
5744 */
5745 name = alloc((unsigned)(p - *arg + extra));
5746 if (name == NULL)
5747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 rettv->v_type = VAR_STRING;
5749 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (*p == '\\')
5754 {
5755 switch (*++p)
5756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 case 'b': *name++ = BS; ++p; break;
5758 case 'e': *name++ = ESC; ++p; break;
5759 case 'f': *name++ = FF; ++p; break;
5760 case 'n': *name++ = NL; ++p; break;
5761 case 'r': *name++ = CAR; ++p; break;
5762 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 case 'X': /* hex: "\x1", "\x12" */
5765 case 'x':
5766 case 'u': /* Unicode: "\u0023" */
5767 case 'U':
5768 if (vim_isxdigit(p[1]))
5769 {
5770 int n, nr;
5771 int c = toupper(*p);
5772
5773 if (c == 'X')
5774 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005775 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else
5778 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 nr = 0;
5780 while (--n >= 0 && vim_isxdigit(p[1]))
5781 {
5782 ++p;
5783 nr = (nr << 4) + hex2nr(*p);
5784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_MBYTE
5787 /* For "\u" store the number according to
5788 * 'encoding'. */
5789 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else
5792#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 break;
5796
5797 /* octal: "\1", "\12", "\123" */
5798 case '0':
5799 case '1':
5800 case '2':
5801 case '3':
5802 case '4':
5803 case '5':
5804 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '7': *name = *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 *name = (*name << 3) + *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
5810 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814
5815 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (extra != 0)
5818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 /* FALLTHROUGH */
5823
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 }
5828 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 *arg = p + 1;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return OK;
5836}
5837
5838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Return OK or FAIL.
5841 */
5842 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 int evaluate;
5847{
5848 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 int reduce = 0;
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 break;
5861 ++reduce;
5862 ++p;
5863 }
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 return FAIL;
5870 }
5871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 if (!evaluate)
5874 {
5875 *arg = p + 1;
5876 return OK;
5877 }
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 str = alloc((unsigned)((p - *arg) - reduce));
5883 if (str == NULL)
5884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++p;
5895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 *arg = p + 1;
5900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Allocate a variable for a List and fill it from "*arg".
5906 * Return OK or FAIL.
5907 */
5908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 int evaluate;
5913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l = NULL;
5915 typval_T tv;
5916 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (evaluate)
5919 {
5920 l = list_alloc();
5921 if (l == NULL)
5922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 while (**arg != ']' && **arg != NUL)
5927 {
5928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5929 goto failret;
5930 if (evaluate)
5931 {
5932 item = listitem_alloc();
5933 if (item != NULL)
5934 {
5935 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005936 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 list_append(l, item);
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 else
5940 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 }
5942
5943 if (**arg == ']')
5944 break;
5945 if (**arg != ',')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 goto failret;
5949 }
5950 *arg = skipwhite(*arg + 1);
5951 }
5952
5953 if (**arg != ']')
5954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956failret:
5957 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005958 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 return FAIL;
5960 }
5961
5962 *arg = skipwhite(*arg + 1);
5963 if (evaluate)
5964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 rettv->v_type = VAR_LIST;
5966 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 ++l->lv_refcount;
5968 }
5969
5970 return OK;
5971}
5972
5973/*
5974 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005975 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005977 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978list_alloc()
5979{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 list_T *l;
5981
5982 l = (list_T *)alloc_clear(sizeof(list_T));
5983 if (l != NULL)
5984 {
5985 /* Prepend the list to the list of lists for garbage collection. */
5986 if (first_list != NULL)
5987 first_list->lv_used_prev = l;
5988 l->lv_used_prev = NULL;
5989 l->lv_used_next = first_list;
5990 first_list = l;
5991 }
5992 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996 * Allocate an empty list for a return value.
5997 * Returns OK or FAIL.
5998 */
5999 static int
6000rettv_list_alloc(rettv)
6001 typval_T *rettv;
6002{
6003 list_T *l = list_alloc();
6004
6005 if (l == NULL)
6006 return FAIL;
6007
6008 rettv->vval.v_list = l;
6009 rettv->v_type = VAR_LIST;
6010 ++l->lv_refcount;
6011 return OK;
6012}
6013
6014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Unreference a list: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006018 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031list_free(l, recurse)
6032 list_T *l;
6033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062listitem_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 vim_free(item);
6076}
6077
6078/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079 * Remove a list item from a List and free it. Also clears the value.
6080 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006081 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l;
6084 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006085{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006086 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006087 listitem_free(item);
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Get the number of items in a list.
6092 */
6093 static long
6094list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 if (l == NULL)
6098 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006099 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100}
6101
6102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE when two lists have exactly the same values.
6104 */
6105 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 list_T *l1;
6108 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111{
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006114 if (l1 == NULL || l2 == NULL)
6115 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 if (l1 == l2)
6117 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 if (list_len(l1) != list_len(l2))
6119 return FALSE;
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 for (item1 = l1->lv_first, item2 = l2->lv_first;
6122 item1 != NULL && item2 != NULL;
6123 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return FALSE;
6126 return item1 == NULL && item2 == NULL;
6127}
6128
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006129#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6130 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006131/*
6132 * Return the dictitem that an entry in a hashtable points to.
6133 */
6134 dictitem_T *
6135dict_lookup(hi)
6136 hashitem_T *hi;
6137{
6138 return HI2DI(hi);
6139}
6140#endif
6141
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 * Return TRUE when two dictionaries have exactly the same key/values.
6144 */
6145 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dict_T *d1;
6148 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 hashitem_T *hi;
6153 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006156 if (d1 == NULL || d2 == NULL)
6157 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006158 if (d1 == d2)
6159 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 if (dict_len(d1) != dict_len(d2))
6161 return FALSE;
6162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006163 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 if (!HASHITEM_EMPTY(hi))
6167 {
6168 item2 = dict_find(d2, hi->hi_key, -1);
6169 if (item2 == NULL)
6170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 return FALSE;
6173 --todo;
6174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175 }
6176 return TRUE;
6177}
6178
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179static int tv_equal_recurse_limit;
6180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006184 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 */
6186 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 typval_T *tv1;
6189 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 int ic; /* ignore case */
6191 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
6193 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006194 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006196 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 * recursiveness to a limit. We guess they are equal then.
6203 * A fixed limit has the problem of still taking an awful long time.
6204 * Reduce the limit every time running into it. That should work fine for
6205 * deeply linked structures that are not recursively linked and catch
6206 * recursiveness quickly. */
6207 if (!recursive)
6208 tv_equal_recurse_limit = 1000;
6209 if (recursive_cnt >= tv_equal_recurse_limit)
6210 {
6211 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 ++recursive_cnt;
6225 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6226 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006227 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 case VAR_FUNC:
6230 return (tv1->vval.v_string != NULL
6231 && tv2->vval.v_string != NULL
6232 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6233
6234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006237#ifdef FEAT_FLOAT
6238 case VAR_FLOAT:
6239 return tv1->vval.v_float == tv2->vval.v_float;
6240#endif
6241
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 case VAR_STRING:
6243 s1 = get_tv_string_buf(tv1, buf1);
6244 s2 = get_tv_string_buf(tv2, buf2);
6245 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return TRUE;
6250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long n;
6261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
6338list_find_nr(l, idx, errorp)
6339 list_T *l;
6340 long idx;
6341 int *errorp; /* set to TRUE when something wrong */
6342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
6359list_find_str(l, idx)
6360 list_T *l;
6361 long idx;
6362{
6363 listitem_T *li;
6364
6365 li = list_find(l, idx - 1);
6366 if (li == NULL)
6367 {
6368 EMSGN(_(e_listidx), idx);
6369 return NULL;
6370 }
6371 return get_tv_string(&li->li_tv);
6372}
6373
6374/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375 * Locate "item" list "l" and return its index.
6376 * Returns -1 when "item" is not in the list.
6377 */
6378 static long
6379list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382{
6383 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385
6386 if (l == NULL)
6387 return -1;
6388 idx = 0;
6389 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6390 ++idx;
6391 if (li == NULL)
6392 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006393 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006394}
6395
6396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 * Append item "item" to the end of list "l".
6398 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 list_T *l;
6402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 copy_tv(tv, &li->li_tv);
6435 list_append(l, li);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006440 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 * Return FAIL when out of memory.
6442 */
6443 int
6444list_append_dict(list, dict)
6445 list_T *list;
6446 dict_T *dict;
6447{
6448 listitem_T *li = listitem_alloc();
6449
6450 if (li == NULL)
6451 return FAIL;
6452 li->li_tv.v_type = VAR_DICT;
6453 li->li_tv.v_lock = 0;
6454 li->li_tv.vval.v_dict = dict;
6455 list_append(list, li);
6456 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 return OK;
6458}
6459
6460/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 * Returns FAIL when out of memory.
6464 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006465 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 list_T *l;
6468 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 list_append(l, li);
6476 li->li_tv.v_type = VAR_STRING;
6477 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (str == NULL)
6479 li->li_tv.vval.v_string = NULL;
6480 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006481 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 return FAIL;
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487 * Append "n" to list "l".
6488 * Returns FAIL when out of memory.
6489 */
6490 static int
6491list_append_number(l, n)
6492 list_T *l;
6493 varnumber_T n;
6494{
6495 listitem_T *li;
6496
6497 li = listitem_alloc();
6498 if (li == NULL)
6499 return FAIL;
6500 li->li_tv.v_type = VAR_NUMBER;
6501 li->li_tv.v_lock = 0;
6502 li->li_tv.vval.v_number = n;
6503 list_append(l, li);
6504 return OK;
6505}
6506
6507/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 * If "item" is NULL append at the end.
6510 * Return FAIL when out of memory.
6511 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006512 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l;
6515 typval_T *tv;
6516 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
6520 if (ni == NULL)
6521 return FAIL;
6522 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523 list_insert(l, ni, item);
6524 return OK;
6525}
6526
6527 void
6528list_insert(l, ni, item)
6529 list_T *l;
6530 listitem_T *ni;
6531 listitem_T *item;
6532{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 if (item == NULL)
6534 /* Append new item at end of list. */
6535 list_append(l, ni);
6536 else
6537 {
6538 /* Insert new item before existing item. */
6539 ni->li_prev = item->li_prev;
6540 ni->li_next = item;
6541 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 ++l->lv_idx;
6545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006549 l->lv_idx_item = NULL;
6550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554}
6555
6556/*
6557 * Extend "l1" with "l2".
6558 * If "bef" is NULL append at the end, otherwise insert before this item.
6559 * Returns FAIL when out of memory.
6560 */
6561 static int
6562list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006570 /* We also quit the loop when we have inserted the original item count of
6571 * the list, avoid a hang when we extend a list with itself. */
6572 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6574 return FAIL;
6575 return OK;
6576}
6577
6578/*
6579 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6580 * Return FAIL when out of memory.
6581 */
6582 static int
6583list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *l1;
6585 list_T *l2;
6586 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587{
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006590 if (l1 == NULL || l2 == NULL)
6591 return FAIL;
6592
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 if (l == NULL)
6596 return FAIL;
6597 tv->v_type = VAR_LIST;
6598 tv->vval.v_list = l;
6599
6600 /* append all items from the second list */
6601 return list_extend(l, l2, NULL);
6602}
6603
6604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 * Returns NULL when out of memory.
6609 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006669vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 list_T *l;
6671 listitem_T *item;
6672 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 /* notify watchers */
6677 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006679 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 list_fix_watch(l, ip);
6681 if (ip == item2)
6682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684
6685 if (item2->li_next == NULL)
6686 l->lv_last = item->li_prev;
6687 else
6688 item2->li_next->li_prev = item->li_prev;
6689 if (item->li_prev == NULL)
6690 l->lv_first = item2->li_next;
6691 else
6692 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694}
6695
6696/*
6697 * Return an allocated string with the string representation of a list.
6698 * May return NULL.
6699 */
6700 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006703 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
6705 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706
6707 if (tv->vval.v_list == NULL)
6708 return NULL;
6709 ga_init2(&ga, (int)sizeof(char), 80);
6710 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 vim_free(ga.ga_data);
6714 return NULL;
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 ga_append(&ga, ']');
6717 ga_append(&ga, NUL);
6718 return (char_u *)ga.ga_data;
6719}
6720
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721typedef struct join_S {
6722 char_u *s;
6723 char_u *tofree;
6724} join_T;
6725
6726 static int
6727list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6728 garray_T *gap; /* to store the result in */
6729 list_T *l;
6730 char_u *sep;
6731 int echo_style;
6732 int copyID;
6733 garray_T *join_gap; /* to keep each list item string */
6734{
6735 int i;
6736 join_T *p;
6737 int len;
6738 int sumlen = 0;
6739 int first = TRUE;
6740 char_u *tofree;
6741 char_u numbuf[NUMBUFLEN];
6742 listitem_T *item;
6743 char_u *s;
6744
6745 /* Stringify each item in the list. */
6746 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6747 {
6748 if (echo_style)
6749 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6750 else
6751 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6752 if (s == NULL)
6753 return FAIL;
6754
6755 len = (int)STRLEN(s);
6756 sumlen += len;
6757
Bram Moolenaarcde88542015-08-11 19:14:00 +02006758 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6760 if (tofree != NULL || s != numbuf)
6761 {
6762 p->s = s;
6763 p->tofree = tofree;
6764 }
6765 else
6766 {
6767 p->s = vim_strnsave(s, len);
6768 p->tofree = p->s;
6769 }
6770
6771 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006772 if (did_echo_string_emsg) /* recursion error, bail out */
6773 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 }
6775
6776 /* Allocate result buffer with its total size, avoid re-allocation and
6777 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6778 if (join_gap->ga_len >= 2)
6779 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6780 if (ga_grow(gap, sumlen + 2) == FAIL)
6781 return FAIL;
6782
6783 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6784 {
6785 if (first)
6786 first = FALSE;
6787 else
6788 ga_concat(gap, sep);
6789 p = ((join_T *)join_gap->ga_data) + i;
6790
6791 if (p->s != NULL)
6792 ga_concat(gap, p->s);
6793 line_breakcheck();
6794 }
6795
6796 return OK;
6797}
6798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006801 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006804 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006805list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006809 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006810 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 garray_T join_ga;
6813 int retval;
6814 join_T *p;
6815 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816
Bram Moolenaard39a7512015-04-16 22:51:22 +02006817 if (l->lv_len < 1)
6818 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6820 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6821
6822 /* Dispose each item in join_ga. */
6823 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = (join_T *)join_ga.ga_data;
6826 for (i = 0; i < join_ga.ga_len; ++i)
6827 {
6828 vim_free(p->tofree);
6829 ++p;
6830 }
6831 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006833
6834 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835}
6836
6837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 * Garbage collection for lists and dictionaries.
6839 *
6840 * We use reference counts to be able to free most items right away when they
6841 * are no longer used. But for composite items it's possible that it becomes
6842 * unused while the reference count is > 0: When there is a recursive
6843 * reference. Example:
6844 * :let l = [1, 2, 3]
6845 * :let d = {9: l}
6846 * :let l[1] = d
6847 *
6848 * Since this is quite unusual we handle this with garbage collection: every
6849 * once in a while find out which lists and dicts are not referenced from any
6850 * variable.
6851 *
6852 * Here is a good reference text about garbage collection (refers to Python
6853 * but it applies to all reference-counting mechanisms):
6854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856
6857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 * Do garbage collection for lists and dicts.
6859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 int
6862garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 buf_T *buf;
6867 win_T *wp;
6868 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006869 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006870 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006872#ifdef FEAT_WINDOWS
6873 tabpage_T *tp;
6874#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876 /* Only do this once. */
6877 want_garbage_collect = FALSE;
6878 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006879 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 /* We advance by two because we add one for items referenced through
6882 * previous_funccal. */
6883 current_copyID += COPYID_INC;
6884 copyID = current_copyID;
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /*
6887 * 1. Go through all accessible variables and mark all lists and dicts
6888 * with copyID.
6889 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
6891 /* Don't free variables in the previous_funccal list unless they are only
6892 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006893 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6895 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6897 NULL);
6898 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6899 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 }
6901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 /* script-local variables */
6903 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* buffer-local variables */
6907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006915#ifdef FEAT_AUTOCMD
6916 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006919#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006921#ifdef FEAT_WINDOWS
6922 /* tabpage-local variables */
6923 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6925 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#endif
6927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930
6931 /* function-local variables */
6932 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6935 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 }
6937
Bram Moolenaard812df62008-11-09 12:46:09 +00006938 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006940
Bram Moolenaar1dced572012-04-05 16:54:08 +02006941#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006943#endif
6944
Bram Moolenaardb913952012-06-29 12:54:53 +02006945#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006947#endif
6948
6949#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006951#endif
6952
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 /*
6956 * 2. Free lists and dictionaries that are not referenced.
6957 */
6958 did_free = free_unref_items(copyID);
6959
6960 /*
6961 * 3. Check if any funccal can be freed now.
6962 */
6963 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (can_free_funccal(*pfc, copyID))
6966 {
6967 fc = *pfc;
6968 *pfc = fc->caller;
6969 free_funccal(fc, TRUE);
6970 did_free = TRUE;
6971 did_free_funccal = TRUE;
6972 }
6973 else
6974 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 if (did_free_funccal)
6977 /* When a funccal was freed some more items might be garbage
6978 * collected, so run again. */
6979 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 else if (p_verbose > 0)
6982 {
6983 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6984 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985
6986 return did_free;
6987}
6988
6989/*
6990 * Free lists and dictionaries that are no longer referenced.
6991 */
6992 static int
6993free_unref_items(copyID)
6994 int copyID;
6995{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 dict_T *dd, *dd_next;
6997 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 int did_free = FALSE;
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007008 /* Free the Dictionary and ordinary items it contains, but don't
7009 * recurse into Lists and Dictionaries, they will be in the list
7010 * of dicts or list of lists. */
7011 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dd = dd_next;
7015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 * Go through the list of lists and free items without the copyID.
7019 * But don't free a list that has a watcher (used in a for loop), these
7020 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
7022 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 {
7024 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7026 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the List and ordinary items it contains, but don't recurse
7029 * into Lists and Dictionaries, they will be in the list of dicts
7030 * or list of lists. */
7031 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 ll = ll_next;
7035 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 return did_free;
7037}
7038
7039/*
7040 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 * "list_stack" is used to add lists to be marked. Can be NULL.
7042 *
7043 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int
7046set_ref_in_ht(ht, copyID, list_stack)
7047 hashtab_T *ht;
7048 int copyID;
7049 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050{
7051 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 hashtab_T *cur_ht;
7055 ht_stack_T *ht_stack = NULL;
7056 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_ht = ht;
7059 for (;;)
7060 {
7061 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 /* Mark each item in the hashtab. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 todo = (int)cur_ht->ht_used;
7067 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7068 if (!HASHITEM_EMPTY(hi))
7069 {
7070 --todo;
7071 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7072 &ht_stack, list_stack);
7073 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075
7076 if (ht_stack == NULL)
7077 break;
7078
7079 /* take an item from the stack */
7080 cur_ht = ht_stack->ht;
7081 tempitem = ht_stack;
7082 ht_stack = ht_stack->prev;
7083 free(tempitem);
7084 }
7085
7086 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087}
7088
7089/*
7090 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7092 *
7093 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int
7096set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097 list_T *l;
7098 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 listitem_T *li;
7102 int abort = FALSE;
7103 list_T *cur_l;
7104 list_stack_T *list_stack = NULL;
7105 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 cur_l = l;
7108 for (;;)
7109 {
7110 if (!abort)
7111 /* Mark each item in the list. If the item contains a hashtab
7112 * it is added to ht_stack, if it contains a list it is added to
7113 * list_stack. */
7114 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7115 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7116 ht_stack, &list_stack);
7117 if (list_stack == NULL)
7118 break;
7119
7120 /* take an item from the stack */
7121 cur_l = list_stack->list;
7122 tempitem = list_stack;
7123 list_stack = list_stack->prev;
7124 free(tempitem);
7125 }
7126
7127 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128}
7129
7130/*
7131 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 * "list_stack" is used to add lists to be marked. Can be NULL.
7133 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7134 *
7135 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 int
7138set_ref_in_item(tv, copyID, ht_stack, list_stack)
7139 typval_T *tv;
7140 int copyID;
7141 ht_stack_T **ht_stack;
7142 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143{
7144 dict_T *dd;
7145 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147
7148 switch (tv->v_type)
7149 {
7150 case VAR_DICT:
7151 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007152 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007154 /* Didn't see this dict yet. */
7155 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 if (ht_stack == NULL)
7157 {
7158 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7159 }
7160 else
7161 {
7162 ht_stack_T *newitem = (ht_stack_T*)malloc(
7163 sizeof(ht_stack_T));
7164 if (newitem == NULL)
7165 abort = TRUE;
7166 else
7167 {
7168 newitem->ht = &dd->dv_hashtab;
7169 newitem->prev = *ht_stack;
7170 *ht_stack = newitem;
7171 }
7172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175
7176 case VAR_LIST:
7177 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007178 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 /* Didn't see this list yet. */
7181 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 if (list_stack == NULL)
7183 {
7184 abort = set_ref_in_list(ll, copyID, ht_stack);
7185 }
7186 else
7187 {
7188 list_stack_T *newitem = (list_stack_T*)malloc(
7189 sizeof(list_stack_T));
7190 if (newitem == NULL)
7191 abort = TRUE;
7192 else
7193 {
7194 newitem->list = ll;
7195 newitem->prev = *list_stack;
7196 *list_stack = newitem;
7197 }
7198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209dict_alloc()
7210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
7236 static int
7237rettv_dict_alloc(rettv)
7238 typval_T *rettv;
7239{
7240 dict_T *d = dict_alloc();
7241
7242 if (d == NULL)
7243 return FAIL;
7244
7245 rettv->vval.v_dict = d;
7246 rettv->v_type = VAR_DICT;
7247 ++d->dv_refcount;
7248 return OK;
7249}
7250
7251
7252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 * Unreference a Dictionary: decrement the reference count and free it when it
7254 * becomes zero.
7255 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007256 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 if (d != NULL && --d->dv_refcount <= 0)
7261 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262}
7263
7264/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007265 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Ignores the reference count.
7267 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007268 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007269dict_free(d, recurse)
7270 dict_T *d;
7271 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277 /* Remove the dict from the list of dicts for garbage collection. */
7278 if (d->dv_used_prev == NULL)
7279 first_dict = d->dv_used_next;
7280 else
7281 d->dv_used_prev->dv_used_next = d->dv_used_next;
7282 if (d->dv_used_next != NULL)
7283 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7284
7285 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 if (!HASHITEM_EMPTY(hi))
7291 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 /* Remove the item before deleting it, just in case there is
7293 * something recursive causing trouble. */
7294 di = HI2DI(hi);
7295 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296 if (recurse || (di->di_tv.v_type != VAR_LIST
7297 && di->di_tv.v_type != VAR_DICT))
7298 clear_tv(&di->di_tv);
7299 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 --todo;
7301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 vim_free(d);
7305}
7306
7307/*
7308 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 * The "key" is copied to the new item.
7310 * Note that the value of the item "di_tv" still needs to be initialized!
7311 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007313 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314dictitem_alloc(key)
7315 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326}
7327
7328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Make a copy of a Dictionary item.
7330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007337 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7338 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (di != NULL)
7340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007342 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 copy_tv(&org->di_tv, &di->di_tv);
7344 }
7345 return di;
7346}
7347
7348/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 * Remove item "item" from Dictionary "dict" and free it.
7350 */
7351 static void
7352dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 dict_T *dict;
7354 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (HASHITEM_EMPTY(hi))
7360 EMSG2(_(e_intern2), "dictitem_remove()");
7361 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 dictitem_free(item);
7364}
7365
7366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 * Free a dict item. Also clears the value.
7368 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007369 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007374 if (item->di_flags & DI_FLAGS_ALLOC)
7375 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376}
7377
7378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7380 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 * Returns NULL when out of memory.
7383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389{
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *copy;
7391 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 if (orig == NULL)
7396 return NULL;
7397
7398 copy = dict_alloc();
7399 if (copy != NULL)
7400 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 if (copyID != 0)
7402 {
7403 orig->dv_copyID = copyID;
7404 orig->dv_copydict = copy;
7405 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 di = dictitem_alloc(hi->hi_key);
7414 if (di == NULL)
7415 break;
7416 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 {
7418 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7419 copyID) == FAIL)
7420 {
7421 vim_free(di);
7422 break;
7423 }
7424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 else
7426 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7427 if (dict_add(copy, di) == FAIL)
7428 {
7429 dictitem_free(di);
7430 break;
7431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 if (todo > 0)
7437 {
7438 dict_unref(copy);
7439 copy = NULL;
7440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441 }
7442
7443 return copy;
7444}
7445
7446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007450 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dict_T *d;
7453 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454{
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456}
7457
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007459 * Add a number or string entry to dictionary "d".
7460 * When "str" is NULL use number "nr", otherwise use "str".
7461 * Returns FAIL when out of memory and when key already exists.
7462 */
7463 int
7464dict_add_nr_str(d, key, nr, str)
7465 dict_T *d;
7466 char *key;
7467 long nr;
7468 char_u *str;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 if (str == NULL)
7477 {
7478 item->di_tv.v_type = VAR_NUMBER;
7479 item->di_tv.vval.v_number = nr;
7480 }
7481 else
7482 {
7483 item->di_tv.v_type = VAR_STRING;
7484 item->di_tv.vval.v_string = vim_strsave(str);
7485 }
7486 if (dict_add(d, item) == FAIL)
7487 {
7488 dictitem_free(item);
7489 return FAIL;
7490 }
7491 return OK;
7492}
7493
7494/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007495 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007496 * Returns FAIL when out of memory and when key already exists.
7497 */
7498 int
7499dict_add_list(d, key, list)
7500 dict_T *d;
7501 char *key;
7502 list_T *list;
7503{
7504 dictitem_T *item;
7505
7506 item = dictitem_alloc((char_u *)key);
7507 if (item == NULL)
7508 return FAIL;
7509 item->di_tv.v_lock = 0;
7510 item->di_tv.v_type = VAR_LIST;
7511 item->di_tv.vval.v_list = list;
7512 if (dict_add(d, item) == FAIL)
7513 {
7514 dictitem_free(item);
7515 return FAIL;
7516 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007517 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007518 return OK;
7519}
7520
7521/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 * Get the number of items in a Dictionary.
7523 */
7524 static long
7525dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528 if (d == NULL)
7529 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531}
7532
7533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 * Find item "key[len]" in Dictionary "d".
7535 * If "len" is negative use strlen(key).
7536 * Returns NULL when not found.
7537 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007538 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 char_u *key;
7542 int len;
7543{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544#define AKEYLEN 200
7545 char_u buf[AKEYLEN];
7546 char_u *akey;
7547 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 if (len < 0)
7551 akey = key;
7552 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 tofree = akey = vim_strnsave(key, len);
7555 if (akey == NULL)
7556 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 else
7559 {
7560 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007561 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 akey = buf;
7563 }
7564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 vim_free(tofree);
7567 if (HASHITEM_EMPTY(hi))
7568 return NULL;
7569 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570}
7571
7572/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 * Get a string item from a dictionary.
7574 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575 * Returns NULL if the entry doesn't exist or out of memory.
7576 */
7577 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579 dict_T *d;
7580 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007581 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582{
7583 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007584 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585
7586 di = dict_find(d, key, -1);
7587 if (di == NULL)
7588 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007589 s = get_tv_string(&di->di_tv);
7590 if (save && s != NULL)
7591 s = vim_strsave(s);
7592 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007593}
7594
7595/*
7596 * Get a number item from a dictionary.
7597 * Returns 0 if the entry doesn't exist or out of memory.
7598 */
7599 long
7600get_dict_number(d, key)
7601 dict_T *d;
7602 char_u *key;
7603{
7604 dictitem_T *di;
7605
7606 di = dict_find(d, key, -1);
7607 if (di == NULL)
7608 return 0;
7609 return get_tv_number(&di->di_tv);
7610}
7611
7612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 * Return an allocated string with the string representation of a Dictionary.
7614 * May return NULL.
7615 */
7616 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007617dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
7680get_dict_tv(arg, rettv, evaluate)
7681 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 int evaluate;
7684{
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dict_T *d = NULL;
7686 typval_T tvkey;
7687 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007688 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 /*
7694 * First check if it's not a curly-braces thing: {expr}.
7695 * Must do this without evaluating, otherwise a function may be called
7696 * twice. Unfortunately this means we need to call eval1() twice for the
7697 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 if (*start != '}')
7701 {
7702 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7703 return FAIL;
7704 if (*start == '}')
7705 return NOTDONE;
7706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707
7708 if (evaluate)
7709 {
7710 d = dict_alloc();
7711 if (d == NULL)
7712 return FAIL;
7713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 tvkey.v_type = VAR_UNKNOWN;
7715 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 *arg = skipwhite(*arg + 1);
7718 while (**arg != '}' && **arg != NUL)
7719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 if (**arg != ':')
7723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007724 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 goto failret;
7727 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007730 key = get_tv_string_buf_chk(&tvkey, buf);
7731 if (key == NULL || *key == NUL)
7732 {
7733 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7734 if (key != NULL)
7735 EMSG(_(e_emptykey));
7736 clear_tv(&tvkey);
7737 goto failret;
7738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7743 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007744 if (evaluate)
7745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 goto failret;
7747 }
7748 if (evaluate)
7749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 if (item != NULL)
7752 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007753 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 clear_tv(&tv);
7756 goto failret;
7757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 item = dictitem_alloc(key);
7759 clear_tv(&tvkey);
7760 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007763 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 if (dict_add(d, item) == FAIL)
7765 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 }
7767 }
7768
7769 if (**arg == '}')
7770 break;
7771 if (**arg != ',')
7772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007773 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 goto failret;
7775 }
7776 *arg = skipwhite(*arg + 1);
7777 }
7778
7779 if (**arg != '}')
7780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007781 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782failret:
7783 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007784 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 return FAIL;
7786 }
7787
7788 *arg = skipwhite(*arg + 1);
7789 if (evaluate)
7790 {
7791 rettv->v_type = VAR_DICT;
7792 rettv->vval.v_dict = d;
7793 ++d->dv_refcount;
7794 }
7795
7796 return OK;
7797}
7798
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 * Return a string with the string representation of a variable.
7801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007803 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007805 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 */
7807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 static int recurse = 0;
7815 char_u *r = NULL;
7816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007819 if (!did_echo_string_emsg)
7820 {
7821 /* Only give this message once for a recursive call to avoid
7822 * flooding the user with errors. And stop iterating over lists
7823 * and dicts. */
7824 did_echo_string_emsg = TRUE;
7825 EMSG(_("E724: variable nested too deep for displaying"));
7826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007828 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 }
7830 ++recurse;
7831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 switch (tv->v_type)
7833 {
7834 case VAR_FUNC:
7835 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 r = tv->vval.v_string;
7837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_list == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"[...]";
7849 }
7850 else
7851 {
7852 tv->vval.v_list->lv_copyID = copyID;
7853 *tofree = list2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859 if (tv->vval.v_dict == NULL)
7860 {
7861 *tofree = NULL;
7862 r = NULL;
7863 }
7864 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7865 {
7866 *tofree = NULL;
7867 r = (char_u *)"{...}";
7868 }
7869 else
7870 {
7871 tv->vval.v_dict->dv_copyID = copyID;
7872 *tofree = dict2string(tv, copyID);
7873 r = *tofree;
7874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 case VAR_STRING:
7878 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 *tofree = NULL;
7880 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#ifdef FEAT_FLOAT
7884 case VAR_FLOAT:
7885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7887 r = numbuf;
7888 break;
7889#endif
7890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895
Bram Moolenaar8502c702014-06-17 12:51:16 +02007896 if (--recurse == 0)
7897 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899}
7900
7901/*
7902 * Return a string with the string representation of a variable.
7903 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7904 * "numbuf" is used for a number.
7905 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007906 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 */
7908 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 char_u **tofree;
7912 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007913 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914{
7915 switch (tv->v_type)
7916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 case VAR_FUNC:
7918 *tofree = string_quote(tv->vval.v_string, TRUE);
7919 return *tofree;
7920 case VAR_STRING:
7921 *tofree = string_quote(tv->vval.v_string, FALSE);
7922 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923#ifdef FEAT_FLOAT
7924 case VAR_FLOAT:
7925 *tofree = NULL;
7926 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7927 return numbuf;
7928#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937}
7938
7939/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 * Return string "str" in ' quotes, doubling ' characters.
7941 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 */
7944 static char_u *
7945string_quote(str, function)
7946 char_u *str;
7947 int function;
7948{
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 char_u *p, *r, *s;
7951
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 len = (function ? 13 : 3);
7953 if (str != NULL)
7954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 for (p = str; *p != NUL; mb_ptr_adv(p))
7957 if (*p == '\'')
7958 ++len;
7959 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 s = r = alloc(len);
7961 if (r != NULL)
7962 {
7963 if (function)
7964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 r += 10;
7967 }
7968 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 if (str != NULL)
7971 for (p = str; *p != NUL; )
7972 {
7973 if (*p == '\'')
7974 *r++ = '\'';
7975 MB_COPY_CHAR(p, r);
7976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 if (function)
7979 *r++ = ')';
7980 *r++ = NUL;
7981 }
7982 return s;
7983}
7984
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986/*
7987 * Convert the string "text" to a floating point number.
7988 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7989 * this always uses a decimal point.
7990 * Returns the length of the text that was consumed.
7991 */
7992 static int
7993string2float(text, value)
7994 char_u *text;
7995 float_T *value; /* result stored here */
7996{
7997 char *s = (char *)text;
7998 float_T f;
7999
8000 f = strtod(s, &s);
8001 *value = f;
8002 return (int)((char_u *)s - text);
8003}
8004#endif
8005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * Get the value of an environment variable.
8008 * "arg" is pointing to the '$'. It is advanced to after the name.
8009 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
8012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int evaluate;
8017{
8018 char_u *string = NULL;
8019 int len;
8020 int cc;
8021 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 ++*arg;
8025 name = *arg;
8026 len = get_env_len(arg);
8027 if (evaluate)
8028 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008030 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 cc = name[len];
8033 name[len] = NUL;
8034 /* first try vim_getenv(), fast for normal environment vars */
8035 string = vim_getenv(name, &mustfree);
8036 if (string != NULL && *string != NUL)
8037 {
8038 if (!mustfree)
8039 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 else
8042 {
8043 if (mustfree)
8044 vim_free(string);
8045
8046 /* next try expanding things like $VIM and ${HOME} */
8047 string = expand_env_save(name - 1);
8048 if (string != NULL && *string == '$')
8049 {
8050 vim_free(string);
8051 string = NULL;
8052 }
8053 }
8054 name[len] = cc;
8055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059
8060 return OK;
8061}
8062
8063/*
8064 * Array with names and number of arguments of all internal functions
8065 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8066 */
8067static struct fst
8068{
8069 char *f_name; /* function name */
8070 char f_min_argc; /* minimal number of arguments */
8071 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008073 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074} functions[] =
8075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008080 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008081 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"append", 2, 2, f_append},
8084 {"argc", 0, 0, f_argc},
8085 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008086 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008087 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008088#ifdef FEAT_FLOAT
8089 {"asin", 1, 1, f_asin}, /* WJMc */
8090#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008091 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008092 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008093 {"assert_false", 1, 2, f_assert_false},
8094 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#ifdef FEAT_FLOAT
8096 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008097 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"bufexists", 1, 1, f_bufexists},
8102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8105 {"buflisted", 1, 1, f_buflisted},
8106 {"bufloaded", 1, 1, f_bufloaded},
8107 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008108 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufwinnr", 1, 1, f_bufwinnr},
8110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008112 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"ceil", 1, 1, f_ceil},
8116#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008117 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008118 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008120 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008122#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008123 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008124 {"complete_add", 1, 1, f_complete_add},
8125 {"complete_check", 0, 0, f_complete_check},
8126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008131 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008133 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008135 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008136 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"delete", 1, 1, f_delete},
8138 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008139 {"diff_filler", 1, 1, f_diff_filler},
8140 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008141 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008143 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"eventhandler", 0, 0, f_eventhandler},
8145 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008146 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148#ifdef FEAT_FLOAT
8149 {"exp", 1, 1, f_exp},
8150#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008151 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008152 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008153 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8155 {"filereadable", 1, 1, f_filereadable},
8156 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008157 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008158 {"finddir", 1, 3, f_finddir},
8159 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#ifdef FEAT_FLOAT
8161 {"float2nr", 1, 1, f_float2nr},
8162 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008163 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008165 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"fnamemodify", 2, 2, f_fnamemodify},
8167 {"foldclosed", 1, 1, f_foldclosed},
8168 {"foldclosedend", 1, 1, f_foldclosedend},
8169 {"foldlevel", 1, 1, f_foldlevel},
8170 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008171 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008174 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008175 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008176 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"getchar", 0, 1, f_getchar},
8179 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008180 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"getcmdline", 0, 0, f_getcmdline},
8182 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008183 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008184 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008185 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008187 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008188 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"getfsize", 1, 1, f_getfsize},
8190 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008191 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008193 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008194 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008195 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008196 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008197 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008198 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008200 {"gettabvar", 2, 3, f_gettabvar},
8201 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"getwinposx", 0, 0, f_getwinposx},
8203 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008204 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008205 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008206 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008207 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008209 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008210 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008211 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8213 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8214 {"histadd", 2, 2, f_histadd},
8215 {"histdel", 1, 2, f_histdel},
8216 {"histget", 1, 2, f_histget},
8217 {"histnr", 1, 1, f_histnr},
8218 {"hlID", 1, 1, f_hlID},
8219 {"hlexists", 1, 1, f_hlexists},
8220 {"hostname", 0, 0, f_hostname},
8221 {"iconv", 3, 3, f_iconv},
8222 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008224 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008226 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"inputrestore", 0, 0, f_inputrestore},
8228 {"inputsave", 0, 0, f_inputsave},
8229 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008231 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008233 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008236 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"libcall", 3, 3, f_libcall},
8240 {"libcallnr", 3, 3, f_libcallnr},
8241 {"line", 1, 1, f_line},
8242 {"line2byte", 1, 1, f_line2byte},
8243 {"lispindent", 1, 1, f_lispindent},
8244 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008246 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247 {"log10", 1, 1, f_log10},
8248#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008249#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008250 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008251#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008253 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008254 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008255 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008256 {"matchadd", 2, 5, f_matchadd},
8257 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008258 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008259 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008260 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008261 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008262 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008263 {"max", 1, 1, f_max},
8264 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008265#ifdef vim_mkdir
8266 {"mkdir", 1, 3, f_mkdir},
8267#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008269#ifdef FEAT_MZSCHEME
8270 {"mzeval", 1, 1, f_mzeval},
8271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008273 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008274 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008275 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276#ifdef FEAT_FLOAT
8277 {"pow", 2, 2, f_pow},
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008280 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008281 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008282#ifdef FEAT_PYTHON3
8283 {"py3eval", 1, 1, f_py3eval},
8284#endif
8285#ifdef FEAT_PYTHON
8286 {"pyeval", 1, 1, f_pyeval},
8287#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008288 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008289 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008290 {"reltime", 0, 2, f_reltime},
8291 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"remote_expr", 2, 3, f_remote_expr},
8293 {"remote_foreground", 1, 1, f_remote_foreground},
8294 {"remote_peek", 1, 2, f_remote_peek},
8295 {"remote_read", 1, 1, f_remote_read},
8296 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008297 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008299 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302#ifdef FEAT_FLOAT
8303 {"round", 1, 1, f_round},
8304#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008305 {"screenattr", 2, 2, f_screenattr},
8306 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008307 {"screencol", 0, 0, f_screencol},
8308 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008309 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008310 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008311 {"searchpair", 3, 7, f_searchpair},
8312 {"searchpairpos", 3, 7, f_searchpairpos},
8313 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"server2client", 2, 2, f_server2client},
8315 {"serverlist", 0, 0, f_serverlist},
8316 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008317 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"setcmdpos", 1, 1, f_setcmdpos},
8319 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008320 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008321 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008322 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008323 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008325 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008326 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008328#ifdef FEAT_CRYPT
8329 {"sha256", 1, 1, f_sha256},
8330#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008331 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008332 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
8335 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008336 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008337#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008338 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008339 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008340 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008341 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008342 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008343#ifdef FEAT_FLOAT
8344 {"sqrt", 1, 1, f_sqrt},
8345 {"str2float", 1, 1, f_str2float},
8346#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008347 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008348 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008349 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350#ifdef HAVE_STRFTIME
8351 {"strftime", 1, 2, f_strftime},
8352#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"strlen", 1, 1, f_strlen},
8356 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008357 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008359 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008360 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"substitute", 4, 4, f_substitute},
8362 {"synID", 3, 3, f_synID},
8363 {"synIDattr", 2, 3, f_synIDattr},
8364 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008365 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008366 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008367 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008368 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008369 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008370 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008371 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008372 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008373 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374#ifdef FEAT_FLOAT
8375 {"tan", 1, 1, f_tan},
8376 {"tanh", 1, 1, f_tanh},
8377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008379 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"tolower", 1, 1, f_tolower},
8381 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008382 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#ifdef FEAT_FLOAT
8384 {"trunc", 1, 1, f_trunc},
8385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008387 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008388 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008389 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008390 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"virtcol", 1, 1, f_virtcol},
8392 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008393 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"winbufnr", 1, 1, f_winbufnr},
8395 {"wincol", 0, 0, f_wincol},
8396 {"winheight", 1, 1, f_winheight},
8397 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008398 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008400 {"winrestview", 1, 1, f_winrestview},
8401 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008403 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008404 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008405 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406};
8407
8408#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8409
8410/*
8411 * Function given to ExpandGeneric() to obtain the list of internal
8412 * or user defined function names.
8413 */
8414 char_u *
8415get_function_name(xp, idx)
8416 expand_T *xp;
8417 int idx;
8418{
8419 static int intidx = -1;
8420 char_u *name;
8421
8422 if (idx == 0)
8423 intidx = -1;
8424 if (intidx < 0)
8425 {
8426 name = get_user_func_name(xp, idx);
8427 if (name != NULL)
8428 return name;
8429 }
8430 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8431 {
8432 STRCPY(IObuff, functions[intidx].f_name);
8433 STRCAT(IObuff, "(");
8434 if (functions[intidx].f_max_argc == 0)
8435 STRCAT(IObuff, ")");
8436 return IObuff;
8437 }
8438
8439 return NULL;
8440}
8441
8442/*
8443 * Function given to ExpandGeneric() to obtain the list of internal or
8444 * user defined variable or function names.
8445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 char_u *
8447get_expr_name(xp, idx)
8448 expand_T *xp;
8449 int idx;
8450{
8451 static int intidx = -1;
8452 char_u *name;
8453
8454 if (idx == 0)
8455 intidx = -1;
8456 if (intidx < 0)
8457 {
8458 name = get_function_name(xp, idx);
8459 if (name != NULL)
8460 return name;
8461 }
8462 return get_user_var_name(xp, ++intidx);
8463}
8464
8465#endif /* FEAT_CMDL_COMPL */
8466
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008467#if defined(EBCDIC) || defined(PROTO)
8468/*
8469 * Compare struct fst by function name.
8470 */
8471 static int
8472compare_func_name(s1, s2)
8473 const void *s1;
8474 const void *s2;
8475{
8476 struct fst *p1 = (struct fst *)s1;
8477 struct fst *p2 = (struct fst *)s2;
8478
8479 return STRCMP(p1->f_name, p2->f_name);
8480}
8481
8482/*
8483 * Sort the function table by function name.
8484 * The sorting of the table above is ASCII dependant.
8485 * On machines using EBCDIC we have to sort it.
8486 */
8487 static void
8488sortFunctions()
8489{
8490 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8491
8492 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8493}
8494#endif
8495
8496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497/*
8498 * Find internal function in table above.
8499 * Return index, or -1 if not found
8500 */
8501 static int
8502find_internal_func(name)
8503 char_u *name; /* name of the function */
8504{
8505 int first = 0;
8506 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8507 int cmp;
8508 int x;
8509
8510 /*
8511 * Find the function name in the table. Binary search.
8512 */
8513 while (first <= last)
8514 {
8515 x = first + ((unsigned)(last - first) >> 1);
8516 cmp = STRCMP(name, functions[x].f_name);
8517 if (cmp < 0)
8518 last = x - 1;
8519 else if (cmp > 0)
8520 first = x + 1;
8521 else
8522 return x;
8523 }
8524 return -1;
8525}
8526
8527/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8529 * name it contains, otherwise return "name".
8530 */
8531 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008532deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 char_u *name;
8534 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008535 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536{
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 int cc;
8539
8540 cc = name[*lenp];
8541 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008542 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 {
8548 *lenp = 0;
8549 return (char_u *)""; /* just in case */
8550 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008551 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008552 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 }
8554
8555 return name;
8556}
8557
8558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 * Allocate a variable for the result of a function.
8560 * Return OK or FAIL.
8561 */
8562 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8564 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 char_u *name; /* name of the function */
8566 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 char_u **arg; /* argument, pointing to the '(' */
8569 linenr_T firstline; /* first line of range */
8570 linenr_T lastline; /* last line of range */
8571 int *doesrange; /* return: function handled range */
8572 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 char_u *argp;
8576 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008577 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 int argcount = 0; /* number of arguments found */
8579
8580 /*
8581 * Get the arguments.
8582 */
8583 argp = *arg;
8584 while (argcount < MAX_FUNC_ARGS)
8585 {
8586 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8587 if (*argp == ')' || *argp == ',' || *argp == NUL)
8588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8590 {
8591 ret = FAIL;
8592 break;
8593 }
8594 ++argcount;
8595 if (*argp != ',')
8596 break;
8597 }
8598 if (*argp == ')')
8599 ++argp;
8600 else
8601 ret = FAIL;
8602
8603 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 {
8608 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008609 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 *arg = skipwhite(argp);
8618 return ret;
8619}
8620
8621
8622/*
8623 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008624 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008625 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 */
8627 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008628call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008630 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008634 typval_T *argvars; /* vars for arguments, must have "argcount"
8635 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 linenr_T firstline; /* first line of range */
8637 linenr_T lastline; /* last line of range */
8638 int *doesrange; /* return: function handled range */
8639 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#define ERROR_UNKNOWN 0
8644#define ERROR_TOOMANY 1
8645#define ERROR_TOOFEW 2
8646#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647#define ERROR_DICT 4
8648#define ERROR_NONE 5
8649#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 int error = ERROR_NONE;
8651 int i;
8652 int llen;
8653 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#define FLEN_FIXED 40
8655 char_u fname_buf[FLEN_FIXED + 1];
8656 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008657 char_u *name;
8658
8659 /* Make a copy of the name, if it comes from a funcref variable it could
8660 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008661 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008662 if (name == NULL)
8663 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 /*
8666 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8667 * Change <SNR>123_name() to K_SNR 123_name().
8668 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 llen = eval_fname_script(name);
8671 if (llen > 0)
8672 {
8673 fname_buf[0] = K_SPECIAL;
8674 fname_buf[1] = KS_EXTRA;
8675 fname_buf[2] = (int)KE_SNR;
8676 i = 3;
8677 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8678 {
8679 if (current_SID <= 0)
8680 error = ERROR_SCRIPT;
8681 else
8682 {
8683 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8684 i = (int)STRLEN(fname_buf);
8685 }
8686 }
8687 if (i + STRLEN(name + llen) < FLEN_FIXED)
8688 {
8689 STRCPY(fname_buf + i, name + llen);
8690 fname = fname_buf;
8691 }
8692 else
8693 {
8694 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8695 if (fname == NULL)
8696 error = ERROR_OTHER;
8697 else
8698 {
8699 mch_memmove(fname, fname_buf, (size_t)i);
8700 STRCPY(fname + i, name + llen);
8701 }
8702 }
8703 }
8704 else
8705 fname = name;
8706
8707 *doesrange = FALSE;
8708
8709
8710 /* execute the function if no errors detected and executing */
8711 if (evaluate && error == ERROR_NONE)
8712 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 char_u *rfname = fname;
8714
8715 /* Ignore "g:" before a function name. */
8716 if (fname[0] == 'g' && fname[1] == ':')
8717 rfname = fname + 2;
8718
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008719 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8720 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 error = ERROR_UNKNOWN;
8722
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008723 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {
8725 /*
8726 * User defined function.
8727 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731 /* Trigger FuncUndefined event, may load the function. */
8732 if (fp == NULL
8733 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008734 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 }
8740#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008741 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743 {
8744 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008745 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008746 }
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (fp != NULL)
8749 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 else
8759 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008760 int did_save_redo = FALSE;
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 /*
8763 * Call the user function.
8764 * Save and restore search patterns, script variables and
8765 * redo buffer.
8766 */
8767 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008768#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008769 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008770#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008771 {
8772 saveRedobuff();
8773 did_save_redo = TRUE;
8774 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008777 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008778 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8779 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8780 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008781 /* Function was unreferenced while being used, free it
8782 * now. */
8783 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008784 if (did_save_redo)
8785 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 restore_search_patterns();
8787 error = ERROR_NONE;
8788 }
8789 }
8790 }
8791 else
8792 {
8793 /*
8794 * Find the function name in the table, call its implementation.
8795 */
8796 i = find_internal_func(fname);
8797 if (i >= 0)
8798 {
8799 if (argcount < functions[i].f_min_argc)
8800 error = ERROR_TOOFEW;
8801 else if (argcount > functions[i].f_max_argc)
8802 error = ERROR_TOOMANY;
8803 else
8804 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008805 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 error = ERROR_NONE;
8808 }
8809 }
8810 }
8811 /*
8812 * The function call (or "FuncUndefined" autocommand sequence) might
8813 * have been aborted by an error, an interrupt, or an explicitly thrown
8814 * exception that has not been caught so far. This situation can be
8815 * tested for by calling aborting(). For an error in an internal
8816 * function or for the "E132" error in call_user_func(), however, the
8817 * throw point at which the "force_abort" flag (temporarily reset by
8818 * emsg()) is normally updated has not been reached yet. We need to
8819 * update that flag first to make aborting() reliable.
8820 */
8821 update_force_abort();
8822 }
8823 if (error == ERROR_NONE)
8824 ret = OK;
8825
8826 /*
8827 * Report an error unless the argument evaluation or function call has been
8828 * cancelled due to an aborting error, an interrupt, or an exception.
8829 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 if (!aborting())
8831 {
8832 switch (error)
8833 {
8834 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 break;
8837 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008838 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008839 break;
8840 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008841 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008842 name);
8843 break;
8844 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008845 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 name);
8847 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 name);
8851 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008852 }
8853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (fname != name && fname != fname_buf)
8856 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008857 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859 return ret;
8860}
8861
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008862/*
8863 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008864 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 */
8866 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008867emsg_funcname(ermsg, name)
8868 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869 char_u *name;
8870{
8871 char_u *p;
8872
8873 if (*name == K_SPECIAL)
8874 p = concat_str((char_u *)"<SNR>", name + 3);
8875 else
8876 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008877 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008878 if (p != name)
8879 vim_free(p);
8880}
8881
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008882/*
8883 * Return TRUE for a non-zero Number and a non-empty String.
8884 */
8885 static int
8886non_zero_arg(argvars)
8887 typval_T *argvars;
8888{
8889 return ((argvars[0].v_type == VAR_NUMBER
8890 && argvars[0].vval.v_number != 0)
8891 || (argvars[0].v_type == VAR_STRING
8892 && argvars[0].vval.v_string != NULL
8893 && *argvars[0].vval.v_string != NUL));
8894}
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896/*********************************************
8897 * Implementation of the built-in functions
8898 */
8899
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008901static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8902
8903/*
8904 * Get the float value of "argvars[0]" into "f".
8905 * Returns FAIL when the argument is not a Number or Float.
8906 */
8907 static int
8908get_float_arg(argvars, f)
8909 typval_T *argvars;
8910 float_T *f;
8911{
8912 if (argvars[0].v_type == VAR_FLOAT)
8913 {
8914 *f = argvars[0].vval.v_float;
8915 return OK;
8916 }
8917 if (argvars[0].v_type == VAR_NUMBER)
8918 {
8919 *f = (float_T)argvars[0].vval.v_number;
8920 return OK;
8921 }
8922 EMSG(_("E808: Number or Float required"));
8923 return FAIL;
8924}
8925
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008926/*
8927 * "abs(expr)" function
8928 */
8929 static void
8930f_abs(argvars, rettv)
8931 typval_T *argvars;
8932 typval_T *rettv;
8933{
8934 if (argvars[0].v_type == VAR_FLOAT)
8935 {
8936 rettv->v_type = VAR_FLOAT;
8937 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8938 }
8939 else
8940 {
8941 varnumber_T n;
8942 int error = FALSE;
8943
8944 n = get_tv_number_chk(&argvars[0], &error);
8945 if (error)
8946 rettv->vval.v_number = -1;
8947 else if (n > 0)
8948 rettv->vval.v_number = n;
8949 else
8950 rettv->vval.v_number = -n;
8951 }
8952}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008953
8954/*
8955 * "acos()" function
8956 */
8957 static void
8958f_acos(argvars, rettv)
8959 typval_T *argvars;
8960 typval_T *rettv;
8961{
8962 float_T f;
8963
8964 rettv->v_type = VAR_FLOAT;
8965 if (get_float_arg(argvars, &f) == OK)
8966 rettv->vval.v_float = acos(f);
8967 else
8968 rettv->vval.v_float = 0.0;
8969}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008970#endif
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 */
8975 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008976f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 typval_T *argvars;
8978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008985 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008986 && !tv_check_lock(l->lv_lock,
8987 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008988 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 }
8991 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 EMSG(_(e_listreq));
8993}
8994
8995/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008996 * "alloc_fail(id, countdown, repeat)" function
8997 */
8998 static void
8999f_alloc_fail(argvars, rettv)
9000 typval_T *argvars;
9001 typval_T *rettv UNUSED;
9002{
9003 if (argvars[0].v_type != VAR_NUMBER
9004 || argvars[0].vval.v_number <= 0
9005 || argvars[1].v_type != VAR_NUMBER
9006 || argvars[1].vval.v_number < 0
9007 || argvars[2].v_type != VAR_NUMBER)
9008 EMSG(_(e_invarg));
9009 else
9010 {
9011 alloc_fail_id = argvars[0].vval.v_number;
9012 alloc_fail_countdown = argvars[1].vval.v_number;
9013 alloc_fail_repeat = argvars[2].vval.v_number;
9014 }
9015}
9016
9017/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009018 * "and(expr, expr)" function
9019 */
9020 static void
9021f_and(argvars, rettv)
9022 typval_T *argvars;
9023 typval_T *rettv;
9024{
9025 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9026 & get_tv_number_chk(&argvars[1], NULL);
9027}
9028
9029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030 * "append(lnum, string/list)" function
9031 */
9032 static void
9033f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 typval_T *argvars;
9035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036{
9037 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 list_T *l = NULL;
9040 listitem_T *li = NULL;
9041 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 long added = 0;
9043
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009044 /* When coming here from Insert mode, sync undo, so that this can be
9045 * undone separately from what was previously inserted. */
9046 if (u_sync_once == 2)
9047 {
9048 u_sync_once = 1; /* notify that u_sync() was called */
9049 u_sync(TRUE);
9050 }
9051
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 lnum = get_tv_lnum(argvars);
9053 if (lnum >= 0
9054 && lnum <= curbuf->b_ml.ml_line_count
9055 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 l = argvars[1].vval.v_list;
9060 if (l == NULL)
9061 return;
9062 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 for (;;)
9065 {
9066 if (l == NULL)
9067 tv = &argvars[1]; /* append a string */
9068 else if (li == NULL)
9069 break; /* end of list */
9070 else
9071 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 line = get_tv_string_chk(tv);
9073 if (line == NULL) /* type error */
9074 {
9075 rettv->vval.v_number = 1; /* Failed */
9076 break;
9077 }
9078 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 ++added;
9080 if (l == NULL)
9081 break;
9082 li = li->li_next;
9083 }
9084
9085 appended_lines_mark(lnum, added);
9086 if (curwin->w_cursor.lnum > lnum)
9087 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 else
9090 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093/*
9094 * "argc()" function
9095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "argidx()" function
9106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009116 * "arglistid()" function
9117 */
9118 static void
9119f_arglistid(argvars, rettv)
9120 typval_T *argvars UNUSED;
9121 typval_T *rettv;
9122{
9123 win_T *wp;
9124 tabpage_T *tp = NULL;
9125 long n;
9126
9127 rettv->vval.v_number = -1;
9128 if (argvars[0].v_type != VAR_UNKNOWN)
9129 {
9130 if (argvars[1].v_type != VAR_UNKNOWN)
9131 {
9132 n = get_tv_number(&argvars[1]);
9133 if (n >= 0)
9134 tp = find_tabpage(n);
9135 }
9136 else
9137 tp = curtab;
9138
9139 if (tp != NULL)
9140 {
9141 wp = find_win_by_nr(&argvars[0], tp);
9142 if (wp != NULL)
9143 rettv->vval.v_number = wp->w_alist->id;
9144 }
9145 }
9146 else
9147 rettv->vval.v_number = curwin->w_alist->id;
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * "argv(nr)" function
9152 */
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 int idx;
9159
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009160 if (argvars[0].v_type != VAR_UNKNOWN)
9161 {
9162 idx = get_tv_number_chk(&argvars[0], NULL);
9163 if (idx >= 0 && idx < ARGCOUNT)
9164 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9165 else
9166 rettv->vval.v_string = NULL;
9167 rettv->v_type = VAR_STRING;
9168 }
9169 else if (rettv_list_alloc(rettv) == OK)
9170 for (idx = 0; idx < ARGCOUNT; ++idx)
9171 list_append_string(rettv->vval.v_list,
9172 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009175static void prepare_assert_error __ARGS((garray_T*gap));
9176static 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));
9177static void assert_error __ARGS((garray_T *gap));
9178static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9179
9180/*
9181 * Prepare "gap" for an assert error and add the sourcing position.
9182 */
9183 static void
9184prepare_assert_error(gap)
9185 garray_T *gap;
9186{
9187 char buf[NUMBUFLEN];
9188
9189 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009190 if (sourcing_name != NULL)
9191 {
9192 ga_concat(gap, sourcing_name);
9193 if (sourcing_lnum > 0)
9194 ga_concat(gap, (char_u *)" ");
9195 }
9196 if (sourcing_lnum > 0)
9197 {
9198 sprintf(buf, "line %ld", (long)sourcing_lnum);
9199 ga_concat(gap, (char_u *)buf);
9200 }
9201 if (sourcing_name != NULL || sourcing_lnum > 0)
9202 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009203}
9204
9205/*
9206 * Fill "gap" with information about an assert error.
9207 */
9208 static void
9209fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9210 garray_T *gap;
9211 typval_T *opt_msg_tv;
9212 char_u *exp_str;
9213 typval_T *exp_tv;
9214 typval_T *got_tv;
9215{
9216 char_u numbuf[NUMBUFLEN];
9217 char_u *tofree;
9218
9219 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9220 {
9221 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9222 vim_free(tofree);
9223 }
9224 else
9225 {
9226 ga_concat(gap, (char_u *)"Expected ");
9227 if (exp_str == NULL)
9228 {
9229 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9230 vim_free(tofree);
9231 }
9232 else
9233 ga_concat(gap, exp_str);
9234 ga_concat(gap, (char_u *)" but got ");
9235 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9236 vim_free(tofree);
9237 }
9238}
Bram Moolenaar43345542015-11-29 17:35:35 +01009239
9240/*
9241 * Add an assert error to v:errors.
9242 */
9243 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 garray_T *gap;
9246{
9247 struct vimvar *vp = &vimvars[VV_ERRORS];
9248
9249 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9250 /* Make sure v:errors is a list. */
9251 set_vim_var_list(VV_ERRORS, list_alloc());
9252 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9253}
9254
Bram Moolenaar43345542015-11-29 17:35:35 +01009255/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009257 */
9258 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009260 typval_T *argvars;
9261 typval_T *rettv UNUSED;
9262{
9263 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009264
9265 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9266 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267 prepare_assert_error(&ga);
9268 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9269 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009270 ga_clear(&ga);
9271 }
9272}
9273
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009274/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009275 * "assert_exception(string[, msg])" function
9276 */
9277 static void
9278f_assert_exception(argvars, rettv)
9279 typval_T *argvars;
9280 typval_T *rettv UNUSED;
9281{
9282 garray_T ga;
9283 char *error;
9284
9285 error = (char *)get_tv_string_chk(&argvars[0]);
9286 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9287 {
9288 prepare_assert_error(&ga);
9289 ga_concat(&ga, (char_u *)"v:exception is not set");
9290 assert_error(&ga);
9291 ga_clear(&ga);
9292 }
9293 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9294 {
9295 prepare_assert_error(&ga);
9296 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9297 &vimvars[VV_EXCEPTION].vv_tv);
9298 assert_error(&ga);
9299 ga_clear(&ga);
9300 }
9301}
9302
9303/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304 * Common for assert_true() and assert_false().
9305 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009306 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009307assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009308 typval_T *argvars;
9309 int isTrue;
9310{
9311 int error = FALSE;
9312 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009313
9314 if (argvars[0].v_type != VAR_NUMBER
9315 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9316 || error)
9317 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009318 prepare_assert_error(&ga);
9319 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009320 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321 NULL, &argvars[0]);
9322 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009323 ga_clear(&ga);
9324 }
9325}
9326
9327/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009328 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009329 */
9330 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009332 typval_T *argvars;
9333 typval_T *rettv UNUSED;
9334{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009335 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009336}
9337
9338/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009339 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009340 */
9341 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009342f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009343 typval_T *argvars;
9344 typval_T *rettv UNUSED;
9345{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009347}
9348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009349#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009350/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009351 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009352 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353 static void
9354f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009355 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009356 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009357{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009358 float_T f;
9359
9360 rettv->v_type = VAR_FLOAT;
9361 if (get_float_arg(argvars, &f) == OK)
9362 rettv->vval.v_float = asin(f);
9363 else
9364 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365}
9366
9367/*
9368 * "atan()" function
9369 */
9370 static void
9371f_atan(argvars, rettv)
9372 typval_T *argvars;
9373 typval_T *rettv;
9374{
9375 float_T f;
9376
9377 rettv->v_type = VAR_FLOAT;
9378 if (get_float_arg(argvars, &f) == OK)
9379 rettv->vval.v_float = atan(f);
9380 else
9381 rettv->vval.v_float = 0.0;
9382}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009383
9384/*
9385 * "atan2()" function
9386 */
9387 static void
9388f_atan2(argvars, rettv)
9389 typval_T *argvars;
9390 typval_T *rettv;
9391{
9392 float_T fx, fy;
9393
9394 rettv->v_type = VAR_FLOAT;
9395 if (get_float_arg(argvars, &fx) == OK
9396 && get_float_arg(&argvars[1], &fy) == OK)
9397 rettv->vval.v_float = atan2(fx, fy);
9398 else
9399 rettv->vval.v_float = 0.0;
9400}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009401#endif
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403/*
9404 * "browse(save, title, initdir, default)" function
9405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifdef FEAT_BROWSE
9412 int save;
9413 char_u *title;
9414 char_u *initdir;
9415 char_u *defname;
9416 char_u buf[NUMBUFLEN];
9417 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 save = get_tv_number_chk(&argvars[0], &error);
9421 title = get_tv_string_chk(&argvars[1]);
9422 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9423 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 if (error || title == NULL || initdir == NULL || defname == NULL)
9426 rettv->vval.v_string = NULL;
9427 else
9428 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009429 do_browse(save ? BROWSE_SAVE : 0,
9430 title, defname, NULL, initdir, NULL, curbuf);
9431#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009433#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009435}
9436
9437/*
9438 * "browsedir(title, initdir)" function
9439 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009444{
9445#ifdef FEAT_BROWSE
9446 char_u *title;
9447 char_u *initdir;
9448 char_u buf[NUMBUFLEN];
9449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009450 title = get_tv_string_chk(&argvars[0]);
9451 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 if (title == NULL || initdir == NULL)
9454 rettv->vval.v_string = NULL;
9455 else
9456 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009457 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
Bram Moolenaar33570922005-01-25 22:26:29 +00009464static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466/*
9467 * Find a buffer by number or exact name.
9468 */
9469 static buf_T *
9470find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009475 if (avar->v_type == VAR_NUMBER)
9476 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009477 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009479 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009480 if (buf == NULL)
9481 {
9482 /* No full path name match, try a match with a URL or a "nofile"
9483 * buffer, these don't use the full path. */
9484 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9485 if (buf->b_fname != NULL
9486 && (path_with_url(buf->b_fname)
9487#ifdef FEAT_QUICKFIX
9488 || bt_nofile(buf)
9489#endif
9490 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009491 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009492 break;
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 }
9495 return buf;
9496}
9497
9498/*
9499 * "bufexists(expr)" function
9500 */
9501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009503 typval_T *argvars;
9504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * "buflisted(expr)" function
9511 */
9512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 buf_T *buf;
9518
9519 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
9524 * "bufloaded(expr)" function
9525 */
9526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 buf_T *buf;
9532
9533 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009537static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539/*
9540 * Get buffer by number or pattern.
9541 */
9542 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009543get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009545 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 int save_magic;
9549 char_u *save_cpo;
9550 buf_T *buf;
9551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 if (tv->v_type == VAR_NUMBER)
9553 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009554 if (tv->v_type != VAR_STRING)
9555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (name == NULL || *name == NUL)
9557 return curbuf;
9558 if (name[0] == '$' && name[1] == NUL)
9559 return lastbuf;
9560
9561 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9562 save_magic = p_magic;
9563 p_magic = TRUE;
9564 save_cpo = p_cpo;
9565 p_cpo = (char_u *)"";
9566
9567 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009568 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
9570 p_magic = save_magic;
9571 p_cpo = save_cpo;
9572
9573 /* If not found, try expanding the name, like done for bufexists(). */
9574 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
9577 return buf;
9578}
9579
9580/*
9581 * "bufname(expr)" function
9582 */
9583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 typval_T *argvars;
9586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 buf_T *buf;
9589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009592 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 --emsg_off;
9599}
9600
9601/*
9602 * "bufnr(expr)" function
9603 */
9604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *argvars;
9607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009610 int error = FALSE;
9611 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009615 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009616 --emsg_off;
9617
9618 /* If the buffer isn't found and the second argument is not zero create a
9619 * new buffer. */
9620 if (buf == NULL
9621 && argvars[1].v_type != VAR_UNKNOWN
9622 && get_tv_number_chk(&argvars[1], &error) != 0
9623 && !error
9624 && (name = get_tv_string_chk(&argvars[0])) != NULL
9625 && !error)
9626 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
9635 * "bufwinnr(nr)" function
9636 */
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642#ifdef FEAT_WINDOWS
9643 win_T *wp;
9644 int winnr = 0;
9645#endif
9646 buf_T *buf;
9647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009650 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_WINDOWS
9652 for (wp = firstwin; wp; wp = wp->w_next)
9653 {
9654 ++winnr;
9655 if (wp->w_buffer == buf)
9656 break;
9657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661#endif
9662 --emsg_off;
9663}
9664
9665/*
9666 * "byte2line(byte)" function
9667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#else
9676 long boff = 0;
9677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009678 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 (linenr_T)0, &boff);
9684#endif
9685}
9686
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009687 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009688byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009691 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009692{
9693#ifdef FEAT_MBYTE
9694 char_u *t;
9695#endif
9696 char_u *str;
9697 long idx;
9698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 str = get_tv_string_chk(&argvars[0]);
9700 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009703 return;
9704
9705#ifdef FEAT_MBYTE
9706 t = str;
9707 for ( ; idx > 0; idx--)
9708 {
9709 if (*t == NUL) /* EOL reached */
9710 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009711 if (enc_utf8 && comp)
9712 t += utf_ptr2len(t);
9713 else
9714 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009716 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009717#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009718 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009720#endif
9721}
9722
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009723/*
9724 * "byteidx()" function
9725 */
9726 static void
9727f_byteidx(argvars, rettv)
9728 typval_T *argvars;
9729 typval_T *rettv;
9730{
9731 byteidx(argvars, rettv, FALSE);
9732}
9733
9734/*
9735 * "byteidxcomp()" function
9736 */
9737 static void
9738f_byteidxcomp(argvars, rettv)
9739 typval_T *argvars;
9740 typval_T *rettv;
9741{
9742 byteidx(argvars, rettv, TRUE);
9743}
9744
Bram Moolenaardb913952012-06-29 12:54:53 +02009745 int
9746func_call(name, args, selfdict, rettv)
9747 char_u *name;
9748 typval_T *args;
9749 dict_T *selfdict;
9750 typval_T *rettv;
9751{
9752 listitem_T *item;
9753 typval_T argv[MAX_FUNC_ARGS + 1];
9754 int argc = 0;
9755 int dummy;
9756 int r = 0;
9757
9758 for (item = args->vval.v_list->lv_first; item != NULL;
9759 item = item->li_next)
9760 {
9761 if (argc == MAX_FUNC_ARGS)
9762 {
9763 EMSG(_("E699: Too many arguments"));
9764 break;
9765 }
9766 /* Make a copy of each argument. This is needed to be able to set
9767 * v_lock to VAR_FIXED in the copy without changing the original list.
9768 */
9769 copy_tv(&item->li_tv, &argv[argc++]);
9770 }
9771
9772 if (item == NULL)
9773 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9774 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9775 &dummy, TRUE, selfdict);
9776
9777 /* Free the arguments. */
9778 while (argc > 0)
9779 clear_tv(&argv[--argc]);
9780
9781 return r;
9782}
9783
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009784/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009785 * "call(func, arglist)" function
9786 */
9787 static void
9788f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009791{
9792 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009794
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009795 if (argvars[1].v_type != VAR_LIST)
9796 {
9797 EMSG(_(e_listreq));
9798 return;
9799 }
9800 if (argvars[1].vval.v_list == NULL)
9801 return;
9802
9803 if (argvars[0].v_type == VAR_FUNC)
9804 func = argvars[0].vval.v_string;
9805 else
9806 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009807 if (*func == NUL)
9808 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009809
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810 if (argvars[2].v_type != VAR_UNKNOWN)
9811 {
9812 if (argvars[2].v_type != VAR_DICT)
9813 {
9814 EMSG(_(e_dictreq));
9815 return;
9816 }
9817 selfdict = argvars[2].vval.v_dict;
9818 }
9819
Bram Moolenaardb913952012-06-29 12:54:53 +02009820 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009821}
9822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009823#ifdef FEAT_FLOAT
9824/*
9825 * "ceil({float})" function
9826 */
9827 static void
9828f_ceil(argvars, rettv)
9829 typval_T *argvars;
9830 typval_T *rettv;
9831{
9832 float_T f;
9833
9834 rettv->v_type = VAR_FLOAT;
9835 if (get_float_arg(argvars, &f) == OK)
9836 rettv->vval.v_float = ceil(f);
9837 else
9838 rettv->vval.v_float = 0.0;
9839}
9840#endif
9841
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009842/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009843 * "changenr()" function
9844 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009845 static void
9846f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009847 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009848 typval_T *rettv;
9849{
9850 rettv->vval.v_number = curbuf->b_u_seq_cur;
9851}
9852
9853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 * "char2nr(string)" function
9855 */
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861#ifdef FEAT_MBYTE
9862 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009863 {
9864 int utf8 = 0;
9865
9866 if (argvars[1].v_type != VAR_UNKNOWN)
9867 utf8 = get_tv_number_chk(&argvars[1], NULL);
9868
9869 if (utf8)
9870 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9871 else
9872 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 else
9875#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877}
9878
9879/*
9880 * "cindent(lnum)" function
9881 */
9882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009883f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009884 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887#ifdef FEAT_CINDENT
9888 pos_T pos;
9889 linenr_T lnum;
9890
9891 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9894 {
9895 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 curwin->w_cursor = pos;
9898 }
9899 else
9900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009905 * "clearmatches()" function
9906 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009907 static void
9908f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009909 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009910 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009911{
9912#ifdef FEAT_SEARCH_EXTRA
9913 clear_matches(curwin);
9914#endif
9915}
9916
9917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 * "col(string)" function
9919 */
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 colnr_T col = 0;
9926 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009927 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009929 fp = var2fpos(&argvars[0], FALSE, &fnum);
9930 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 {
9932 if (fp->col == MAXCOL)
9933 {
9934 /* '> can be MAXCOL, get the length of the line then */
9935 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009936 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 col = MAXCOL;
9939 }
9940 else
9941 {
9942 col = fp->col + 1;
9943#ifdef FEAT_VIRTUALEDIT
9944 /* col(".") when the cursor is on the NUL at the end of the line
9945 * because of "coladd" can be seen as an extra column. */
9946 if (virtual_active() && fp == &curwin->w_cursor)
9947 {
9948 char_u *p = ml_get_cursor();
9949
9950 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9951 curwin->w_virtcol - curwin->w_cursor.coladd))
9952 {
9953# ifdef FEAT_MBYTE
9954 int l;
9955
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009956 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 col += l;
9958# else
9959 if (*p != NUL && p[1] == NUL)
9960 ++col;
9961# endif
9962 }
9963 }
9964#endif
9965 }
9966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
Bram Moolenaar572cb562005-08-05 21:35:02 +00009970#if defined(FEAT_INS_EXPAND)
9971/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009972 * "complete()" function
9973 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009974 static void
9975f_complete(argvars, rettv)
9976 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009977 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009978{
9979 int startcol;
9980
9981 if ((State & INSERT) == 0)
9982 {
9983 EMSG(_("E785: complete() can only be used in Insert mode"));
9984 return;
9985 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009986
9987 /* Check for undo allowed here, because if something was already inserted
9988 * the line was already saved for undo and this check isn't done. */
9989 if (!undo_allowed())
9990 return;
9991
Bram Moolenaarade00832006-03-10 21:46:58 +00009992 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9993 {
9994 EMSG(_(e_invarg));
9995 return;
9996 }
9997
9998 startcol = get_tv_number_chk(&argvars[0], NULL);
9999 if (startcol <= 0)
10000 return;
10001
10002 set_completion(startcol - 1, argvars[1].vval.v_list);
10003}
10004
10005/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010006 * "complete_add()" function
10007 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010008 static void
10009f_complete_add(argvars, rettv)
10010 typval_T *argvars;
10011 typval_T *rettv;
10012{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010013 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010014}
10015
10016/*
10017 * "complete_check()" function
10018 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010019 static void
10020f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010021 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010022 typval_T *rettv;
10023{
10024 int saved = RedrawingDisabled;
10025
10026 RedrawingDisabled = 0;
10027 ins_compl_check_keys(0);
10028 rettv->vval.v_number = compl_interrupted;
10029 RedrawingDisabled = saved;
10030}
10031#endif
10032
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033/*
10034 * "confirm(message, buttons[, default [, type]])" function
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010038 typval_T *argvars UNUSED;
10039 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10042 char_u *message;
10043 char_u *buttons = NULL;
10044 char_u buf[NUMBUFLEN];
10045 char_u buf2[NUMBUFLEN];
10046 int def = 1;
10047 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048 char_u *typestr;
10049 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 message = get_tv_string_chk(&argvars[0]);
10052 if (message == NULL)
10053 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10057 if (buttons == NULL)
10058 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010061 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010062 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010064 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10065 if (typestr == NULL)
10066 error = TRUE;
10067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 switch (TOUPPER_ASC(*typestr))
10070 {
10071 case 'E': type = VIM_ERROR; break;
10072 case 'Q': type = VIM_QUESTION; break;
10073 case 'I': type = VIM_INFO; break;
10074 case 'W': type = VIM_WARNING; break;
10075 case 'G': type = VIM_GENERIC; break;
10076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 }
10079 }
10080 }
10081
10082 if (buttons == NULL || *buttons == NUL)
10083 buttons = (char_u *)_("&Ok");
10084
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010085 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010087 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088#endif
10089}
10090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091/*
10092 * "copy()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010099 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010100}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010102#ifdef FEAT_FLOAT
10103/*
10104 * "cos()" function
10105 */
10106 static void
10107f_cos(argvars, rettv)
10108 typval_T *argvars;
10109 typval_T *rettv;
10110{
10111 float_T f;
10112
10113 rettv->v_type = VAR_FLOAT;
10114 if (get_float_arg(argvars, &f) == OK)
10115 rettv->vval.v_float = cos(f);
10116 else
10117 rettv->vval.v_float = 0.0;
10118}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010119
10120/*
10121 * "cosh()" function
10122 */
10123 static void
10124f_cosh(argvars, rettv)
10125 typval_T *argvars;
10126 typval_T *rettv;
10127{
10128 float_T f;
10129
10130 rettv->v_type = VAR_FLOAT;
10131 if (get_float_arg(argvars, &f) == OK)
10132 rettv->vval.v_float = cosh(f);
10133 else
10134 rettv->vval.v_float = 0.0;
10135}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010136#endif
10137
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010139 * "count()" function
10140 */
10141 static void
10142f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 typval_T *argvars;
10144 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010145{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010146 long n = 0;
10147 int ic = FALSE;
10148
Bram Moolenaare9a41262005-01-15 22:18:47 +000010149 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 listitem_T *li;
10152 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010154
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 if ((l = argvars[0].vval.v_list) != NULL)
10156 {
10157 li = l->lv_first;
10158 if (argvars[2].v_type != VAR_UNKNOWN)
10159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 int error = FALSE;
10161
10162 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163 if (argvars[3].v_type != VAR_UNKNOWN)
10164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 idx = get_tv_number_chk(&argvars[3], &error);
10166 if (!error)
10167 {
10168 li = list_find(l, idx);
10169 if (li == NULL)
10170 EMSGN(_(e_listidx), idx);
10171 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 if (error)
10174 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 }
10176
10177 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010178 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 ++n;
10180 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 else if (argvars[0].v_type == VAR_DICT)
10183 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 int todo;
10185 dict_T *d;
10186 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010188 if ((d = argvars[0].vval.v_dict) != NULL)
10189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 int error = FALSE;
10191
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 if (argvars[2].v_type != VAR_UNKNOWN)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 if (argvars[3].v_type != VAR_UNKNOWN)
10196 EMSG(_(e_invarg));
10197 }
10198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010199 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010201 {
10202 if (!HASHITEM_EMPTY(hi))
10203 {
10204 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010205 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010206 ++n;
10207 }
10208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 }
10210 }
10211 else
10212 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010213 rettv->vval.v_number = n;
10214}
10215
10216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10218 *
10219 * Checks the existence of a cscope connection.
10220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010223 typval_T *argvars UNUSED;
10224 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226#ifdef FEAT_CSCOPE
10227 int num = 0;
10228 char_u *dbpath = NULL;
10229 char_u *prepend = NULL;
10230 char_u buf[NUMBUFLEN];
10231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010232 if (argvars[0].v_type != VAR_UNKNOWN
10233 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 num = (int)get_tv_number(&argvars[0]);
10236 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243}
10244
10245/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010246 * "cursor(lnum, col)" function, or
10247 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010249 * Moves the cursor to the specified line and column.
10250 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *argvars;
10255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010258#ifdef FEAT_VIRTUALEDIT
10259 long coladd = 0;
10260#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010261 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010263 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010264 if (argvars[1].v_type == VAR_UNKNOWN)
10265 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010266 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010267 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010268
Bram Moolenaar493c1782014-05-28 14:34:46 +020010269 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010270 {
10271 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010272 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010273 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010274 line = pos.lnum;
10275 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010276#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010277 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010278#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010279 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010280 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010281 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010282 set_curswant = FALSE;
10283 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010284 }
10285 else
10286 {
10287 line = get_tv_lnum(argvars);
10288 col = get_tv_number_chk(&argvars[1], NULL);
10289#ifdef FEAT_VIRTUALEDIT
10290 if (argvars[2].v_type != VAR_UNKNOWN)
10291 coladd = get_tv_number_chk(&argvars[2], NULL);
10292#endif
10293 }
10294 if (line < 0 || col < 0
10295#ifdef FEAT_VIRTUALEDIT
10296 || coladd < 0
10297#endif
10298 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 if (line > 0)
10301 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 if (col > 0)
10303 curwin->w_cursor.col = col - 1;
10304#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010305 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#endif
10307
10308 /* Make sure the cursor is in a valid position. */
10309 check_cursor();
10310#ifdef FEAT_MBYTE
10311 /* Correct cursor for multi-byte character. */
10312 if (has_mbyte)
10313 mb_adjust_cursor();
10314#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010315
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010316 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010317 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318}
10319
10320/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010321 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 */
10323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 typval_T *argvars;
10326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010328 int noref = 0;
10329
10330 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010332 if (noref < 0 || noref > 1)
10333 EMSG(_(e_invarg));
10334 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010335 {
10336 current_copyID += COPYID_INC;
10337 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340
10341/*
10342 * "delete()" function
10343 */
10344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 typval_T *argvars;
10347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353}
10354
10355/*
10356 * "did_filetype()" function
10357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010360 typval_T *argvars UNUSED;
10361 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362{
10363#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365#endif
10366}
10367
10368/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010369 * "diff_filler()" function
10370 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010373 typval_T *argvars UNUSED;
10374 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010375{
10376#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010378#endif
10379}
10380
10381/*
10382 * "diff_hlID()" function
10383 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010386 typval_T *argvars UNUSED;
10387 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010388{
10389#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010391 static linenr_T prev_lnum = 0;
10392 static int changedtick = 0;
10393 static int fnum = 0;
10394 static int change_start = 0;
10395 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010396 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010397 int filler_lines;
10398 int col;
10399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010400 if (lnum < 0) /* ignore type error in {lnum} arg */
10401 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010402 if (lnum != prev_lnum
10403 || changedtick != curbuf->b_changedtick
10404 || fnum != curbuf->b_fnum)
10405 {
10406 /* New line, buffer, change: need to get the values. */
10407 filler_lines = diff_check(curwin, lnum);
10408 if (filler_lines < 0)
10409 {
10410 if (filler_lines == -1)
10411 {
10412 change_start = MAXCOL;
10413 change_end = -1;
10414 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10415 hlID = HLF_ADD; /* added line */
10416 else
10417 hlID = HLF_CHD; /* changed line */
10418 }
10419 else
10420 hlID = HLF_ADD; /* added line */
10421 }
10422 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010423 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010424 prev_lnum = lnum;
10425 changedtick = curbuf->b_changedtick;
10426 fnum = curbuf->b_fnum;
10427 }
10428
10429 if (hlID == HLF_CHD || hlID == HLF_TXD)
10430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010432 if (col >= change_start && col <= change_end)
10433 hlID = HLF_TXD; /* changed text */
10434 else
10435 hlID = HLF_CHD; /* changed line */
10436 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010437 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010438#endif
10439}
10440
10441/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010442 * "empty({expr})" function
10443 */
10444 static void
10445f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *argvars;
10447 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010448{
10449 int n;
10450
10451 switch (argvars[0].v_type)
10452 {
10453 case VAR_STRING:
10454 case VAR_FUNC:
10455 n = argvars[0].vval.v_string == NULL
10456 || *argvars[0].vval.v_string == NUL;
10457 break;
10458 case VAR_NUMBER:
10459 n = argvars[0].vval.v_number == 0;
10460 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010461#ifdef FEAT_FLOAT
10462 case VAR_FLOAT:
10463 n = argvars[0].vval.v_float == 0.0;
10464 break;
10465#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010466 case VAR_LIST:
10467 n = argvars[0].vval.v_list == NULL
10468 || argvars[0].vval.v_list->lv_first == NULL;
10469 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010470 case VAR_DICT:
10471 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010474 default:
10475 EMSG2(_(e_intern2), "f_empty()");
10476 n = 0;
10477 }
10478
10479 rettv->vval.v_number = n;
10480}
10481
10482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 * "escape({string}, {chars})" function
10484 */
10485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 typval_T *argvars;
10488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489{
10490 char_u buf[NUMBUFLEN];
10491
Bram Moolenaar758711c2005-02-02 23:11:38 +000010492 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10493 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495}
10496
10497/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010498 * "eval()" function
10499 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010500 static void
10501f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010502 typval_T *argvars;
10503 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010504{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010505 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 s = get_tv_string_chk(&argvars[0]);
10508 if (s != NULL)
10509 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010510
Bram Moolenaar615b9972015-01-14 17:15:05 +010010511 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10513 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010514 if (p != NULL && !aborting())
10515 EMSG2(_(e_invexpr2), p);
10516 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010518 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010520 else if (*s != NUL)
10521 EMSG(_(e_trailing));
10522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "eventhandler()" function
10526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010529 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533}
10534
10535/*
10536 * "executable()" function
10537 */
10538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010540 typval_T *argvars;
10541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010543 char_u *name = get_tv_string(&argvars[0]);
10544
10545 /* Check in $PATH and also check directly if there is a directory name. */
10546 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10547 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010548}
10549
10550/*
10551 * "exepath()" function
10552 */
10553 static void
10554f_exepath(argvars, rettv)
10555 typval_T *argvars;
10556 typval_T *rettv;
10557{
10558 char_u *p = NULL;
10559
Bram Moolenaarb5971142015-03-21 17:32:19 +010010560 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010561 rettv->v_type = VAR_STRING;
10562 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563}
10564
10565/*
10566 * "exists()" function
10567 */
10568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *argvars;
10571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
10573 char_u *p;
10574 char_u *name;
10575 int n = FALSE;
10576 int len = 0;
10577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 if (*p == '$') /* environment variable */
10580 {
10581 /* first try "normal" environment variables (fast) */
10582 if (mch_getenv(p + 1) != NULL)
10583 n = TRUE;
10584 else
10585 {
10586 /* try expanding things like $VIM and ${HOME} */
10587 p = expand_env_save(p);
10588 if (p != NULL && *p != '$')
10589 n = TRUE;
10590 vim_free(p);
10591 }
10592 }
10593 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010596 if (*skipwhite(p) != NUL)
10597 n = FALSE; /* trailing garbage */
10598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 else if (*p == '*') /* internal or user defined function */
10600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010601 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 }
10603 else if (*p == ':')
10604 {
10605 n = cmd_exists(p + 1);
10606 }
10607 else if (*p == '#')
10608 {
10609#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010610 if (p[1] == '#')
10611 n = autocmd_supported(p + 2);
10612 else
10613 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614#endif
10615 }
10616 else /* internal variable */
10617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010618 char_u *tofree;
10619 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010621 /* get_name_len() takes care of expanding curly braces */
10622 name = p;
10623 len = get_name_len(&p, &tofree, TRUE, FALSE);
10624 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010626 if (tofree != NULL)
10627 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010628 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010629 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010631 /* handle d.key, l[idx], f(expr) */
10632 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10633 if (n)
10634 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 }
10636 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010637 if (*p != NUL)
10638 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010640 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 }
10642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644}
10645
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010646#ifdef FEAT_FLOAT
10647/*
10648 * "exp()" function
10649 */
10650 static void
10651f_exp(argvars, rettv)
10652 typval_T *argvars;
10653 typval_T *rettv;
10654{
10655 float_T f;
10656
10657 rettv->v_type = VAR_FLOAT;
10658 if (get_float_arg(argvars, &f) == OK)
10659 rettv->vval.v_float = exp(f);
10660 else
10661 rettv->vval.v_float = 0.0;
10662}
10663#endif
10664
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665/*
10666 * "expand()" function
10667 */
10668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010670 typval_T *argvars;
10671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
10673 char_u *s;
10674 int len;
10675 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010676 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010679 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010682 if (argvars[1].v_type != VAR_UNKNOWN
10683 && argvars[2].v_type != VAR_UNKNOWN
10684 && get_tv_number_chk(&argvars[2], &error)
10685 && !error)
10686 {
10687 rettv->v_type = VAR_LIST;
10688 rettv->vval.v_list = NULL;
10689 }
10690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (*s == '%' || *s == '#' || *s == '<')
10693 {
10694 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010695 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010697 if (rettv->v_type == VAR_LIST)
10698 {
10699 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10700 list_append_string(rettv->vval.v_list, result, -1);
10701 else
10702 vim_free(result);
10703 }
10704 else
10705 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 }
10707 else
10708 {
10709 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010710 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 if (argvars[1].v_type != VAR_UNKNOWN
10712 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010713 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010714 if (!error)
10715 {
10716 ExpandInit(&xpc);
10717 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010718 if (p_wic)
10719 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010720 if (rettv->v_type == VAR_STRING)
10721 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10722 options, WILD_ALL);
10723 else if (rettv_list_alloc(rettv) != FAIL)
10724 {
10725 int i;
10726
10727 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10728 for (i = 0; i < xpc.xp_numfiles; i++)
10729 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10730 ExpandCleanup(&xpc);
10731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 }
10733 else
10734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 }
10736}
10737
10738/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010739 * Go over all entries in "d2" and add them to "d1".
10740 * When "action" is "error" then a duplicate key is an error.
10741 * When "action" is "force" then a duplicate key is overwritten.
10742 * Otherwise duplicate keys are ignored ("action" is "keep").
10743 */
10744 void
10745dict_extend(d1, d2, action)
10746 dict_T *d1;
10747 dict_T *d2;
10748 char_u *action;
10749{
10750 dictitem_T *di1;
10751 hashitem_T *hi2;
10752 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010753 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010754
10755 todo = (int)d2->dv_hashtab.ht_used;
10756 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10757 {
10758 if (!HASHITEM_EMPTY(hi2))
10759 {
10760 --todo;
10761 di1 = dict_find(d1, hi2->hi_key, -1);
10762 if (d1->dv_scope != 0)
10763 {
10764 /* Disallow replacing a builtin function in l: and g:.
10765 * Check the key to be valid when adding to any
10766 * scope. */
10767 if (d1->dv_scope == VAR_DEF_SCOPE
10768 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10769 && var_check_func_name(hi2->hi_key,
10770 di1 == NULL))
10771 break;
10772 if (!valid_varname(hi2->hi_key))
10773 break;
10774 }
10775 if (di1 == NULL)
10776 {
10777 di1 = dictitem_copy(HI2DI(hi2));
10778 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10779 dictitem_free(di1);
10780 }
10781 else if (*action == 'e')
10782 {
10783 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10784 break;
10785 }
10786 else if (*action == 'f' && HI2DI(hi2) != di1)
10787 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010788 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10789 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010790 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010791 clear_tv(&di1->di_tv);
10792 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10793 }
10794 }
10795 }
10796}
10797
10798/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010799 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010800 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010801 */
10802 static void
10803f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010804 typval_T *argvars;
10805 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010806{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010807 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010808
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 list_T *l1, *l2;
10812 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010815
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 l1 = argvars[0].vval.v_list;
10817 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010818 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010819 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 {
10821 if (argvars[2].v_type != VAR_UNKNOWN)
10822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 before = get_tv_number_chk(&argvars[2], &error);
10824 if (error)
10825 return; /* type error; errmsg already given */
10826
Bram Moolenaar758711c2005-02-02 23:11:38 +000010827 if (before == l1->lv_len)
10828 item = NULL;
10829 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010831 item = list_find(l1, before);
10832 if (item == NULL)
10833 {
10834 EMSGN(_(e_listidx), before);
10835 return;
10836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 }
10838 }
10839 else
10840 item = NULL;
10841 list_extend(l1, l2, item);
10842
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 copy_tv(&argvars[0], rettv);
10844 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010846 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10847 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010848 dict_T *d1, *d2;
10849 char_u *action;
10850 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851
10852 d1 = argvars[0].vval.v_dict;
10853 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010854 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010855 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 {
10857 /* Check the third argument. */
10858 if (argvars[2].v_type != VAR_UNKNOWN)
10859 {
10860 static char *(av[]) = {"keep", "force", "error"};
10861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 action = get_tv_string_chk(&argvars[2]);
10863 if (action == NULL)
10864 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010865 for (i = 0; i < 3; ++i)
10866 if (STRCMP(action, av[i]) == 0)
10867 break;
10868 if (i == 3)
10869 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010870 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 return;
10872 }
10873 }
10874 else
10875 action = (char_u *)"force";
10876
Bram Moolenaara9922d62013-05-30 13:01:18 +020010877 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 copy_tv(&argvars[0], rettv);
10880 }
10881 }
10882 else
10883 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010884}
10885
10886/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010887 * "feedkeys()" function
10888 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010889 static void
10890f_feedkeys(argvars, rettv)
10891 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010892 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010893{
10894 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010895 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010896 char_u *keys, *flags;
10897 char_u nbuf[NUMBUFLEN];
10898 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010899 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010900
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010901 /* This is not allowed in the sandbox. If the commands would still be
10902 * executed in the sandbox it would be OK, but it probably happens later,
10903 * when "sandbox" is no longer set. */
10904 if (check_secure())
10905 return;
10906
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010907 keys = get_tv_string(&argvars[0]);
10908 if (*keys != NUL)
10909 {
10910 if (argvars[1].v_type != VAR_UNKNOWN)
10911 {
10912 flags = get_tv_string_buf(&argvars[1], nbuf);
10913 for ( ; *flags != NUL; ++flags)
10914 {
10915 switch (*flags)
10916 {
10917 case 'n': remap = FALSE; break;
10918 case 'm': remap = TRUE; break;
10919 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010920 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010921 }
10922 }
10923 }
10924
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010925 /* Need to escape K_SPECIAL and CSI before putting the string in the
10926 * typeahead buffer. */
10927 keys_esc = vim_strsave_escape_csi(keys);
10928 if (keys_esc != NULL)
10929 {
10930 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010931 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010932 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010933 if (vgetc_busy)
10934 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010935 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010936 }
10937}
10938
10939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 * "filereadable()" function
10941 */
10942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010947 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 char_u *p;
10949 int n;
10950
Bram Moolenaarc236c162008-07-13 17:41:49 +000010951#ifndef O_NONBLOCK
10952# define O_NONBLOCK 0
10953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010955 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10956 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
10958 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010959 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 }
10961 else
10962 n = FALSE;
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965}
10966
10967/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010968 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 * rights to write into.
10970 */
10971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010973 typval_T *argvars;
10974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010976 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010979static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010980
10981 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010982findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010983 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010984 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010985 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010986{
10987#ifdef FEAT_SEARCHPATH
10988 char_u *fname;
10989 char_u *fresult = NULL;
10990 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10991 char_u *p;
10992 char_u pathbuf[NUMBUFLEN];
10993 int count = 1;
10994 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010995 int error = FALSE;
10996#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010997
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010998 rettv->vval.v_string = NULL;
10999 rettv->v_type = VAR_STRING;
11000
11001#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011003
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11007 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011008 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 else
11010 {
11011 if (*p != NUL)
11012 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011015 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011016 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011017 }
11018
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011019 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11020 error = TRUE;
11021
11022 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 do
11025 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011026 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011027 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 fresult = find_file_in_path_option(first ? fname : NULL,
11029 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011030 0, first, path,
11031 find_what,
11032 curbuf->b_ffname,
11033 find_what == FINDFILE_DIR
11034 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011035 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011036
11037 if (fresult != NULL && rettv->v_type == VAR_LIST)
11038 list_append_string(rettv->vval.v_list, fresult, -1);
11039
11040 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011042
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011043 if (rettv->v_type == VAR_STRING)
11044 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011045#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011046}
11047
Bram Moolenaar33570922005-01-25 22:26:29 +000011048static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11049static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011050
11051/*
11052 * Implementation of map() and filter().
11053 */
11054 static void
11055filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011056 typval_T *argvars;
11057 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011058 int map;
11059{
11060 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011062 listitem_T *li, *nli;
11063 list_T *l = NULL;
11064 dictitem_T *di;
11065 hashtab_T *ht;
11066 hashitem_T *hi;
11067 dict_T *d = NULL;
11068 typval_T save_val;
11069 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011071 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011072 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011074 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011075 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011076 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011077
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011079 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011080 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011081 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011082 return;
11083 }
11084 else if (argvars[0].v_type == VAR_DICT)
11085 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011086 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011087 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088 return;
11089 }
11090 else
11091 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011092 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 return;
11094 }
11095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 expr = get_tv_string_buf_chk(&argvars[1], buf);
11097 /* On type errors, the preceding call has already displayed an error
11098 * message. Avoid a misleading error message for an empty string that
11099 * was not passed as argument. */
11100 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 prepare_vimvar(VV_VAL, &save_val);
11103 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011104
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011105 /* We reset "did_emsg" to be able to detect whether an error
11106 * occurred during evaluation of the expression. */
11107 save_did_emsg = did_emsg;
11108 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011109
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011110 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 vimvars[VV_KEY].vv_type = VAR_STRING;
11114
11115 ht = &d->dv_hashtab;
11116 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011117 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 if (!HASHITEM_EMPTY(hi))
11121 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011122 int r;
11123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 --todo;
11125 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011126 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011127 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11128 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 break;
11130 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011131 r = filter_map_one(&di->di_tv, expr, map, &rem);
11132 clear_tv(&vimvars[VV_KEY].vv_tv);
11133 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011134 break;
11135 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011136 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011137 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11138 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011139 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 }
11143 }
11144 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 }
11146 else
11147 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011148 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 for (li = l->lv_first; li != NULL; li = nli)
11151 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011152 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011153 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011155 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011156 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011157 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011158 break;
11159 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011161 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011162 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011163 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011164
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011165 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011167
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011168 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170
11171 copy_tv(&argvars[0], rettv);
11172}
11173
11174 static int
11175filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011176 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011177 char_u *expr;
11178 int map;
11179 int *remp;
11180{
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011182 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011183 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011186 s = expr;
11187 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011188 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011189 if (*s != NUL) /* check for trailing chars after expr */
11190 {
11191 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011192 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011193 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011194 }
11195 if (map)
11196 {
11197 /* map(): replace the list item value */
11198 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011199 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011200 *tv = rettv;
11201 }
11202 else
11203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 int error = FALSE;
11205
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011208 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 /* On type error, nothing has been removed; return FAIL to stop the
11210 * loop. The error message was given by get_tv_number_chk(). */
11211 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011212 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011214 retval = OK;
11215theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011217 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011218}
11219
11220/*
11221 * "filter()" function
11222 */
11223 static void
11224f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011225 typval_T *argvars;
11226 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011227{
11228 filter_map(argvars, rettv, FALSE);
11229}
11230
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 * "finddir({fname}[, {path}[, {count}]])" function
11233 */
11234 static void
11235f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011236 typval_T *argvars;
11237 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011239 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240}
11241
11242/*
11243 * "findfile({fname}[, {path}[, {count}]])" function
11244 */
11245 static void
11246f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *argvars;
11248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011250 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251}
11252
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011253#ifdef FEAT_FLOAT
11254/*
11255 * "float2nr({float})" function
11256 */
11257 static void
11258f_float2nr(argvars, rettv)
11259 typval_T *argvars;
11260 typval_T *rettv;
11261{
11262 float_T f;
11263
11264 if (get_float_arg(argvars, &f) == OK)
11265 {
11266 if (f < -0x7fffffff)
11267 rettv->vval.v_number = -0x7fffffff;
11268 else if (f > 0x7fffffff)
11269 rettv->vval.v_number = 0x7fffffff;
11270 else
11271 rettv->vval.v_number = (varnumber_T)f;
11272 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011273}
11274
11275/*
11276 * "floor({float})" function
11277 */
11278 static void
11279f_floor(argvars, rettv)
11280 typval_T *argvars;
11281 typval_T *rettv;
11282{
11283 float_T f;
11284
11285 rettv->v_type = VAR_FLOAT;
11286 if (get_float_arg(argvars, &f) == OK)
11287 rettv->vval.v_float = floor(f);
11288 else
11289 rettv->vval.v_float = 0.0;
11290}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011291
11292/*
11293 * "fmod()" function
11294 */
11295 static void
11296f_fmod(argvars, rettv)
11297 typval_T *argvars;
11298 typval_T *rettv;
11299{
11300 float_T fx, fy;
11301
11302 rettv->v_type = VAR_FLOAT;
11303 if (get_float_arg(argvars, &fx) == OK
11304 && get_float_arg(&argvars[1], &fy) == OK)
11305 rettv->vval.v_float = fmod(fx, fy);
11306 else
11307 rettv->vval.v_float = 0.0;
11308}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011309#endif
11310
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011312 * "fnameescape({string})" function
11313 */
11314 static void
11315f_fnameescape(argvars, rettv)
11316 typval_T *argvars;
11317 typval_T *rettv;
11318{
11319 rettv->vval.v_string = vim_strsave_fnameescape(
11320 get_tv_string(&argvars[0]), FALSE);
11321 rettv->v_type = VAR_STRING;
11322}
11323
11324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 * "fnamemodify({fname}, {mods})" function
11326 */
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332 char_u *fname;
11333 char_u *mods;
11334 int usedlen = 0;
11335 int len;
11336 char_u *fbuf = NULL;
11337 char_u buf[NUMBUFLEN];
11338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 fname = get_tv_string_chk(&argvars[0]);
11340 mods = get_tv_string_buf_chk(&argvars[1], buf);
11341 if (fname == NULL || mods == NULL)
11342 fname = NULL;
11343 else
11344 {
11345 len = (int)STRLEN(fname);
11346 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 vim_free(fbuf);
11355}
11356
Bram Moolenaar33570922005-01-25 22:26:29 +000011357static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
11359/*
11360 * "foldclosed()" function
11361 */
11362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011366 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368#ifdef FEAT_FOLDING
11369 linenr_T lnum;
11370 linenr_T first, last;
11371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11374 {
11375 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11376 {
11377 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 return;
11382 }
11383 }
11384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386}
11387
11388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389 * "foldclosed()" function
11390 */
11391 static void
11392f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011393 typval_T *argvars;
11394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011395{
11396 foldclosed_both(argvars, rettv, FALSE);
11397}
11398
11399/*
11400 * "foldclosedend()" function
11401 */
11402 static void
11403f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *argvars;
11405 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406{
11407 foldclosed_both(argvars, rettv, TRUE);
11408}
11409
11410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 * "foldlevel()" function
11412 */
11413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011415 typval_T *argvars UNUSED;
11416 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
11418#ifdef FEAT_FOLDING
11419 linenr_T lnum;
11420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425}
11426
11427/*
11428 * "foldtext()" function
11429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
11435#ifdef FEAT_FOLDING
11436 linenr_T lnum;
11437 char_u *s;
11438 char_u *r;
11439 int len;
11440 char *txt;
11441#endif
11442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11447 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11448 <= curbuf->b_ml.ml_line_count
11449 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 {
11451 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011452 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11453 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 {
11455 if (!linewhite(lnum))
11456 break;
11457 ++lnum;
11458 }
11459
11460 /* Find interesting text in this line. */
11461 s = skipwhite(ml_get(lnum));
11462 /* skip C comment-start */
11463 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011466 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011468 {
11469 s = skipwhite(ml_get(lnum + 1));
11470 if (*s == '*')
11471 s = skipwhite(s + 1);
11472 }
11473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 txt = _("+-%s%3ld lines: ");
11475 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011476 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 + 20 /* for %3ld */
11478 + STRLEN(s))); /* concatenated */
11479 if (r != NULL)
11480 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011481 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11482 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11483 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 len = (int)STRLEN(r);
11485 STRCAT(r, s);
11486 /* remove 'foldmarker' and 'commentstring' */
11487 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 }
11490 }
11491#endif
11492}
11493
11494/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011495 * "foldtextresult(lnum)" function
11496 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011499 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011501{
11502#ifdef FEAT_FOLDING
11503 linenr_T lnum;
11504 char_u *text;
11505 char_u buf[51];
11506 foldinfo_T foldinfo;
11507 int fold_count;
11508#endif
11509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 rettv->v_type = VAR_STRING;
11511 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011512#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 /* treat illegal types and illegal string values for {lnum} the same */
11515 if (lnum < 0)
11516 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011517 fold_count = foldedCount(curwin, lnum, &foldinfo);
11518 if (fold_count > 0)
11519 {
11520 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11521 &foldinfo, buf);
11522 if (text == buf)
11523 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011525 }
11526#endif
11527}
11528
11529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 * "foreground()" function
11531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011534 typval_T *argvars UNUSED;
11535 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537#ifdef FEAT_GUI
11538 if (gui.in_use)
11539 gui_mch_set_foreground();
11540#else
11541# ifdef WIN32
11542 win32_set_foreground();
11543# endif
11544#endif
11545}
11546
11547/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011548 * "function()" function
11549 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011554{
11555 char_u *s;
11556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011558 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011560 /* Don't check an autoload name for existence here. */
11561 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011562 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011563 else
11564 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011565 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011566 {
11567 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011568 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011569
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011570 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11571 * also be called from another script. Using trans_function_name()
11572 * would also work, but some plugins depend on the name being
11573 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011574 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011575 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011576 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011577 if (rettv->vval.v_string != NULL)
11578 {
11579 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011580 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011581 }
11582 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011583 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011584 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011586 }
11587}
11588
11589/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011590 * "garbagecollect()" function
11591 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011592 static void
11593f_garbagecollect(argvars, rettv)
11594 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011595 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011596{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011597 /* This is postponed until we are back at the toplevel, because we may be
11598 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11599 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011600
11601 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11602 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011603}
11604
11605/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011606 * "get()" function
11607 */
11608 static void
11609f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011610 typval_T *argvars;
11611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011612{
Bram Moolenaar33570922005-01-25 22:26:29 +000011613 listitem_T *li;
11614 list_T *l;
11615 dictitem_T *di;
11616 dict_T *d;
11617 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011618
Bram Moolenaare9a41262005-01-15 22:18:47 +000011619 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011620 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011621 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 int error = FALSE;
11624
11625 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11626 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011628 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011630 else if (argvars[0].v_type == VAR_DICT)
11631 {
11632 if ((d = argvars[0].vval.v_dict) != NULL)
11633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011634 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011635 if (di != NULL)
11636 tv = &di->di_tv;
11637 }
11638 }
11639 else
11640 EMSG2(_(e_listdictarg), "get()");
11641
11642 if (tv == NULL)
11643 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011645 copy_tv(&argvars[2], rettv);
11646 }
11647 else
11648 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649}
11650
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651static 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 +000011652
11653/*
11654 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011655 * Return a range (from start to end) of lines in rettv from the specified
11656 * buffer.
11657 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011658 */
11659 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011660get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011661 buf_T *buf;
11662 linenr_T start;
11663 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011664 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011665 typval_T *rettv;
11666{
11667 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011668
Bram Moolenaar959a1432013-12-14 12:17:38 +010011669 rettv->v_type = VAR_STRING;
11670 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011671 if (retlist && rettv_list_alloc(rettv) == FAIL)
11672 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011673
11674 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11675 return;
11676
11677 if (!retlist)
11678 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011679 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11680 p = ml_get_buf(buf, start, FALSE);
11681 else
11682 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011683 rettv->vval.v_string = vim_strsave(p);
11684 }
11685 else
11686 {
11687 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011688 return;
11689
11690 if (start < 1)
11691 start = 1;
11692 if (end > buf->b_ml.ml_line_count)
11693 end = buf->b_ml.ml_line_count;
11694 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011695 if (list_append_string(rettv->vval.v_list,
11696 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011697 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011698 }
11699}
11700
11701/*
11702 * "getbufline()" function
11703 */
11704 static void
11705f_getbufline(argvars, rettv)
11706 typval_T *argvars;
11707 typval_T *rettv;
11708{
11709 linenr_T lnum;
11710 linenr_T end;
11711 buf_T *buf;
11712
11713 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11714 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011715 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011716 --emsg_off;
11717
Bram Moolenaar661b1822005-07-28 22:36:45 +000011718 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011719 if (argvars[2].v_type == VAR_UNKNOWN)
11720 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011721 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011722 end = get_tv_lnum_buf(&argvars[2], buf);
11723
Bram Moolenaar342337a2005-07-21 21:11:17 +000011724 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011725}
11726
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727/*
11728 * "getbufvar()" function
11729 */
11730 static void
11731f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *argvars;
11733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011734{
11735 buf_T *buf;
11736 buf_T *save_curbuf;
11737 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011739 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11742 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011743 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011744 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011745
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011748
11749 if (buf != NULL && varname != NULL)
11750 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011751 /* set curbuf to be our buf, temporarily */
11752 save_curbuf = curbuf;
11753 curbuf = buf;
11754
Bram Moolenaar0d660222005-01-07 21:51:51 +000011755 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011756 {
11757 if (get_option_tv(&varname, rettv, TRUE) == OK)
11758 done = TRUE;
11759 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011760 else if (STRCMP(varname, "changedtick") == 0)
11761 {
11762 rettv->v_type = VAR_NUMBER;
11763 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011764 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011765 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011766 else
11767 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011768 /* Look up the variable. */
11769 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11770 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11771 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011772 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011773 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011774 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011775 done = TRUE;
11776 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011778
11779 /* restore previous notion of curbuf */
11780 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781 }
11782
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011783 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11784 /* use the default value */
11785 copy_tv(&argvars[2], rettv);
11786
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787 --emsg_off;
11788}
11789
11790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 * "getchar()" function
11792 */
11793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
11798 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011799 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011801 /* Position the cursor. Needed after a message that ends in a space. */
11802 windgoto(msg_row, msg_col);
11803
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 ++no_mapping;
11805 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011806 for (;;)
11807 {
11808 if (argvars[0].v_type == VAR_UNKNOWN)
11809 /* getchar(): blocking wait. */
11810 n = safe_vgetc();
11811 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11812 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011813 n = vpeekc_any();
11814 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011815 /* illegal argument or getchar(0) and no char avail: return zero */
11816 n = 0;
11817 else
11818 /* getchar(0) and char avail: return char */
11819 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011820
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011821 if (n == K_IGNORE)
11822 continue;
11823 break;
11824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 --no_mapping;
11826 --allow_keys;
11827
Bram Moolenaar219b8702006-11-01 14:32:36 +000011828 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11829 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11830 vimvars[VV_MOUSE_COL].vv_nr = 0;
11831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 if (IS_SPECIAL(n) || mod_mask != 0)
11834 {
11835 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11836 int i = 0;
11837
11838 /* Turn a special key into three bytes, plus modifier. */
11839 if (mod_mask != 0)
11840 {
11841 temp[i++] = K_SPECIAL;
11842 temp[i++] = KS_MODIFIER;
11843 temp[i++] = mod_mask;
11844 }
11845 if (IS_SPECIAL(n))
11846 {
11847 temp[i++] = K_SPECIAL;
11848 temp[i++] = K_SECOND(n);
11849 temp[i++] = K_THIRD(n);
11850 }
11851#ifdef FEAT_MBYTE
11852 else if (has_mbyte)
11853 i += (*mb_char2bytes)(n, temp + i);
11854#endif
11855 else
11856 temp[i++] = n;
11857 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 rettv->v_type = VAR_STRING;
11859 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011860
11861#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011862 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011863 {
11864 int row = mouse_row;
11865 int col = mouse_col;
11866 win_T *win;
11867 linenr_T lnum;
11868# ifdef FEAT_WINDOWS
11869 win_T *wp;
11870# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011871 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011872
11873 if (row >= 0 && col >= 0)
11874 {
11875 /* Find the window at the mouse coordinates and compute the
11876 * text position. */
11877 win = mouse_find_win(&row, &col);
11878 (void)mouse_comp_pos(win, &row, &col, &lnum);
11879# ifdef FEAT_WINDOWS
11880 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011881 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011882# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011883 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011884 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11885 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11886 }
11887 }
11888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 }
11890}
11891
11892/*
11893 * "getcharmod()" function
11894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011897 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901}
11902
11903/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011904 * "getcharsearch()" function
11905 */
11906 static void
11907f_getcharsearch(argvars, rettv)
11908 typval_T *argvars UNUSED;
11909 typval_T *rettv;
11910{
11911 if (rettv_dict_alloc(rettv) != FAIL)
11912 {
11913 dict_T *dict = rettv->vval.v_dict;
11914
11915 dict_add_nr_str(dict, "char", 0L, last_csearch());
11916 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11917 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11918 }
11919}
11920
11921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 * "getcmdline()" function
11923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931}
11932
11933/*
11934 * "getcmdpos()" function
11935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011938 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942}
11943
11944/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011945 * "getcmdtype()" function
11946 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011947 static void
11948f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011949 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011950 typval_T *rettv;
11951{
11952 rettv->v_type = VAR_STRING;
11953 rettv->vval.v_string = alloc(2);
11954 if (rettv->vval.v_string != NULL)
11955 {
11956 rettv->vval.v_string[0] = get_cmdline_type();
11957 rettv->vval.v_string[1] = NUL;
11958 }
11959}
11960
11961/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011962 * "getcmdwintype()" function
11963 */
11964 static void
11965f_getcmdwintype(argvars, rettv)
11966 typval_T *argvars UNUSED;
11967 typval_T *rettv;
11968{
11969 rettv->v_type = VAR_STRING;
11970 rettv->vval.v_string = NULL;
11971#ifdef FEAT_CMDWIN
11972 rettv->vval.v_string = alloc(2);
11973 if (rettv->vval.v_string != NULL)
11974 {
11975 rettv->vval.v_string[0] = cmdwin_type;
11976 rettv->vval.v_string[1] = NUL;
11977 }
11978#endif
11979}
11980
11981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 * "getcwd()" function
11983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011989 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011992 rettv->vval.v_string = NULL;
11993 cwd = alloc(MAXPATHL);
11994 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011996 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11997 {
11998 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020012000 if (rettv->vval.v_string != NULL)
12001 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020012003 }
12004 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 }
12006}
12007
12008/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012009 * "getfontname()" function
12010 */
12011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->v_type = VAR_STRING;
12017 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012018#ifdef FEAT_GUI
12019 if (gui.in_use)
12020 {
12021 GuiFont font;
12022 char_u *name = NULL;
12023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012024 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012025 {
12026 /* Get the "Normal" font. Either the name saved by
12027 * hl_set_font_name() or from the font ID. */
12028 font = gui.norm_font;
12029 name = hl_get_font_name();
12030 }
12031 else
12032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012034 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12035 return;
12036 font = gui_mch_get_font(name, FALSE);
12037 if (font == NOFONT)
12038 return; /* Invalid font name, return empty string. */
12039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012041 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012042 gui_mch_free_font(font);
12043 }
12044#endif
12045}
12046
12047/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012048 * "getfperm({fname})" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012054{
12055 char_u *fname;
12056 struct stat st;
12057 char_u *perm = NULL;
12058 char_u flags[] = "rwx";
12059 int i;
12060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012064 if (mch_stat((char *)fname, &st) >= 0)
12065 {
12066 perm = vim_strsave((char_u *)"---------");
12067 if (perm != NULL)
12068 {
12069 for (i = 0; i < 9; i++)
12070 {
12071 if (st.st_mode & (1 << (8 - i)))
12072 perm[i] = flags[i % 3];
12073 }
12074 }
12075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012077}
12078
12079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 * "getfsize({fname})" function
12081 */
12082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 typval_T *argvars;
12085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086{
12087 char_u *fname;
12088 struct stat st;
12089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
12094 if (mch_stat((char *)fname, &st) >= 0)
12095 {
12096 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012101
12102 /* non-perfect check for overflow */
12103 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12104 rettv->vval.v_number = -2;
12105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 }
12107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109}
12110
12111/*
12112 * "getftime({fname})" function
12113 */
12114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 typval_T *argvars;
12117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118{
12119 char_u *fname;
12120 struct stat st;
12121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123
12124 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128}
12129
12130/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012131 * "getftype({fname})" function
12132 */
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012137{
12138 char_u *fname;
12139 struct stat st;
12140 char_u *type = NULL;
12141 char *t;
12142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012146 if (mch_lstat((char *)fname, &st) >= 0)
12147 {
12148#ifdef S_ISREG
12149 if (S_ISREG(st.st_mode))
12150 t = "file";
12151 else if (S_ISDIR(st.st_mode))
12152 t = "dir";
12153# ifdef S_ISLNK
12154 else if (S_ISLNK(st.st_mode))
12155 t = "link";
12156# endif
12157# ifdef S_ISBLK
12158 else if (S_ISBLK(st.st_mode))
12159 t = "bdev";
12160# endif
12161# ifdef S_ISCHR
12162 else if (S_ISCHR(st.st_mode))
12163 t = "cdev";
12164# endif
12165# ifdef S_ISFIFO
12166 else if (S_ISFIFO(st.st_mode))
12167 t = "fifo";
12168# endif
12169# ifdef S_ISSOCK
12170 else if (S_ISSOCK(st.st_mode))
12171 t = "fifo";
12172# endif
12173 else
12174 t = "other";
12175#else
12176# ifdef S_IFMT
12177 switch (st.st_mode & S_IFMT)
12178 {
12179 case S_IFREG: t = "file"; break;
12180 case S_IFDIR: t = "dir"; break;
12181# ifdef S_IFLNK
12182 case S_IFLNK: t = "link"; break;
12183# endif
12184# ifdef S_IFBLK
12185 case S_IFBLK: t = "bdev"; break;
12186# endif
12187# ifdef S_IFCHR
12188 case S_IFCHR: t = "cdev"; break;
12189# endif
12190# ifdef S_IFIFO
12191 case S_IFIFO: t = "fifo"; break;
12192# endif
12193# ifdef S_IFSOCK
12194 case S_IFSOCK: t = "socket"; break;
12195# endif
12196 default: t = "other";
12197 }
12198# else
12199 if (mch_isdir(fname))
12200 t = "dir";
12201 else
12202 t = "file";
12203# endif
12204#endif
12205 type = vim_strsave((char_u *)t);
12206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012208}
12209
12210/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012211 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012212 */
12213 static void
12214f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012215 typval_T *argvars;
12216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217{
12218 linenr_T lnum;
12219 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012220 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012221
12222 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012223 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012224 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012225 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012226 retlist = FALSE;
12227 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012229 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012231 retlist = TRUE;
12232 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012233
Bram Moolenaar342337a2005-07-21 21:11:17 +000012234 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012235}
12236
12237/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012238 * "getmatches()" function
12239 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012240 static void
12241f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012242 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012243 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012244{
12245#ifdef FEAT_SEARCH_EXTRA
12246 dict_T *dict;
12247 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012248 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012249
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012250 if (rettv_list_alloc(rettv) == OK)
12251 {
12252 while (cur != NULL)
12253 {
12254 dict = dict_alloc();
12255 if (dict == NULL)
12256 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012257 if (cur->match.regprog == NULL)
12258 {
12259 /* match added with matchaddpos() */
12260 for (i = 0; i < MAXPOSMATCH; ++i)
12261 {
12262 llpos_T *llpos;
12263 char buf[6];
12264 list_T *l;
12265
12266 llpos = &cur->pos.pos[i];
12267 if (llpos->lnum == 0)
12268 break;
12269 l = list_alloc();
12270 if (l == NULL)
12271 break;
12272 list_append_number(l, (varnumber_T)llpos->lnum);
12273 if (llpos->col > 0)
12274 {
12275 list_append_number(l, (varnumber_T)llpos->col);
12276 list_append_number(l, (varnumber_T)llpos->len);
12277 }
12278 sprintf(buf, "pos%d", i + 1);
12279 dict_add_list(dict, buf, l);
12280 }
12281 }
12282 else
12283 {
12284 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12285 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012286 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012287 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12288 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012289# ifdef FEAT_CONCEAL
12290 if (cur->conceal_char)
12291 {
12292 char_u buf[MB_MAXBYTES + 1];
12293
12294 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12295 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12296 }
12297# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012298 list_append_dict(rettv->vval.v_list, dict);
12299 cur = cur->next;
12300 }
12301 }
12302#endif
12303}
12304
12305/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012306 * "getpid()" function
12307 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012308 static void
12309f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012310 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012311 typval_T *rettv;
12312{
12313 rettv->vval.v_number = mch_get_pid();
12314}
12315
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012316static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12317
12318/*
12319 * "getcurpos()" function
12320 */
12321 static void
12322f_getcurpos(argvars, rettv)
12323 typval_T *argvars;
12324 typval_T *rettv;
12325{
12326 getpos_both(argvars, rettv, TRUE);
12327}
12328
Bram Moolenaar18081e32008-02-20 19:11:07 +000012329/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012330 * "getpos(string)" function
12331 */
12332 static void
12333f_getpos(argvars, rettv)
12334 typval_T *argvars;
12335 typval_T *rettv;
12336{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012337 getpos_both(argvars, rettv, FALSE);
12338}
12339
12340 static void
12341getpos_both(argvars, rettv, getcurpos)
12342 typval_T *argvars;
12343 typval_T *rettv;
12344 int getcurpos;
12345{
Bram Moolenaara5525202006-03-02 22:52:09 +000012346 pos_T *fp;
12347 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012348 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012349
12350 if (rettv_list_alloc(rettv) == OK)
12351 {
12352 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012353 if (getcurpos)
12354 fp = &curwin->w_cursor;
12355 else
12356 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012357 if (fnum != -1)
12358 list_append_number(l, (varnumber_T)fnum);
12359 else
12360 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012361 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12362 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012363 list_append_number(l, (fp != NULL)
12364 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012365 : (varnumber_T)0);
12366 list_append_number(l,
12367#ifdef FEAT_VIRTUALEDIT
12368 (fp != NULL) ? (varnumber_T)fp->coladd :
12369#endif
12370 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012371 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012372 list_append_number(l, curwin->w_curswant == MAXCOL ?
12373 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012374 }
12375 else
12376 rettv->vval.v_number = FALSE;
12377}
12378
12379/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012380 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012381 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012382 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012383f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012384 typval_T *argvars UNUSED;
12385 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012386{
12387#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012388 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012389#endif
12390
Bram Moolenaar2641f772005-03-25 21:58:17 +000012391#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012392 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012393 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012394 wp = NULL;
12395 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12396 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012397 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012398 if (wp == NULL)
12399 return;
12400 }
12401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012402 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012403 }
12404#endif
12405}
12406
12407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 * "getreg()" function
12409 */
12410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012412 typval_T *argvars;
12413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414{
12415 char_u *strregname;
12416 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012417 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012418 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012423 strregname = get_tv_string_chk(&argvars[0]);
12424 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012425 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012428 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12429 return_list = get_tv_number_chk(&argvars[2], &error);
12430 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012433 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012434
12435 if (error)
12436 return;
12437
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 regname = (strregname == NULL ? '"' : *strregname);
12439 if (regname == 0)
12440 regname = '"';
12441
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012442 if (return_list)
12443 {
12444 rettv->v_type = VAR_LIST;
12445 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12446 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012447 if (rettv->vval.v_list != NULL)
12448 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012449 }
12450 else
12451 {
12452 rettv->v_type = VAR_STRING;
12453 rettv->vval.v_string = get_reg_contents(regname,
12454 arg2 ? GREG_EXPR_SRC : 0);
12455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456}
12457
12458/*
12459 * "getregtype()" function
12460 */
12461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
12466 char_u *strregname;
12467 int regname;
12468 char_u buf[NUMBUFLEN + 2];
12469 long reglen = 0;
12470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012471 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 {
12473 strregname = get_tv_string_chk(&argvars[0]);
12474 if (strregname == NULL) /* type error; errmsg already given */
12475 {
12476 rettv->v_type = VAR_STRING;
12477 rettv->vval.v_string = NULL;
12478 return;
12479 }
12480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 else
12482 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012483 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484
12485 regname = (strregname == NULL ? '"' : *strregname);
12486 if (regname == 0)
12487 regname = '"';
12488
12489 buf[0] = NUL;
12490 buf[1] = NUL;
12491 switch (get_reg_type(regname, &reglen))
12492 {
12493 case MLINE: buf[0] = 'V'; break;
12494 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 case MBLOCK:
12496 buf[0] = Ctrl_V;
12497 sprintf((char *)buf + 1, "%ld", reglen + 1);
12498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->v_type = VAR_STRING;
12501 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012505 * "gettabvar()" function
12506 */
12507 static void
12508f_gettabvar(argvars, rettv)
12509 typval_T *argvars;
12510 typval_T *rettv;
12511{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012512 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012513 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012514 dictitem_T *v;
12515 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012516 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012517
12518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = NULL;
12520
12521 varname = get_tv_string_chk(&argvars[1]);
12522 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12523 if (tp != NULL && varname != NULL)
12524 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012525 /* Set tp to be our tabpage, temporarily. Also set the window to the
12526 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012527 if (switch_win(&oldcurwin, &oldtabpage,
12528 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012529 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012530 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012531 /* look up the variable */
12532 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12533 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12534 if (v != NULL)
12535 {
12536 copy_tv(&v->di_tv, rettv);
12537 done = TRUE;
12538 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012539 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012540
12541 /* restore previous notion of curwin */
12542 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012543 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544
12545 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012546 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012547}
12548
12549/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012550 * "gettabwinvar()" function
12551 */
12552 static void
12553f_gettabwinvar(argvars, rettv)
12554 typval_T *argvars;
12555 typval_T *rettv;
12556{
12557 getwinvar(argvars, rettv, 1);
12558}
12559
12560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 * "getwinposx()" function
12562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012565 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569#ifdef FEAT_GUI
12570 if (gui.in_use)
12571 {
12572 int x, y;
12573
12574 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 }
12577#endif
12578}
12579
12580/*
12581 * "getwinposy()" function
12582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589#ifdef FEAT_GUI
12590 if (gui.in_use)
12591 {
12592 int x, y;
12593
12594 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012595 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 }
12597#endif
12598}
12599
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012600/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012601 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012602 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012603 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012604find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012605 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012606 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012607{
12608#ifdef FEAT_WINDOWS
12609 win_T *wp;
12610#endif
12611 int nr;
12612
12613 nr = get_tv_number_chk(vp, NULL);
12614
12615#ifdef FEAT_WINDOWS
12616 if (nr < 0)
12617 return NULL;
12618 if (nr == 0)
12619 return curwin;
12620
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012621 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12622 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012623 if (--nr <= 0)
12624 break;
12625 return wp;
12626#else
12627 if (nr == 0 || nr == 1)
12628 return curwin;
12629 return NULL;
12630#endif
12631}
12632
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633/*
12634 * "getwinvar()" function
12635 */
12636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *argvars;
12639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012641 getwinvar(argvars, rettv, 0);
12642}
12643
12644/*
12645 * getwinvar() and gettabwinvar()
12646 */
12647 static void
12648getwinvar(argvars, rettv, off)
12649 typval_T *argvars;
12650 typval_T *rettv;
12651 int off; /* 1 for gettabwinvar() */
12652{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012653 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012656 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012657 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012658#ifdef FEAT_WINDOWS
12659 win_T *oldcurwin;
12660 tabpage_T *oldtabpage;
12661 int need_switch_win;
12662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012664#ifdef FEAT_WINDOWS
12665 if (off == 1)
12666 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12667 else
12668 tp = curtab;
12669#endif
12670 win = find_win_by_nr(&argvars[off], tp);
12671 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012674 rettv->v_type = VAR_STRING;
12675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
12677 if (win != NULL && varname != NULL)
12678 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012679#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012680 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012681 * otherwise the window is not valid. Only do this when needed,
12682 * autocommands get blocked. */
12683 need_switch_win = !(tp == curtab && win == curwin);
12684 if (!need_switch_win
12685 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12686#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012687 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012688 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012689 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012690 if (get_option_tv(&varname, rettv, 1) == OK)
12691 done = TRUE;
12692 }
12693 else
12694 {
12695 /* Look up the variable. */
12696 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12697 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12698 varname, FALSE);
12699 if (v != NULL)
12700 {
12701 copy_tv(&v->di_tv, rettv);
12702 done = TRUE;
12703 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012706
Bram Moolenaarba117c22015-09-29 16:53:22 +020012707#ifdef FEAT_WINDOWS
12708 if (need_switch_win)
12709 /* restore previous notion of curwin */
12710 restore_win(oldcurwin, oldtabpage, TRUE);
12711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 }
12713
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012714 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12715 /* use the default return value */
12716 copy_tv(&argvars[off + 2], rettv);
12717
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 --emsg_off;
12719}
12720
12721/*
12722 * "glob()" function
12723 */
12724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 typval_T *argvars;
12727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012729 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012731 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012733 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012734 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012736 if (argvars[1].v_type != VAR_UNKNOWN)
12737 {
12738 if (get_tv_number_chk(&argvars[1], &error))
12739 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012740 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012741 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012742 if (get_tv_number_chk(&argvars[2], &error))
12743 {
12744 rettv->v_type = VAR_LIST;
12745 rettv->vval.v_list = NULL;
12746 }
12747 if (argvars[3].v_type != VAR_UNKNOWN
12748 && get_tv_number_chk(&argvars[3], &error))
12749 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012750 }
12751 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012752 if (!error)
12753 {
12754 ExpandInit(&xpc);
12755 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012756 if (p_wic)
12757 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012758 if (rettv->v_type == VAR_STRING)
12759 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012760 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012761 else if (rettv_list_alloc(rettv) != FAIL)
12762 {
12763 int i;
12764
12765 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12766 NULL, options, WILD_ALL_KEEP);
12767 for (i = 0; i < xpc.xp_numfiles; i++)
12768 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12769
12770 ExpandCleanup(&xpc);
12771 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012772 }
12773 else
12774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775}
12776
12777/*
12778 * "globpath()" function
12779 */
12780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *argvars;
12783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012785 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012788 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012789 garray_T ga;
12790 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012792 /* When the optional second argument is non-zero, don't remove matches
12793 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012795 if (argvars[2].v_type != VAR_UNKNOWN)
12796 {
12797 if (get_tv_number_chk(&argvars[2], &error))
12798 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012799 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012800 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012801 if (get_tv_number_chk(&argvars[3], &error))
12802 {
12803 rettv->v_type = VAR_LIST;
12804 rettv->vval.v_list = NULL;
12805 }
12806 if (argvars[4].v_type != VAR_UNKNOWN
12807 && get_tv_number_chk(&argvars[4], &error))
12808 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012809 }
12810 }
12811 if (file != NULL && !error)
12812 {
12813 ga_init2(&ga, (int)sizeof(char_u *), 10);
12814 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12815 if (rettv->v_type == VAR_STRING)
12816 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12817 else if (rettv_list_alloc(rettv) != FAIL)
12818 for (i = 0; i < ga.ga_len; ++i)
12819 list_append_string(rettv->vval.v_list,
12820 ((char_u **)(ga.ga_data))[i], -1);
12821 ga_clear_strings(&ga);
12822 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825}
12826
12827/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012828 * "glob2regpat()" function
12829 */
12830 static void
12831f_glob2regpat(argvars, rettv)
12832 typval_T *argvars;
12833 typval_T *rettv;
12834{
12835 char_u *pat = get_tv_string_chk(&argvars[0]);
12836
12837 rettv->v_type = VAR_STRING;
12838 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "has()" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 int i;
12850 char_u *name;
12851 int n = FALSE;
12852 static char *(has_list[]) =
12853 {
12854#ifdef AMIGA
12855 "amiga",
12856# ifdef FEAT_ARP
12857 "arp",
12858# endif
12859#endif
12860#ifdef __BEOS__
12861 "beos",
12862#endif
12863#ifdef MSDOS
12864# ifdef DJGPP
12865 "dos32",
12866# else
12867 "dos16",
12868# endif
12869#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012870#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 "mac",
12872#endif
12873#if defined(MACOS_X_UNIX)
12874 "macunix",
12875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876#ifdef __QNX__
12877 "qnx",
12878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#ifdef UNIX
12880 "unix",
12881#endif
12882#ifdef VMS
12883 "vms",
12884#endif
12885#ifdef WIN16
12886 "win16",
12887#endif
12888#ifdef WIN32
12889 "win32",
12890#endif
12891#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12892 "win32unix",
12893#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012894#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 "win64",
12896#endif
12897#ifdef EBCDIC
12898 "ebcdic",
12899#endif
12900#ifndef CASE_INSENSITIVE_FILENAME
12901 "fname_case",
12902#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012903#ifdef HAVE_ACL
12904 "acl",
12905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#ifdef FEAT_ARABIC
12907 "arabic",
12908#endif
12909#ifdef FEAT_AUTOCMD
12910 "autocmd",
12911#endif
12912#ifdef FEAT_BEVAL
12913 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012914# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12915 "balloon_multiline",
12916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917#endif
12918#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12919 "builtin_terms",
12920# ifdef ALL_BUILTIN_TCAPS
12921 "all_builtin_terms",
12922# endif
12923#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012924#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12925 || defined(FEAT_GUI_W32) \
12926 || defined(FEAT_GUI_MOTIF))
12927 "browsefilter",
12928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929#ifdef FEAT_BYTEOFF
12930 "byte_offset",
12931#endif
12932#ifdef FEAT_CINDENT
12933 "cindent",
12934#endif
12935#ifdef FEAT_CLIENTSERVER
12936 "clientserver",
12937#endif
12938#ifdef FEAT_CLIPBOARD
12939 "clipboard",
12940#endif
12941#ifdef FEAT_CMDL_COMPL
12942 "cmdline_compl",
12943#endif
12944#ifdef FEAT_CMDHIST
12945 "cmdline_hist",
12946#endif
12947#ifdef FEAT_COMMENTS
12948 "comments",
12949#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012950#ifdef FEAT_CONCEAL
12951 "conceal",
12952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953#ifdef FEAT_CRYPT
12954 "cryptv",
12955#endif
12956#ifdef FEAT_CSCOPE
12957 "cscope",
12958#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012959#ifdef FEAT_CURSORBIND
12960 "cursorbind",
12961#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012962#ifdef CURSOR_SHAPE
12963 "cursorshape",
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965#ifdef DEBUG
12966 "debug",
12967#endif
12968#ifdef FEAT_CON_DIALOG
12969 "dialog_con",
12970#endif
12971#ifdef FEAT_GUI_DIALOG
12972 "dialog_gui",
12973#endif
12974#ifdef FEAT_DIFF
12975 "diff",
12976#endif
12977#ifdef FEAT_DIGRAPHS
12978 "digraphs",
12979#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012980#ifdef FEAT_DIRECTX
12981 "directx",
12982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef FEAT_DND
12984 "dnd",
12985#endif
12986#ifdef FEAT_EMACS_TAGS
12987 "emacs_tags",
12988#endif
12989 "eval", /* always present, of course! */
12990#ifdef FEAT_EX_EXTRA
12991 "ex_extra",
12992#endif
12993#ifdef FEAT_SEARCH_EXTRA
12994 "extra_search",
12995#endif
12996#ifdef FEAT_FKMAP
12997 "farsi",
12998#endif
12999#ifdef FEAT_SEARCHPATH
13000 "file_in_path",
13001#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013002#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013003 "filterpipe",
13004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#ifdef FEAT_FIND_ID
13006 "find_in_path",
13007#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013008#ifdef FEAT_FLOAT
13009 "float",
13010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011#ifdef FEAT_FOLDING
13012 "folding",
13013#endif
13014#ifdef FEAT_FOOTER
13015 "footer",
13016#endif
13017#if !defined(USE_SYSTEM) && defined(UNIX)
13018 "fork",
13019#endif
13020#ifdef FEAT_GETTEXT
13021 "gettext",
13022#endif
13023#ifdef FEAT_GUI
13024 "gui",
13025#endif
13026#ifdef FEAT_GUI_ATHENA
13027# ifdef FEAT_GUI_NEXTAW
13028 "gui_neXtaw",
13029# else
13030 "gui_athena",
13031# endif
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_GUI_GTK
13034 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013037#ifdef FEAT_GUI_GNOME
13038 "gui_gnome",
13039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040#ifdef FEAT_GUI_MAC
13041 "gui_mac",
13042#endif
13043#ifdef FEAT_GUI_MOTIF
13044 "gui_motif",
13045#endif
13046#ifdef FEAT_GUI_PHOTON
13047 "gui_photon",
13048#endif
13049#ifdef FEAT_GUI_W16
13050 "gui_win16",
13051#endif
13052#ifdef FEAT_GUI_W32
13053 "gui_win32",
13054#endif
13055#ifdef FEAT_HANGULIN
13056 "hangul_input",
13057#endif
13058#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13059 "iconv",
13060#endif
13061#ifdef FEAT_INS_EXPAND
13062 "insert_expand",
13063#endif
13064#ifdef FEAT_JUMPLIST
13065 "jumplist",
13066#endif
13067#ifdef FEAT_KEYMAP
13068 "keymap",
13069#endif
13070#ifdef FEAT_LANGMAP
13071 "langmap",
13072#endif
13073#ifdef FEAT_LIBCALL
13074 "libcall",
13075#endif
13076#ifdef FEAT_LINEBREAK
13077 "linebreak",
13078#endif
13079#ifdef FEAT_LISP
13080 "lispindent",
13081#endif
13082#ifdef FEAT_LISTCMDS
13083 "listcmds",
13084#endif
13085#ifdef FEAT_LOCALMAP
13086 "localmap",
13087#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013088#ifdef FEAT_LUA
13089# ifndef DYNAMIC_LUA
13090 "lua",
13091# endif
13092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093#ifdef FEAT_MENU
13094 "menu",
13095#endif
13096#ifdef FEAT_SESSION
13097 "mksession",
13098#endif
13099#ifdef FEAT_MODIFY_FNAME
13100 "modify_fname",
13101#endif
13102#ifdef FEAT_MOUSE
13103 "mouse",
13104#endif
13105#ifdef FEAT_MOUSESHAPE
13106 "mouseshape",
13107#endif
13108#if defined(UNIX) || defined(VMS)
13109# ifdef FEAT_MOUSE_DEC
13110 "mouse_dec",
13111# endif
13112# ifdef FEAT_MOUSE_GPM
13113 "mouse_gpm",
13114# endif
13115# ifdef FEAT_MOUSE_JSB
13116 "mouse_jsbterm",
13117# endif
13118# ifdef FEAT_MOUSE_NET
13119 "mouse_netterm",
13120# endif
13121# ifdef FEAT_MOUSE_PTERM
13122 "mouse_pterm",
13123# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013124# ifdef FEAT_MOUSE_SGR
13125 "mouse_sgr",
13126# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013127# ifdef FEAT_SYSMOUSE
13128 "mouse_sysmouse",
13129# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013130# ifdef FEAT_MOUSE_URXVT
13131 "mouse_urxvt",
13132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133# ifdef FEAT_MOUSE_XTERM
13134 "mouse_xterm",
13135# endif
13136#endif
13137#ifdef FEAT_MBYTE
13138 "multi_byte",
13139#endif
13140#ifdef FEAT_MBYTE_IME
13141 "multi_byte_ime",
13142#endif
13143#ifdef FEAT_MULTI_LANG
13144 "multi_lang",
13145#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013146#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013147#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013148 "mzscheme",
13149#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151#ifdef FEAT_OLE
13152 "ole",
13153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154#ifdef FEAT_PATH_EXTRA
13155 "path_extra",
13156#endif
13157#ifdef FEAT_PERL
13158#ifndef DYNAMIC_PERL
13159 "perl",
13160#endif
13161#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013162#ifdef FEAT_PERSISTENT_UNDO
13163 "persistent_undo",
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165#ifdef FEAT_PYTHON
13166#ifndef DYNAMIC_PYTHON
13167 "python",
13168#endif
13169#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013170#ifdef FEAT_PYTHON3
13171#ifndef DYNAMIC_PYTHON3
13172 "python3",
13173#endif
13174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175#ifdef FEAT_POSTSCRIPT
13176 "postscript",
13177#endif
13178#ifdef FEAT_PRINTER
13179 "printer",
13180#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013181#ifdef FEAT_PROFILE
13182 "profile",
13183#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013184#ifdef FEAT_RELTIME
13185 "reltime",
13186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187#ifdef FEAT_QUICKFIX
13188 "quickfix",
13189#endif
13190#ifdef FEAT_RIGHTLEFT
13191 "rightleft",
13192#endif
13193#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13194 "ruby",
13195#endif
13196#ifdef FEAT_SCROLLBIND
13197 "scrollbind",
13198#endif
13199#ifdef FEAT_CMDL_INFO
13200 "showcmd",
13201 "cmdline_info",
13202#endif
13203#ifdef FEAT_SIGNS
13204 "signs",
13205#endif
13206#ifdef FEAT_SMARTINDENT
13207 "smartindent",
13208#endif
13209#ifdef FEAT_SNIFF
13210 "sniff",
13211#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013212#ifdef STARTUPTIME
13213 "startuptime",
13214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215#ifdef FEAT_STL_OPT
13216 "statusline",
13217#endif
13218#ifdef FEAT_SUN_WORKSHOP
13219 "sun_workshop",
13220#endif
13221#ifdef FEAT_NETBEANS_INTG
13222 "netbeans_intg",
13223#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013224#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013225 "spell",
13226#endif
13227#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 "syntax",
13229#endif
13230#if defined(USE_SYSTEM) || !defined(UNIX)
13231 "system",
13232#endif
13233#ifdef FEAT_TAG_BINS
13234 "tag_binary",
13235#endif
13236#ifdef FEAT_TAG_OLDSTATIC
13237 "tag_old_static",
13238#endif
13239#ifdef FEAT_TAG_ANYWHITE
13240 "tag_any_white",
13241#endif
13242#ifdef FEAT_TCL
13243# ifndef DYNAMIC_TCL
13244 "tcl",
13245# endif
13246#endif
13247#ifdef TERMINFO
13248 "terminfo",
13249#endif
13250#ifdef FEAT_TERMRESPONSE
13251 "termresponse",
13252#endif
13253#ifdef FEAT_TEXTOBJ
13254 "textobjects",
13255#endif
13256#ifdef HAVE_TGETENT
13257 "tgetent",
13258#endif
13259#ifdef FEAT_TITLE
13260 "title",
13261#endif
13262#ifdef FEAT_TOOLBAR
13263 "toolbar",
13264#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013265#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13266 "unnamedplus",
13267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268#ifdef FEAT_USR_CMDS
13269 "user-commands", /* was accidentally included in 5.4 */
13270 "user_commands",
13271#endif
13272#ifdef FEAT_VIMINFO
13273 "viminfo",
13274#endif
13275#ifdef FEAT_VERTSPLIT
13276 "vertsplit",
13277#endif
13278#ifdef FEAT_VIRTUALEDIT
13279 "virtualedit",
13280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282#ifdef FEAT_VISUALEXTRA
13283 "visualextra",
13284#endif
13285#ifdef FEAT_VREPLACE
13286 "vreplace",
13287#endif
13288#ifdef FEAT_WILDIGN
13289 "wildignore",
13290#endif
13291#ifdef FEAT_WILDMENU
13292 "wildmenu",
13293#endif
13294#ifdef FEAT_WINDOWS
13295 "windows",
13296#endif
13297#ifdef FEAT_WAK
13298 "winaltkeys",
13299#endif
13300#ifdef FEAT_WRITEBACKUP
13301 "writebackup",
13302#endif
13303#ifdef FEAT_XIM
13304 "xim",
13305#endif
13306#ifdef FEAT_XFONTSET
13307 "xfontset",
13308#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013309#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013310 "xpm",
13311 "xpm_w32", /* for backward compatibility */
13312#else
13313# if defined(HAVE_XPM)
13314 "xpm",
13315# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317#ifdef USE_XSMP
13318 "xsmp",
13319#endif
13320#ifdef USE_XSMP_INTERACT
13321 "xsmp_interact",
13322#endif
13323#ifdef FEAT_XCLIPBOARD
13324 "xterm_clipboard",
13325#endif
13326#ifdef FEAT_XTERM_SAVE
13327 "xterm_save",
13328#endif
13329#if defined(UNIX) && defined(FEAT_X11)
13330 "X11",
13331#endif
13332 NULL
13333 };
13334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 for (i = 0; has_list[i] != NULL; ++i)
13337 if (STRICMP(name, has_list[i]) == 0)
13338 {
13339 n = TRUE;
13340 break;
13341 }
13342
13343 if (n == FALSE)
13344 {
13345 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013346 {
13347 if (name[5] == '-'
13348 && STRLEN(name) > 11
13349 && vim_isdigit(name[6])
13350 && vim_isdigit(name[8])
13351 && vim_isdigit(name[10]))
13352 {
13353 int major = atoi((char *)name + 6);
13354 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013355
13356 /* Expect "patch-9.9.01234". */
13357 n = (major < VIM_VERSION_MAJOR
13358 || (major == VIM_VERSION_MAJOR
13359 && (minor < VIM_VERSION_MINOR
13360 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013361 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013362 }
13363 else
13364 n = has_patch(atoi((char *)name + 5));
13365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 else if (STRICMP(name, "vim_starting") == 0)
13367 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013368#ifdef FEAT_MBYTE
13369 else if (STRICMP(name, "multi_byte_encoding") == 0)
13370 n = has_mbyte;
13371#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013372#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13373 else if (STRICMP(name, "balloon_multiline") == 0)
13374 n = multiline_balloon_available();
13375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376#ifdef DYNAMIC_TCL
13377 else if (STRICMP(name, "tcl") == 0)
13378 n = tcl_enabled(FALSE);
13379#endif
13380#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13381 else if (STRICMP(name, "iconv") == 0)
13382 n = iconv_enabled(FALSE);
13383#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013384#ifdef DYNAMIC_LUA
13385 else if (STRICMP(name, "lua") == 0)
13386 n = lua_enabled(FALSE);
13387#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013388#ifdef DYNAMIC_MZSCHEME
13389 else if (STRICMP(name, "mzscheme") == 0)
13390 n = mzscheme_enabled(FALSE);
13391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392#ifdef DYNAMIC_RUBY
13393 else if (STRICMP(name, "ruby") == 0)
13394 n = ruby_enabled(FALSE);
13395#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013396#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397#ifdef DYNAMIC_PYTHON
13398 else if (STRICMP(name, "python") == 0)
13399 n = python_enabled(FALSE);
13400#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013401#endif
13402#ifdef FEAT_PYTHON3
13403#ifdef DYNAMIC_PYTHON3
13404 else if (STRICMP(name, "python3") == 0)
13405 n = python3_enabled(FALSE);
13406#endif
13407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408#ifdef DYNAMIC_PERL
13409 else if (STRICMP(name, "perl") == 0)
13410 n = perl_enabled(FALSE);
13411#endif
13412#ifdef FEAT_GUI
13413 else if (STRICMP(name, "gui_running") == 0)
13414 n = (gui.in_use || gui.starting);
13415# ifdef FEAT_GUI_W32
13416 else if (STRICMP(name, "gui_win32s") == 0)
13417 n = gui_is_win32s();
13418# endif
13419# ifdef FEAT_BROWSE
13420 else if (STRICMP(name, "browse") == 0)
13421 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13422# endif
13423#endif
13424#ifdef FEAT_SYN_HL
13425 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013426 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427#endif
13428#if defined(WIN3264)
13429 else if (STRICMP(name, "win95") == 0)
13430 n = mch_windows95();
13431#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013432#ifdef FEAT_NETBEANS_INTG
13433 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013434 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 }
13437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439}
13440
13441/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013442 * "has_key()" function
13443 */
13444 static void
13445f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013446 typval_T *argvars;
13447 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013448{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013449 if (argvars[0].v_type != VAR_DICT)
13450 {
13451 EMSG(_(e_dictreq));
13452 return;
13453 }
13454 if (argvars[0].vval.v_dict == NULL)
13455 return;
13456
13457 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013458 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013459}
13460
13461/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013462 * "haslocaldir()" function
13463 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013464 static void
13465f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013466 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013467 typval_T *rettv;
13468{
13469 rettv->vval.v_number = (curwin->w_localdir != NULL);
13470}
13471
13472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 * "hasmapto()" function
13474 */
13475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013477 typval_T *argvars;
13478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479{
13480 char_u *name;
13481 char_u *mode;
13482 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013483 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 mode = (char_u *)"nvo";
13488 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013491 if (argvars[2].v_type != VAR_UNKNOWN)
13492 abbr = get_tv_number(&argvars[2]);
13493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494
Bram Moolenaar2c932302006-03-18 21:42:09 +000013495 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499}
13500
13501/*
13502 * "histadd()" function
13503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013506 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508{
13509#ifdef FEAT_CMDHIST
13510 int histype;
13511 char_u *str;
13512 char_u buf[NUMBUFLEN];
13513#endif
13514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 if (check_restricted() || check_secure())
13517 return;
13518#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13520 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 if (histype >= 0)
13522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 if (*str != NUL)
13525 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013526 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 return;
13530 }
13531 }
13532#endif
13533}
13534
13535/*
13536 * "histdel()" function
13537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013540 typval_T *argvars UNUSED;
13541 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542{
13543#ifdef FEAT_CMDHIST
13544 int n;
13545 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13549 if (str == NULL)
13550 n = 0;
13551 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 else
13559 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 get_tv_string_buf(&argvars[1], buf));
13562 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563#endif
13564}
13565
13566/*
13567 * "histget()" function
13568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573{
13574#ifdef FEAT_CMDHIST
13575 int type;
13576 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013577 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13580 if (str == NULL)
13581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 {
13584 type = get_histtype(str);
13585 if (argvars[1].v_type == VAR_UNKNOWN)
13586 idx = get_history_idx(type);
13587 else
13588 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13589 /* -1 on type error */
13590 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596}
13597
13598/*
13599 * "histnr()" function
13600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013603 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605{
13606 int i;
13607
13608#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 char_u *history = get_tv_string_chk(&argvars[0]);
13610
13611 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 if (i >= HIST_CMD && i < HIST_COUNT)
13613 i = get_history_idx(i);
13614 else
13615#endif
13616 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 * "highlightID(name)" function
13622 */
13623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013625 typval_T *argvars;
13626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629}
13630
13631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013632 * "highlight_exists()" function
13633 */
13634 static void
13635f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *argvars;
13637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638{
13639 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13640}
13641
13642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 * "hostname()" function
13644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649{
13650 char_u hostname[256];
13651
13652 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->v_type = VAR_STRING;
13654 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655}
13656
13657/*
13658 * iconv() function
13659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664{
13665#ifdef FEAT_MBYTE
13666 char_u buf1[NUMBUFLEN];
13667 char_u buf2[NUMBUFLEN];
13668 char_u *from, *to, *str;
13669 vimconv_T vimconv;
13670#endif
13671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->v_type = VAR_STRING;
13673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674
13675#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 str = get_tv_string(&argvars[0]);
13677 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13678 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 vimconv.vc_type = CONV_NONE;
13680 convert_setup(&vimconv, from, to);
13681
13682 /* If the encodings are equal, no conversion needed. */
13683 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
13688 convert_setup(&vimconv, NULL, NULL);
13689 vim_free(from);
13690 vim_free(to);
13691#endif
13692}
13693
13694/*
13695 * "indent()" function
13696 */
13697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013699 typval_T *argvars;
13700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701{
13702 linenr_T lnum;
13703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709}
13710
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013711/*
13712 * "index()" function
13713 */
13714 static void
13715f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 typval_T *argvars;
13717 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013718{
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 list_T *l;
13720 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013721 long idx = 0;
13722 int ic = FALSE;
13723
13724 rettv->vval.v_number = -1;
13725 if (argvars[0].v_type != VAR_LIST)
13726 {
13727 EMSG(_(e_listreq));
13728 return;
13729 }
13730 l = argvars[0].vval.v_list;
13731 if (l != NULL)
13732 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013733 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013734 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013736 int error = FALSE;
13737
Bram Moolenaar758711c2005-02-02 23:11:38 +000013738 /* Start at specified item. Use the cached index that list_find()
13739 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013741 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013742 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 ic = get_tv_number_chk(&argvars[3], &error);
13744 if (error)
13745 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013746 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013747
Bram Moolenaar758711c2005-02-02 23:11:38 +000013748 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013749 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013750 {
13751 rettv->vval.v_number = idx;
13752 break;
13753 }
13754 }
13755}
13756
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757static int inputsecret_flag = 0;
13758
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013759static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13760
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013762 * This function is used by f_input() and f_inputdialog() functions. The third
13763 * argument to f_input() specifies the type of completion to use at the
13764 * prompt. The third argument to f_inputdialog() specifies the value to return
13765 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 */
13767 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013768get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 typval_T *argvars;
13770 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013771 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 char_u *p = NULL;
13775 int c;
13776 char_u buf[NUMBUFLEN];
13777 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013779 int xp_type = EXPAND_NOTHING;
13780 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013783 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784
13785#ifdef NO_CONSOLE_INPUT
13786 /* While starting up, there is no place to enter text. */
13787 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789#endif
13790
13791 cmd_silent = FALSE; /* Want to see the prompt. */
13792 if (prompt != NULL)
13793 {
13794 /* Only the part of the message after the last NL is considered as
13795 * prompt for the command line */
13796 p = vim_strrchr(prompt, '\n');
13797 if (p == NULL)
13798 p = prompt;
13799 else
13800 {
13801 ++p;
13802 c = *p;
13803 *p = NUL;
13804 msg_start();
13805 msg_clr_eos();
13806 msg_puts_attr(prompt, echo_attr);
13807 msg_didout = FALSE;
13808 msg_starthere();
13809 *p = c;
13810 }
13811 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 if (argvars[1].v_type != VAR_UNKNOWN)
13814 {
13815 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13816 if (defstr != NULL)
13817 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013819 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013820 {
13821 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013822 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013823 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013824
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013825 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013826 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013827
Bram Moolenaar4463f292005-09-25 22:20:24 +000013828 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13829 if (xp_name == NULL)
13830 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013831
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013832 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013833
Bram Moolenaar4463f292005-09-25 22:20:24 +000013834 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13835 &xp_arg) == FAIL)
13836 return;
13837 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013838 }
13839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013841 {
13842# ifdef FEAT_EX_EXTRA
13843 int save_ex_normal_busy = ex_normal_busy;
13844 ex_normal_busy = 0;
13845# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013847 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13848 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013849# ifdef FEAT_EX_EXTRA
13850 ex_normal_busy = save_ex_normal_busy;
13851# endif
13852 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013853 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013854 && argvars[1].v_type != VAR_UNKNOWN
13855 && argvars[2].v_type != VAR_UNKNOWN)
13856 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13857 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013858
13859 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 /* since the user typed this, no need to wait for return */
13862 need_wait_return = FALSE;
13863 msg_didout = FALSE;
13864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 cmd_silent = cmd_silent_save;
13866}
13867
13868/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013869 * "input()" function
13870 * Also handles inputsecret() when inputsecret is set.
13871 */
13872 static void
13873f_input(argvars, rettv)
13874 typval_T *argvars;
13875 typval_T *rettv;
13876{
13877 get_user_input(argvars, rettv, FALSE);
13878}
13879
13880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 * "inputdialog()" function
13882 */
13883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 typval_T *argvars;
13886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887{
13888#if defined(FEAT_GUI_TEXTDIALOG)
13889 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13890 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13891 {
13892 char_u *message;
13893 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 message = get_tv_string_chk(&argvars[0]);
13897 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013898 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013899 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 else
13901 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013902 if (message != NULL && defstr != NULL
13903 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013904 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 else
13907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 if (message != NULL && defstr != NULL
13909 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013910 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013911 rettv->vval.v_string = vim_strsave(
13912 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 }
13918 else
13919#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013920 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921}
13922
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013923/*
13924 * "inputlist()" function
13925 */
13926 static void
13927f_inputlist(argvars, rettv)
13928 typval_T *argvars;
13929 typval_T *rettv;
13930{
13931 listitem_T *li;
13932 int selected;
13933 int mouse_used;
13934
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013935#ifdef NO_CONSOLE_INPUT
13936 /* While starting up, there is no place to enter text. */
13937 if (no_console_input())
13938 return;
13939#endif
13940 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13941 {
13942 EMSG2(_(e_listarg), "inputlist()");
13943 return;
13944 }
13945
13946 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013947 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013948 lines_left = Rows; /* avoid more prompt */
13949 msg_scroll = TRUE;
13950 msg_clr_eos();
13951
13952 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13953 {
13954 msg_puts(get_tv_string(&li->li_tv));
13955 msg_putchar('\n');
13956 }
13957
13958 /* Ask for choice. */
13959 selected = prompt_for_number(&mouse_used);
13960 if (mouse_used)
13961 selected -= lines_left;
13962
13963 rettv->vval.v_number = selected;
13964}
13965
13966
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13968
13969/*
13970 * "inputrestore()" function
13971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013974 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976{
13977 if (ga_userinput.ga_len > 0)
13978 {
13979 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13981 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013982 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 }
13984 else if (p_verbose > 1)
13985 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013986 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 }
13989}
13990
13991/*
13992 * "inputsave()" function
13993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013996 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013999 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 if (ga_grow(&ga_userinput, 1) == OK)
14001 {
14002 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14003 + ga_userinput.ga_len);
14004 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014005 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 }
14007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014008 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009}
14010
14011/*
14012 * "inputsecret()" function
14013 */
14014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014016 typval_T *argvars;
14017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018{
14019 ++cmdline_star;
14020 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 --cmdline_star;
14023 --inputsecret_flag;
14024}
14025
14026/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014027 * "insert()" function
14028 */
14029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 typval_T *argvars;
14032 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014033{
14034 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 listitem_T *item;
14036 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014038
14039 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014041 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014042 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 {
14044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 before = get_tv_number_chk(&argvars[2], &error);
14046 if (error)
14047 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014048
Bram Moolenaar758711c2005-02-02 23:11:38 +000014049 if (before == l->lv_len)
14050 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014051 else
14052 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014053 item = list_find(l, before);
14054 if (item == NULL)
14055 {
14056 EMSGN(_(e_listidx), before);
14057 l = NULL;
14058 }
14059 }
14060 if (l != NULL)
14061 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014062 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014063 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014064 }
14065 }
14066}
14067
14068/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014069 * "invert(expr)" function
14070 */
14071 static void
14072f_invert(argvars, rettv)
14073 typval_T *argvars;
14074 typval_T *rettv;
14075{
14076 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14077}
14078
14079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 * "isdirectory()" function
14081 */
14082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014084 typval_T *argvars;
14085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088}
14089
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014090/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014091 * "islocked()" function
14092 */
14093 static void
14094f_islocked(argvars, rettv)
14095 typval_T *argvars;
14096 typval_T *rettv;
14097{
14098 lval_T lv;
14099 char_u *end;
14100 dictitem_T *di;
14101
14102 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014103 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14104 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014105 if (end != NULL && lv.ll_name != NULL)
14106 {
14107 if (*end != NUL)
14108 EMSG(_(e_trailing));
14109 else
14110 {
14111 if (lv.ll_tv == NULL)
14112 {
14113 if (check_changedtick(lv.ll_name))
14114 rettv->vval.v_number = 1; /* always locked */
14115 else
14116 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014117 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014118 if (di != NULL)
14119 {
14120 /* Consider a variable locked when:
14121 * 1. the variable itself is locked
14122 * 2. the value of the variable is locked.
14123 * 3. the List or Dict value is locked.
14124 */
14125 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14126 || tv_islocked(&di->di_tv));
14127 }
14128 }
14129 }
14130 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014131 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014132 else if (lv.ll_newkey != NULL)
14133 EMSG2(_(e_dictkey), lv.ll_newkey);
14134 else if (lv.ll_list != NULL)
14135 /* List item. */
14136 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14137 else
14138 /* Dictionary item. */
14139 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14140 }
14141 }
14142
14143 clear_lval(&lv);
14144}
14145
Bram Moolenaar33570922005-01-25 22:26:29 +000014146static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014147
14148/*
14149 * Turn a dict into a list:
14150 * "what" == 0: list of keys
14151 * "what" == 1: list of values
14152 * "what" == 2: list of items
14153 */
14154 static void
14155dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *argvars;
14157 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014158 int what;
14159{
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 list_T *l2;
14161 dictitem_T *di;
14162 hashitem_T *hi;
14163 listitem_T *li;
14164 listitem_T *li2;
14165 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014166 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014167
Bram Moolenaar8c711452005-01-14 21:53:12 +000014168 if (argvars[0].v_type != VAR_DICT)
14169 {
14170 EMSG(_(e_dictreq));
14171 return;
14172 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014173 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014174 return;
14175
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014176 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014177 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014179 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014184 --todo;
14185 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014186
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014187 li = listitem_alloc();
14188 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014189 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014190 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014191
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014192 if (what == 0)
14193 {
14194 /* keys() */
14195 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014196 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014197 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14198 }
14199 else if (what == 1)
14200 {
14201 /* values() */
14202 copy_tv(&di->di_tv, &li->li_tv);
14203 }
14204 else
14205 {
14206 /* items() */
14207 l2 = list_alloc();
14208 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014209 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014210 li->li_tv.vval.v_list = l2;
14211 if (l2 == NULL)
14212 break;
14213 ++l2->lv_refcount;
14214
14215 li2 = listitem_alloc();
14216 if (li2 == NULL)
14217 break;
14218 list_append(l2, li2);
14219 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014220 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014221 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14222
14223 li2 = listitem_alloc();
14224 if (li2 == NULL)
14225 break;
14226 list_append(l2, li2);
14227 copy_tv(&di->di_tv, &li2->li_tv);
14228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014229 }
14230 }
14231}
14232
14233/*
14234 * "items(dict)" function
14235 */
14236 static void
14237f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 typval_T *argvars;
14239 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014240{
14241 dict_list(argvars, rettv, 2);
14242}
14243
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014245 * "join()" function
14246 */
14247 static void
14248f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 typval_T *argvars;
14250 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014251{
14252 garray_T ga;
14253 char_u *sep;
14254
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014255 if (argvars[0].v_type != VAR_LIST)
14256 {
14257 EMSG(_(e_listreq));
14258 return;
14259 }
14260 if (argvars[0].vval.v_list == NULL)
14261 return;
14262 if (argvars[1].v_type == VAR_UNKNOWN)
14263 sep = (char_u *)" ";
14264 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014266
14267 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268
14269 if (sep != NULL)
14270 {
14271 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014272 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 ga_append(&ga, NUL);
14274 rettv->vval.v_string = (char_u *)ga.ga_data;
14275 }
14276 else
14277 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014278}
14279
14280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014281 * "keys()" function
14282 */
14283 static void
14284f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 typval_T *argvars;
14286 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014287{
14288 dict_list(argvars, rettv, 0);
14289}
14290
14291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 * "last_buffer_nr()" function.
14293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014296 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298{
14299 int n = 0;
14300 buf_T *buf;
14301
14302 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14303 if (n < buf->b_fnum)
14304 n = buf->b_fnum;
14305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014307}
14308
14309/*
14310 * "len()" function
14311 */
14312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 typval_T *argvars;
14315 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014316{
14317 switch (argvars[0].v_type)
14318 {
14319 case VAR_STRING:
14320 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 rettv->vval.v_number = (varnumber_T)STRLEN(
14322 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014323 break;
14324 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014327 case VAR_DICT:
14328 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14329 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014331 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014332 break;
14333 }
14334}
14335
Bram Moolenaar33570922005-01-25 22:26:29 +000014336static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014337
14338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 typval_T *argvars;
14341 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014342 int type;
14343{
14344#ifdef FEAT_LIBCALL
14345 char_u *string_in;
14346 char_u **string_result;
14347 int nr_result;
14348#endif
14349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014351 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353
14354 if (check_restricted() || check_secure())
14355 return;
14356
14357#ifdef FEAT_LIBCALL
14358 /* The first two args must be strings, otherwise its meaningless */
14359 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14360 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014361 string_in = NULL;
14362 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014363 string_in = argvars[2].vval.v_string;
14364 if (type == VAR_NUMBER)
14365 string_result = NULL;
14366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 if (mch_libcall(argvars[0].vval.v_string,
14369 argvars[1].vval.v_string,
14370 string_in,
14371 argvars[2].vval.v_number,
14372 string_result,
14373 &nr_result) == OK
14374 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014376 }
14377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378}
14379
14380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 * "libcall()" function
14382 */
14383 static void
14384f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T *argvars;
14386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014387{
14388 libcall_common(argvars, rettv, VAR_STRING);
14389}
14390
14391/*
14392 * "libcallnr()" function
14393 */
14394 static void
14395f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014396 typval_T *argvars;
14397 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014398{
14399 libcall_common(argvars, rettv, VAR_NUMBER);
14400}
14401
14402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 * "line(string)" function
14404 */
14405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
14410 linenr_T lnum = 0;
14411 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014412 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014414 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 if (fp != NULL)
14416 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418}
14419
14420/*
14421 * "line2byte(lnum)" function
14422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427{
14428#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430#else
14431 linenr_T lnum;
14432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14438 if (rettv->vval.v_number >= 0)
14439 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440#endif
14441}
14442
14443/*
14444 * "lispindent(lnum)" function
14445 */
14446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450{
14451#ifdef FEAT_LISP
14452 pos_T pos;
14453 linenr_T lnum;
14454
14455 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14458 {
14459 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 curwin->w_cursor = pos;
14462 }
14463 else
14464#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466}
14467
14468/*
14469 * "localtime()" function
14470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014473 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
Bram Moolenaar33570922005-01-25 22:26:29 +000014479static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480
14481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 typval_T *argvars;
14484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 int exact;
14486{
14487 char_u *keys;
14488 char_u *which;
14489 char_u buf[NUMBUFLEN];
14490 char_u *keys_buf = NULL;
14491 char_u *rhs;
14492 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014493 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014494 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014495 mapblock_T *mp;
14496 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497
14498 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->v_type = VAR_STRING;
14500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 if (*keys == NUL)
14504 return;
14505
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014506 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014510 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014511 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014512 if (argvars[3].v_type != VAR_UNKNOWN)
14513 get_dict = get_tv_number(&argvars[3]);
14514 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 else
14517 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014518 if (which == NULL)
14519 return;
14520
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 mode = get_map_mode(&which, 0);
14522
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014523 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014524 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014526
14527 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014529 /* Return a string. */
14530 if (rhs != NULL)
14531 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532
Bram Moolenaarbd743252010-10-20 21:23:33 +020014533 }
14534 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14535 {
14536 /* Return a dictionary. */
14537 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14538 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14539 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
Bram Moolenaarbd743252010-10-20 21:23:33 +020014541 dict_add_nr_str(dict, "lhs", 0L, lhs);
14542 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14543 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14544 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14545 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14546 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14547 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014548 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014549 dict_add_nr_str(dict, "mode", 0L, mapmode);
14550
14551 vim_free(lhs);
14552 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 }
14554}
14555
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014556#ifdef FEAT_FLOAT
14557/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014558 * "log()" function
14559 */
14560 static void
14561f_log(argvars, rettv)
14562 typval_T *argvars;
14563 typval_T *rettv;
14564{
14565 float_T f;
14566
14567 rettv->v_type = VAR_FLOAT;
14568 if (get_float_arg(argvars, &f) == OK)
14569 rettv->vval.v_float = log(f);
14570 else
14571 rettv->vval.v_float = 0.0;
14572}
14573
14574/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014575 * "log10()" function
14576 */
14577 static void
14578f_log10(argvars, rettv)
14579 typval_T *argvars;
14580 typval_T *rettv;
14581{
14582 float_T f;
14583
14584 rettv->v_type = VAR_FLOAT;
14585 if (get_float_arg(argvars, &f) == OK)
14586 rettv->vval.v_float = log10(f);
14587 else
14588 rettv->vval.v_float = 0.0;
14589}
14590#endif
14591
Bram Moolenaar1dced572012-04-05 16:54:08 +020014592#ifdef FEAT_LUA
14593/*
14594 * "luaeval()" function
14595 */
14596 static void
14597f_luaeval(argvars, rettv)
14598 typval_T *argvars;
14599 typval_T *rettv;
14600{
14601 char_u *str;
14602 char_u buf[NUMBUFLEN];
14603
14604 str = get_tv_string_buf(&argvars[0], buf);
14605 do_luaeval(str, argvars + 1, rettv);
14606}
14607#endif
14608
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014610 * "map()" function
14611 */
14612 static void
14613f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *argvars;
14615 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014616{
14617 filter_map(argvars, rettv, TRUE);
14618}
14619
14620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 */
14623 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014624f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 typval_T *argvars;
14626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629}
14630
14631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 */
14634 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *argvars;
14637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640}
14641
Bram Moolenaar33570922005-01-25 22:26:29 +000014642static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643
14644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 typval_T *argvars;
14647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 int type;
14649{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014650 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014651 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014652 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 char_u *pat;
14654 regmatch_T regmatch;
14655 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014656 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 char_u *save_cpo;
14658 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014659 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014660 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014661 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 list_T *l = NULL;
14663 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014664 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014665 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
14667 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14668 save_cpo = p_cpo;
14669 p_cpo = (char_u *)"";
14670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671 rettv->vval.v_number = -1;
14672 if (type == 3)
14673 {
14674 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014675 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014677 }
14678 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->v_type = VAR_STRING;
14681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014684 if (argvars[0].v_type == VAR_LIST)
14685 {
14686 if ((l = argvars[0].vval.v_list) == NULL)
14687 goto theend;
14688 li = l->lv_first;
14689 }
14690 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014691 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014693 len = (long)STRLEN(str);
14694 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14697 if (pat == NULL)
14698 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014699
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 int error = FALSE;
14703
14704 start = get_tv_number_chk(&argvars[2], &error);
14705 if (error)
14706 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014707 if (l != NULL)
14708 {
14709 li = list_find(l, start);
14710 if (li == NULL)
14711 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014712 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014713 }
14714 else
14715 {
14716 if (start < 0)
14717 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014718 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014719 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014720 /* When "count" argument is there ignore matches before "start",
14721 * otherwise skip part of the string. Differs when pattern is "^"
14722 * or "\<". */
14723 if (argvars[3].v_type != VAR_UNKNOWN)
14724 startcol = start;
14725 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014726 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014727 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014728 len -= start;
14729 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014730 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014731
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014732 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014733 nth = get_tv_number_chk(&argvars[3], &error);
14734 if (error)
14735 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737
14738 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14739 if (regmatch.regprog != NULL)
14740 {
14741 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014742
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014743 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014744 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014745 if (l != NULL)
14746 {
14747 if (li == NULL)
14748 {
14749 match = FALSE;
14750 break;
14751 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014753 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014754 if (str == NULL)
14755 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014756 }
14757
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014758 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014759
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014760 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014761 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014762 if (l == NULL && !match)
14763 break;
14764
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014765 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014766 if (l != NULL)
14767 {
14768 li = li->li_next;
14769 ++idx;
14770 }
14771 else
14772 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014773#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014774 startcol = (colnr_T)(regmatch.startp[0]
14775 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014776#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014777 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014778#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014779 if (startcol > (colnr_T)len
14780 || str + startcol <= regmatch.startp[0])
14781 {
14782 match = FALSE;
14783 break;
14784 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014785 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014786 }
14787
14788 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792 int i;
14793
14794 /* return list with matched string and submatches */
14795 for (i = 0; i < NSUBEXP; ++i)
14796 {
14797 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014798 {
14799 if (list_append_string(rettv->vval.v_list,
14800 (char_u *)"", 0) == FAIL)
14801 break;
14802 }
14803 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014804 regmatch.startp[i],
14805 (int)(regmatch.endp[i] - regmatch.startp[i]))
14806 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014807 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014808 }
14809 }
14810 else if (type == 2)
14811 {
14812 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014813 if (l != NULL)
14814 copy_tv(&li->li_tv, rettv);
14815 else
14816 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014818 }
14819 else if (l != NULL)
14820 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 else
14822 {
14823 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 (varnumber_T)(regmatch.startp[0] - str);
14826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014829 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 }
14831 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014832 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 }
14834
14835theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 p_cpo = save_cpo;
14838}
14839
14840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 * "match()" function
14842 */
14843 static void
14844f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 typval_T *argvars;
14846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847{
14848 find_some_match(argvars, rettv, 1);
14849}
14850
14851/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014852 * "matchadd()" function
14853 */
14854 static void
14855f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014856 typval_T *argvars UNUSED;
14857 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014858{
14859#ifdef FEAT_SEARCH_EXTRA
14860 char_u buf[NUMBUFLEN];
14861 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14862 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14863 int prio = 10; /* default priority */
14864 int id = -1;
14865 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014866 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014867
14868 rettv->vval.v_number = -1;
14869
14870 if (grp == NULL || pat == NULL)
14871 return;
14872 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014873 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014874 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014875 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014876 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014877 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014878 if (argvars[4].v_type != VAR_UNKNOWN)
14879 {
14880 if (argvars[4].v_type != VAR_DICT)
14881 {
14882 EMSG(_(e_dictreq));
14883 return;
14884 }
14885 if (dict_find(argvars[4].vval.v_dict,
14886 (char_u *)"conceal", -1) != NULL)
14887 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14888 (char_u *)"conceal", FALSE);
14889 }
14890 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014891 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014892 if (error == TRUE)
14893 return;
14894 if (id >= 1 && id <= 3)
14895 {
14896 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14897 return;
14898 }
14899
Bram Moolenaar6561d522015-07-21 15:48:27 +020014900 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14901 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014902#endif
14903}
14904
14905/*
14906 * "matchaddpos()" function
14907 */
14908 static void
14909f_matchaddpos(argvars, rettv)
14910 typval_T *argvars UNUSED;
14911 typval_T *rettv UNUSED;
14912{
14913#ifdef FEAT_SEARCH_EXTRA
14914 char_u buf[NUMBUFLEN];
14915 char_u *group;
14916 int prio = 10;
14917 int id = -1;
14918 int error = FALSE;
14919 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014920 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014921
14922 rettv->vval.v_number = -1;
14923
14924 group = get_tv_string_buf_chk(&argvars[0], buf);
14925 if (group == NULL)
14926 return;
14927
14928 if (argvars[1].v_type != VAR_LIST)
14929 {
14930 EMSG2(_(e_listarg), "matchaddpos()");
14931 return;
14932 }
14933 l = argvars[1].vval.v_list;
14934 if (l == NULL)
14935 return;
14936
14937 if (argvars[2].v_type != VAR_UNKNOWN)
14938 {
14939 prio = get_tv_number_chk(&argvars[2], &error);
14940 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014941 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014942 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014943 if (argvars[4].v_type != VAR_UNKNOWN)
14944 {
14945 if (argvars[4].v_type != VAR_DICT)
14946 {
14947 EMSG(_(e_dictreq));
14948 return;
14949 }
14950 if (dict_find(argvars[4].vval.v_dict,
14951 (char_u *)"conceal", -1) != NULL)
14952 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14953 (char_u *)"conceal", FALSE);
14954 }
14955 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014956 }
14957 if (error == TRUE)
14958 return;
14959
14960 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14961 if (id == 1 || id == 2)
14962 {
14963 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14964 return;
14965 }
14966
Bram Moolenaar6561d522015-07-21 15:48:27 +020014967 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14968 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014969#endif
14970}
14971
14972/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014973 * "matcharg()" function
14974 */
14975 static void
14976f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014977 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014978 typval_T *rettv;
14979{
14980 if (rettv_list_alloc(rettv) == OK)
14981 {
14982#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014983 int id = get_tv_number(&argvars[0]);
14984 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014985
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014986 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014987 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014988 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14989 {
14990 list_append_string(rettv->vval.v_list,
14991 syn_id2name(m->hlg_id), -1);
14992 list_append_string(rettv->vval.v_list, m->pattern, -1);
14993 }
14994 else
14995 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014996 list_append_string(rettv->vval.v_list, NULL, -1);
14997 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014998 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014999 }
15000#endif
15001 }
15002}
15003
15004/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015005 * "matchdelete()" function
15006 */
15007 static void
15008f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015009 typval_T *argvars UNUSED;
15010 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015011{
15012#ifdef FEAT_SEARCH_EXTRA
15013 rettv->vval.v_number = match_delete(curwin,
15014 (int)get_tv_number(&argvars[0]), TRUE);
15015#endif
15016}
15017
15018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 * "matchend()" function
15020 */
15021 static void
15022f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 typval_T *argvars;
15024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025{
15026 find_some_match(argvars, rettv, 0);
15027}
15028
15029/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015030 * "matchlist()" function
15031 */
15032 static void
15033f_matchlist(argvars, rettv)
15034 typval_T *argvars;
15035 typval_T *rettv;
15036{
15037 find_some_match(argvars, rettv, 3);
15038}
15039
15040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041 * "matchstr()" function
15042 */
15043 static void
15044f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *argvars;
15046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047{
15048 find_some_match(argvars, rettv, 2);
15049}
15050
Bram Moolenaar33570922005-01-25 22:26:29 +000015051static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015052
15053 static void
15054max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015055 typval_T *argvars;
15056 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015057 int domax;
15058{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015059 long n = 0;
15060 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015062
15063 if (argvars[0].v_type == VAR_LIST)
15064 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 list_T *l;
15066 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015067
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015068 l = argvars[0].vval.v_list;
15069 if (l != NULL)
15070 {
15071 li = l->lv_first;
15072 if (li != NULL)
15073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015074 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015075 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015076 {
15077 li = li->li_next;
15078 if (li == NULL)
15079 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015080 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015081 if (domax ? i > n : i < n)
15082 n = i;
15083 }
15084 }
15085 }
15086 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015087 else if (argvars[0].v_type == VAR_DICT)
15088 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015090 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015092 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015093
15094 d = argvars[0].vval.v_dict;
15095 if (d != NULL)
15096 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015097 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015100 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015102 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015104 if (first)
15105 {
15106 n = i;
15107 first = FALSE;
15108 }
15109 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015110 n = i;
15111 }
15112 }
15113 }
15114 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015115 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015116 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015117 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015118}
15119
15120/*
15121 * "max()" function
15122 */
15123 static void
15124f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 typval_T *argvars;
15126 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015127{
15128 max_min(argvars, rettv, TRUE);
15129}
15130
15131/*
15132 * "min()" function
15133 */
15134 static void
15135f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 typval_T *argvars;
15137 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015138{
15139 max_min(argvars, rettv, FALSE);
15140}
15141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015142static int mkdir_recurse __ARGS((char_u *dir, int prot));
15143
15144/*
15145 * Create the directory in which "dir" is located, and higher levels when
15146 * needed.
15147 */
15148 static int
15149mkdir_recurse(dir, prot)
15150 char_u *dir;
15151 int prot;
15152{
15153 char_u *p;
15154 char_u *updir;
15155 int r = FAIL;
15156
15157 /* Get end of directory name in "dir".
15158 * We're done when it's "/" or "c:/". */
15159 p = gettail_sep(dir);
15160 if (p <= get_past_head(dir))
15161 return OK;
15162
15163 /* If the directory exists we're done. Otherwise: create it.*/
15164 updir = vim_strnsave(dir, (int)(p - dir));
15165 if (updir == NULL)
15166 return FAIL;
15167 if (mch_isdir(updir))
15168 r = OK;
15169 else if (mkdir_recurse(updir, prot) == OK)
15170 r = vim_mkdir_emsg(updir, prot);
15171 vim_free(updir);
15172 return r;
15173}
15174
15175#ifdef vim_mkdir
15176/*
15177 * "mkdir()" function
15178 */
15179 static void
15180f_mkdir(argvars, rettv)
15181 typval_T *argvars;
15182 typval_T *rettv;
15183{
15184 char_u *dir;
15185 char_u buf[NUMBUFLEN];
15186 int prot = 0755;
15187
15188 rettv->vval.v_number = FAIL;
15189 if (check_restricted() || check_secure())
15190 return;
15191
15192 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015193 if (*dir == NUL)
15194 rettv->vval.v_number = FAIL;
15195 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015196 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015197 if (*gettail(dir) == NUL)
15198 /* remove trailing slashes */
15199 *gettail_sep(dir) = NUL;
15200
15201 if (argvars[1].v_type != VAR_UNKNOWN)
15202 {
15203 if (argvars[2].v_type != VAR_UNKNOWN)
15204 prot = get_tv_number_chk(&argvars[2], NULL);
15205 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15206 mkdir_recurse(dir, prot);
15207 }
15208 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015209 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015210}
15211#endif
15212
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 * "mode()" function
15215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015217f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 typval_T *argvars;
15219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015221 char_u buf[3];
15222
15223 buf[1] = NUL;
15224 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 if (VIsual_active)
15227 {
15228 if (VIsual_select)
15229 buf[0] = VIsual_mode + 's' - 'v';
15230 else
15231 buf[0] = VIsual_mode;
15232 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015233 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015234 || State == CONFIRM)
15235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015237 if (State == ASKMORE)
15238 buf[1] = 'm';
15239 else if (State == CONFIRM)
15240 buf[1] = '?';
15241 }
15242 else if (State == EXTERNCMD)
15243 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 else if (State & INSERT)
15245 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015246#ifdef FEAT_VREPLACE
15247 if (State & VREPLACE_FLAG)
15248 {
15249 buf[0] = 'R';
15250 buf[1] = 'v';
15251 }
15252 else
15253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 if (State & REPLACE_FLAG)
15255 buf[0] = 'R';
15256 else
15257 buf[0] = 'i';
15258 }
15259 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015262 if (exmode_active)
15263 buf[1] = 'v';
15264 }
15265 else if (exmode_active)
15266 {
15267 buf[0] = 'c';
15268 buf[1] = 'e';
15269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015273 if (finish_op)
15274 buf[1] = 'o';
15275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015277 /* Clear out the minor mode when the argument is not a non-zero number or
15278 * non-empty string. */
15279 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015280 buf[1] = NUL;
15281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_string = vim_strsave(buf);
15283 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284}
15285
Bram Moolenaar429fa852013-04-15 12:27:36 +020015286#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015287/*
15288 * "mzeval()" function
15289 */
15290 static void
15291f_mzeval(argvars, rettv)
15292 typval_T *argvars;
15293 typval_T *rettv;
15294{
15295 char_u *str;
15296 char_u buf[NUMBUFLEN];
15297
15298 str = get_tv_string_buf(&argvars[0], buf);
15299 do_mzeval(str, rettv);
15300}
Bram Moolenaar75676462013-01-30 14:55:42 +010015301
15302 void
15303mzscheme_call_vim(name, args, rettv)
15304 char_u *name;
15305 typval_T *args;
15306 typval_T *rettv;
15307{
15308 typval_T argvars[3];
15309
15310 argvars[0].v_type = VAR_STRING;
15311 argvars[0].vval.v_string = name;
15312 copy_tv(args, &argvars[1]);
15313 argvars[2].v_type = VAR_UNKNOWN;
15314 f_call(argvars, rettv);
15315 clear_tv(&argvars[1]);
15316}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015317#endif
15318
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320 * "nextnonblank()" function
15321 */
15322 static void
15323f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *argvars;
15325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326{
15327 linenr_T lnum;
15328
15329 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015331 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 {
15333 lnum = 0;
15334 break;
15335 }
15336 if (*skipwhite(ml_get(lnum)) != NUL)
15337 break;
15338 }
15339 rettv->vval.v_number = lnum;
15340}
15341
15342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 * "nr2char()" function
15344 */
15345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 typval_T *argvars;
15348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349{
15350 char_u buf[NUMBUFLEN];
15351
15352#ifdef FEAT_MBYTE
15353 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015354 {
15355 int utf8 = 0;
15356
15357 if (argvars[1].v_type != VAR_UNKNOWN)
15358 utf8 = get_tv_number_chk(&argvars[1], NULL);
15359 if (utf8)
15360 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15361 else
15362 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 else
15365#endif
15366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 buf[1] = NUL;
15369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->v_type = VAR_STRING;
15371 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015372}
15373
15374/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015375 * "or(expr, expr)" function
15376 */
15377 static void
15378f_or(argvars, rettv)
15379 typval_T *argvars;
15380 typval_T *rettv;
15381{
15382 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15383 | get_tv_number_chk(&argvars[1], NULL);
15384}
15385
15386/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015387 * "pathshorten()" function
15388 */
15389 static void
15390f_pathshorten(argvars, rettv)
15391 typval_T *argvars;
15392 typval_T *rettv;
15393{
15394 char_u *p;
15395
15396 rettv->v_type = VAR_STRING;
15397 p = get_tv_string_chk(&argvars[0]);
15398 if (p == NULL)
15399 rettv->vval.v_string = NULL;
15400 else
15401 {
15402 p = vim_strsave(p);
15403 rettv->vval.v_string = p;
15404 if (p != NULL)
15405 shorten_dir(p);
15406 }
15407}
15408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015409#ifdef FEAT_FLOAT
15410/*
15411 * "pow()" function
15412 */
15413 static void
15414f_pow(argvars, rettv)
15415 typval_T *argvars;
15416 typval_T *rettv;
15417{
15418 float_T fx, fy;
15419
15420 rettv->v_type = VAR_FLOAT;
15421 if (get_float_arg(argvars, &fx) == OK
15422 && get_float_arg(&argvars[1], &fy) == OK)
15423 rettv->vval.v_float = pow(fx, fy);
15424 else
15425 rettv->vval.v_float = 0.0;
15426}
15427#endif
15428
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015430 * "prevnonblank()" function
15431 */
15432 static void
15433f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015434 typval_T *argvars;
15435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015436{
15437 linenr_T lnum;
15438
15439 lnum = get_tv_lnum(argvars);
15440 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15441 lnum = 0;
15442 else
15443 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15444 --lnum;
15445 rettv->vval.v_number = lnum;
15446}
15447
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015448#ifdef HAVE_STDARG_H
15449/* This dummy va_list is here because:
15450 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15451 * - locally in the function results in a "used before set" warning
15452 * - using va_start() to initialize it gives "function with fixed args" error */
15453static va_list ap;
15454#endif
15455
Bram Moolenaar8c711452005-01-14 21:53:12 +000015456/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015457 * "printf()" function
15458 */
15459 static void
15460f_printf(argvars, rettv)
15461 typval_T *argvars;
15462 typval_T *rettv;
15463{
15464 rettv->v_type = VAR_STRING;
15465 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015466#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015467 {
15468 char_u buf[NUMBUFLEN];
15469 int len;
15470 char_u *s;
15471 int saved_did_emsg = did_emsg;
15472 char *fmt;
15473
15474 /* Get the required length, allocate the buffer and do it for real. */
15475 did_emsg = FALSE;
15476 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015477 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015478 if (!did_emsg)
15479 {
15480 s = alloc(len + 1);
15481 if (s != NULL)
15482 {
15483 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015484 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015485 }
15486 }
15487 did_emsg |= saved_did_emsg;
15488 }
15489#endif
15490}
15491
15492/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015493 * "pumvisible()" function
15494 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015495 static void
15496f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015497 typval_T *argvars UNUSED;
15498 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015499{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015500#ifdef FEAT_INS_EXPAND
15501 if (pum_visible())
15502 rettv->vval.v_number = 1;
15503#endif
15504}
15505
Bram Moolenaardb913952012-06-29 12:54:53 +020015506#ifdef FEAT_PYTHON3
15507/*
15508 * "py3eval()" function
15509 */
15510 static void
15511f_py3eval(argvars, rettv)
15512 typval_T *argvars;
15513 typval_T *rettv;
15514{
15515 char_u *str;
15516 char_u buf[NUMBUFLEN];
15517
15518 str = get_tv_string_buf(&argvars[0], buf);
15519 do_py3eval(str, rettv);
15520}
15521#endif
15522
15523#ifdef FEAT_PYTHON
15524/*
15525 * "pyeval()" function
15526 */
15527 static void
15528f_pyeval(argvars, rettv)
15529 typval_T *argvars;
15530 typval_T *rettv;
15531{
15532 char_u *str;
15533 char_u buf[NUMBUFLEN];
15534
15535 str = get_tv_string_buf(&argvars[0], buf);
15536 do_pyeval(str, rettv);
15537}
15538#endif
15539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015541 * "range()" function
15542 */
15543 static void
15544f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015545 typval_T *argvars;
15546 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015547{
15548 long start;
15549 long end;
15550 long stride = 1;
15551 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015552 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015554 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015555 if (argvars[1].v_type == VAR_UNKNOWN)
15556 {
15557 end = start - 1;
15558 start = 0;
15559 }
15560 else
15561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015564 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015565 }
15566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 if (error)
15568 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015569 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015570 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015571 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015572 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015573 else
15574 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015575 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015576 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015577 if (list_append_number(rettv->vval.v_list,
15578 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015580 }
15581}
15582
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015583/*
15584 * "readfile()" function
15585 */
15586 static void
15587f_readfile(argvars, rettv)
15588 typval_T *argvars;
15589 typval_T *rettv;
15590{
15591 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015592 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015593 char_u *fname;
15594 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015595 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15596 int io_size = sizeof(buf);
15597 int readlen; /* size of last fread() */
15598 char_u *prev = NULL; /* previously read bytes, if any */
15599 long prevlen = 0; /* length of data in prev */
15600 long prevsize = 0; /* size of prev buffer */
15601 long maxline = MAXLNUM;
15602 long cnt = 0;
15603 char_u *p; /* position in buf */
15604 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015605
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015606 if (argvars[1].v_type != VAR_UNKNOWN)
15607 {
15608 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15609 binary = TRUE;
15610 if (argvars[2].v_type != VAR_UNKNOWN)
15611 maxline = get_tv_number(&argvars[2]);
15612 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015614 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015615 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015616
15617 /* Always open the file in binary mode, library functions have a mind of
15618 * their own about CR-LF conversion. */
15619 fname = get_tv_string(&argvars[0]);
15620 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15621 {
15622 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15623 return;
15624 }
15625
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015626 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015627 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015628 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015629
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015630 /* This for loop processes what was read, but is also entered at end
15631 * of file so that either:
15632 * - an incomplete line gets written
15633 * - a "binary" file gets an empty line at the end if it ends in a
15634 * newline. */
15635 for (p = buf, start = buf;
15636 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15637 ++p)
15638 {
15639 if (*p == '\n' || readlen <= 0)
15640 {
15641 listitem_T *li;
15642 char_u *s = NULL;
15643 long_u len = p - start;
15644
15645 /* Finished a line. Remove CRs before NL. */
15646 if (readlen > 0 && !binary)
15647 {
15648 while (len > 0 && start[len - 1] == '\r')
15649 --len;
15650 /* removal may cross back to the "prev" string */
15651 if (len == 0)
15652 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15653 --prevlen;
15654 }
15655 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015656 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657 else
15658 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015659 /* Change "prev" buffer to be the right size. This way
15660 * the bytes are only copied once, and very long lines are
15661 * allocated only once. */
15662 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015664 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015665 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015666 prev = NULL; /* the list will own the string */
15667 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015668 }
15669 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015670 if (s == NULL)
15671 {
15672 do_outofmem_msg((long_u) prevlen + len + 1);
15673 failed = TRUE;
15674 break;
15675 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015676
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015677 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015678 {
15679 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015680 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015681 break;
15682 }
15683 li->li_tv.v_type = VAR_STRING;
15684 li->li_tv.v_lock = 0;
15685 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015687
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015688 start = p + 1; /* step over newline */
15689 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015690 break;
15691 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015692 else if (*p == NUL)
15693 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015694#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015695 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15696 * when finding the BF and check the previous two bytes. */
15697 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015698 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015699 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15700 * + 1, these may be in the "prev" string. */
15701 char_u back1 = p >= buf + 1 ? p[-1]
15702 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15703 char_u back2 = p >= buf + 2 ? p[-2]
15704 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15705 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015706
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015707 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015708 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015709 char_u *dest = p - 2;
15710
15711 /* Usually a BOM is at the beginning of a file, and so at
15712 * the beginning of a line; then we can just step over it.
15713 */
15714 if (start == dest)
15715 start = p + 1;
15716 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015718 /* have to shuffle buf to close gap */
15719 int adjust_prevlen = 0;
15720
15721 if (dest < buf)
15722 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015723 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015724 dest = buf;
15725 }
15726 if (readlen > p - buf + 1)
15727 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15728 readlen -= 3 - adjust_prevlen;
15729 prevlen -= adjust_prevlen;
15730 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015731 }
15732 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015733 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015734#endif
15735 } /* for */
15736
15737 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15738 break;
15739 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015741 /* There's part of a line in buf, store it in "prev". */
15742 if (p - start + prevlen >= prevsize)
15743 {
15744 /* need bigger "prev" buffer */
15745 char_u *newprev;
15746
15747 /* A common use case is ordinary text files and "prev" gets a
15748 * fragment of a line, so the first allocation is made
15749 * small, to avoid repeatedly 'allocing' large and
15750 * 'reallocing' small. */
15751 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015752 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015753 else
15754 {
15755 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015756 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015757 prevsize = grow50pc > growmin ? grow50pc : growmin;
15758 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015759 newprev = prev == NULL ? alloc(prevsize)
15760 : vim_realloc(prev, prevsize);
15761 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015762 {
15763 do_outofmem_msg((long_u)prevsize);
15764 failed = TRUE;
15765 break;
15766 }
15767 prev = newprev;
15768 }
15769 /* Add the line part to end of "prev". */
15770 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015771 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015772 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015773 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015774
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015775 /*
15776 * For a negative line count use only the lines at the end of the file,
15777 * free the rest.
15778 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015779 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015780 while (cnt > -maxline)
15781 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015782 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015783 --cnt;
15784 }
15785
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015786 if (failed)
15787 {
15788 list_free(rettv->vval.v_list, TRUE);
15789 /* readfile doc says an empty list is returned on error */
15790 rettv->vval.v_list = list_alloc();
15791 }
15792
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015793 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 fclose(fd);
15795}
15796
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015797#if defined(FEAT_RELTIME)
15798static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15799
15800/*
15801 * Convert a List to proftime_T.
15802 * Return FAIL when there is something wrong.
15803 */
15804 static int
15805list2proftime(arg, tm)
15806 typval_T *arg;
15807 proftime_T *tm;
15808{
15809 long n1, n2;
15810 int error = FALSE;
15811
15812 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15813 || arg->vval.v_list->lv_len != 2)
15814 return FAIL;
15815 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15816 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15817# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015818 tm->HighPart = n1;
15819 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015820# else
15821 tm->tv_sec = n1;
15822 tm->tv_usec = n2;
15823# endif
15824 return error ? FAIL : OK;
15825}
15826#endif /* FEAT_RELTIME */
15827
15828/*
15829 * "reltime()" function
15830 */
15831 static void
15832f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015833 typval_T *argvars UNUSED;
15834 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015835{
15836#ifdef FEAT_RELTIME
15837 proftime_T res;
15838 proftime_T start;
15839
15840 if (argvars[0].v_type == VAR_UNKNOWN)
15841 {
15842 /* No arguments: get current time. */
15843 profile_start(&res);
15844 }
15845 else if (argvars[1].v_type == VAR_UNKNOWN)
15846 {
15847 if (list2proftime(&argvars[0], &res) == FAIL)
15848 return;
15849 profile_end(&res);
15850 }
15851 else
15852 {
15853 /* Two arguments: compute the difference. */
15854 if (list2proftime(&argvars[0], &start) == FAIL
15855 || list2proftime(&argvars[1], &res) == FAIL)
15856 return;
15857 profile_sub(&res, &start);
15858 }
15859
15860 if (rettv_list_alloc(rettv) == OK)
15861 {
15862 long n1, n2;
15863
15864# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015865 n1 = res.HighPart;
15866 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015867# else
15868 n1 = res.tv_sec;
15869 n2 = res.tv_usec;
15870# endif
15871 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15872 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15873 }
15874#endif
15875}
15876
15877/*
15878 * "reltimestr()" function
15879 */
15880 static void
15881f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015882 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015883 typval_T *rettv;
15884{
15885#ifdef FEAT_RELTIME
15886 proftime_T tm;
15887#endif
15888
15889 rettv->v_type = VAR_STRING;
15890 rettv->vval.v_string = NULL;
15891#ifdef FEAT_RELTIME
15892 if (list2proftime(&argvars[0], &tm) == OK)
15893 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15894#endif
15895}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015896
Bram Moolenaar0d660222005-01-07 21:51:51 +000015897#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15898static void make_connection __ARGS((void));
15899static int check_connection __ARGS((void));
15900
15901 static void
15902make_connection()
15903{
15904 if (X_DISPLAY == NULL
15905# ifdef FEAT_GUI
15906 && !gui.in_use
15907# endif
15908 )
15909 {
15910 x_force_connect = TRUE;
15911 setup_term_clip();
15912 x_force_connect = FALSE;
15913 }
15914}
15915
15916 static int
15917check_connection()
15918{
15919 make_connection();
15920 if (X_DISPLAY == NULL)
15921 {
15922 EMSG(_("E240: No connection to Vim server"));
15923 return FAIL;
15924 }
15925 return OK;
15926}
15927#endif
15928
15929#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015930static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931
15932 static void
15933remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015934 typval_T *argvars;
15935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936 int expr;
15937{
15938 char_u *server_name;
15939 char_u *keys;
15940 char_u *r = NULL;
15941 char_u buf[NUMBUFLEN];
15942# ifdef WIN32
15943 HWND w;
15944# else
15945 Window w;
15946# endif
15947
15948 if (check_restricted() || check_secure())
15949 return;
15950
15951# ifdef FEAT_X11
15952 if (check_connection() == FAIL)
15953 return;
15954# endif
15955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015956 server_name = get_tv_string_chk(&argvars[0]);
15957 if (server_name == NULL)
15958 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959 keys = get_tv_string_buf(&argvars[1], buf);
15960# ifdef WIN32
15961 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15962# else
15963 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15964 < 0)
15965# endif
15966 {
15967 if (r != NULL)
15968 EMSG(r); /* sending worked but evaluation failed */
15969 else
15970 EMSG2(_("E241: Unable to send to %s"), server_name);
15971 return;
15972 }
15973
15974 rettv->vval.v_string = r;
15975
15976 if (argvars[2].v_type != VAR_UNKNOWN)
15977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015979 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015980 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015982 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015983 v.di_tv.v_type = VAR_STRING;
15984 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015985 idvar = get_tv_string_chk(&argvars[2]);
15986 if (idvar != NULL)
15987 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989 }
15990}
15991#endif
15992
15993/*
15994 * "remote_expr()" function
15995 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996 static void
15997f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016000{
16001 rettv->v_type = VAR_STRING;
16002 rettv->vval.v_string = NULL;
16003#ifdef FEAT_CLIENTSERVER
16004 remote_common(argvars, rettv, TRUE);
16005#endif
16006}
16007
16008/*
16009 * "remote_foreground()" function
16010 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011 static void
16012f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016013 typval_T *argvars UNUSED;
16014 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016015{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016016#ifdef FEAT_CLIENTSERVER
16017# ifdef WIN32
16018 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 {
16020 char_u *server_name = get_tv_string_chk(&argvars[0]);
16021
16022 if (server_name != NULL)
16023 serverForeground(server_name);
16024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025# else
16026 /* Send a foreground() expression to the server. */
16027 argvars[1].v_type = VAR_STRING;
16028 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16029 argvars[2].v_type = VAR_UNKNOWN;
16030 remote_common(argvars, rettv, TRUE);
16031 vim_free(argvars[1].vval.v_string);
16032# endif
16033#endif
16034}
16035
Bram Moolenaar0d660222005-01-07 21:51:51 +000016036 static void
16037f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040{
16041#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016042 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 char_u *s = NULL;
16044# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016045 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016048
16049 if (check_restricted() || check_secure())
16050 {
16051 rettv->vval.v_number = -1;
16052 return;
16053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016054 serverid = get_tv_string_chk(&argvars[0]);
16055 if (serverid == NULL)
16056 {
16057 rettv->vval.v_number = -1;
16058 return; /* type error; errmsg already given */
16059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016060# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016061 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062 if (n == 0)
16063 rettv->vval.v_number = -1;
16064 else
16065 {
16066 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16067 rettv->vval.v_number = (s != NULL);
16068 }
16069# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 if (check_connection() == FAIL)
16071 return;
16072
16073 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016074 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016075# endif
16076
16077 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 char_u *retvar;
16080
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 v.di_tv.v_type = VAR_STRING;
16082 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 retvar = get_tv_string_chk(&argvars[1]);
16084 if (retvar != NULL)
16085 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 }
16088#else
16089 rettv->vval.v_number = -1;
16090#endif
16091}
16092
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093 static void
16094f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016097{
16098 char_u *r = NULL;
16099
16100#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 char_u *serverid = get_tv_string_chk(&argvars[0]);
16102
16103 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 {
16105# ifdef WIN32
16106 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016107 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016108
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016109 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 if (n != 0)
16111 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16112 if (r == NULL)
16113# else
16114 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016115 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016116# endif
16117 EMSG(_("E277: Unable to read a server reply"));
16118 }
16119#endif
16120 rettv->v_type = VAR_STRING;
16121 rettv->vval.v_string = r;
16122}
16123
16124/*
16125 * "remote_send()" function
16126 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127 static void
16128f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016129 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131{
16132 rettv->v_type = VAR_STRING;
16133 rettv->vval.v_string = NULL;
16134#ifdef FEAT_CLIENTSERVER
16135 remote_common(argvars, rettv, FALSE);
16136#endif
16137}
16138
16139/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016140 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016141 */
16142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016144 typval_T *argvars;
16145 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016146{
Bram Moolenaar33570922005-01-25 22:26:29 +000016147 list_T *l;
16148 listitem_T *item, *item2;
16149 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016150 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016151 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016152 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 dict_T *d;
16154 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016155 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016156
Bram Moolenaar8c711452005-01-14 21:53:12 +000016157 if (argvars[0].v_type == VAR_DICT)
16158 {
16159 if (argvars[2].v_type != VAR_UNKNOWN)
16160 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016161 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016162 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016164 key = get_tv_string_chk(&argvars[1]);
16165 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016167 di = dict_find(d, key, -1);
16168 if (di == NULL)
16169 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016170 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16171 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 {
16173 *rettv = di->di_tv;
16174 init_tv(&di->di_tv);
16175 dictitem_remove(d, di);
16176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016178 }
16179 }
16180 else if (argvars[0].v_type != VAR_LIST)
16181 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016182 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016183 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016185 int error = FALSE;
16186
16187 idx = get_tv_number_chk(&argvars[1], &error);
16188 if (error)
16189 ; /* type error: do nothing, errmsg already given */
16190 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016191 EMSGN(_(e_listidx), idx);
16192 else
16193 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016194 if (argvars[2].v_type == VAR_UNKNOWN)
16195 {
16196 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016197 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016198 *rettv = item->li_tv;
16199 vim_free(item);
16200 }
16201 else
16202 {
16203 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016204 end = get_tv_number_chk(&argvars[2], &error);
16205 if (error)
16206 ; /* type error: do nothing */
16207 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016208 EMSGN(_(e_listidx), end);
16209 else
16210 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016211 int cnt = 0;
16212
16213 for (li = item; li != NULL; li = li->li_next)
16214 {
16215 ++cnt;
16216 if (li == item2)
16217 break;
16218 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016219 if (li == NULL) /* didn't find "item2" after "item" */
16220 EMSG(_(e_invrange));
16221 else
16222 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016223 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016224 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016225 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016226 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016227 l->lv_first = item;
16228 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016229 item->li_prev = NULL;
16230 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016231 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016232 }
16233 }
16234 }
16235 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016236 }
16237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
16240/*
16241 * "rename({from}, {to})" function
16242 */
16243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016244f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016245 typval_T *argvars;
16246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247{
16248 char_u buf[NUMBUFLEN];
16249
16250 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16254 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255}
16256
16257/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016258 * "repeat()" function
16259 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016260 static void
16261f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016262 typval_T *argvars;
16263 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016264{
16265 char_u *p;
16266 int n;
16267 int slen;
16268 int len;
16269 char_u *r;
16270 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016271
16272 n = get_tv_number(&argvars[1]);
16273 if (argvars[0].v_type == VAR_LIST)
16274 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016275 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016276 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016277 if (list_extend(rettv->vval.v_list,
16278 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016279 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016280 }
16281 else
16282 {
16283 p = get_tv_string(&argvars[0]);
16284 rettv->v_type = VAR_STRING;
16285 rettv->vval.v_string = NULL;
16286
16287 slen = (int)STRLEN(p);
16288 len = slen * n;
16289 if (len <= 0)
16290 return;
16291
16292 r = alloc(len + 1);
16293 if (r != NULL)
16294 {
16295 for (i = 0; i < n; i++)
16296 mch_memmove(r + i * slen, p, (size_t)slen);
16297 r[len] = NUL;
16298 }
16299
16300 rettv->vval.v_string = r;
16301 }
16302}
16303
16304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 * "resolve()" function
16306 */
16307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 typval_T *argvars;
16310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311{
16312 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016313#ifdef HAVE_READLINK
16314 char_u *buf = NULL;
16315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016317 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318#ifdef FEAT_SHORTCUT
16319 {
16320 char_u *v = NULL;
16321
16322 v = mch_resolve_shortcut(p);
16323 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016324 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 }
16328#else
16329# ifdef HAVE_READLINK
16330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 char_u *cpy;
16332 int len;
16333 char_u *remain = NULL;
16334 char_u *q;
16335 int is_relative_to_current = FALSE;
16336 int has_trailing_pathsep = FALSE;
16337 int limit = 100;
16338
16339 p = vim_strsave(p);
16340
16341 if (p[0] == '.' && (vim_ispathsep(p[1])
16342 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16343 is_relative_to_current = TRUE;
16344
16345 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016346 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016349 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351
16352 q = getnextcomp(p);
16353 if (*q != NUL)
16354 {
16355 /* Separate the first path component in "p", and keep the
16356 * remainder (beginning with the path separator). */
16357 remain = vim_strsave(q - 1);
16358 q[-1] = NUL;
16359 }
16360
Bram Moolenaard9462e32011-04-11 21:35:11 +020016361 buf = alloc(MAXPATHL + 1);
16362 if (buf == NULL)
16363 goto fail;
16364
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 for (;;)
16366 {
16367 for (;;)
16368 {
16369 len = readlink((char *)p, (char *)buf, MAXPATHL);
16370 if (len <= 0)
16371 break;
16372 buf[len] = NUL;
16373
16374 if (limit-- == 0)
16375 {
16376 vim_free(p);
16377 vim_free(remain);
16378 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 goto fail;
16381 }
16382
16383 /* Ensure that the result will have a trailing path separator
16384 * if the argument has one. */
16385 if (remain == NULL && has_trailing_pathsep)
16386 add_pathsep(buf);
16387
16388 /* Separate the first path component in the link value and
16389 * concatenate the remainders. */
16390 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16391 if (*q != NUL)
16392 {
16393 if (remain == NULL)
16394 remain = vim_strsave(q - 1);
16395 else
16396 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016397 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 if (cpy != NULL)
16399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 vim_free(remain);
16401 remain = cpy;
16402 }
16403 }
16404 q[-1] = NUL;
16405 }
16406
16407 q = gettail(p);
16408 if (q > p && *q == NUL)
16409 {
16410 /* Ignore trailing path separator. */
16411 q[-1] = NUL;
16412 q = gettail(p);
16413 }
16414 if (q > p && !mch_isFullName(buf))
16415 {
16416 /* symlink is relative to directory of argument */
16417 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16418 if (cpy != NULL)
16419 {
16420 STRCPY(cpy, p);
16421 STRCPY(gettail(cpy), buf);
16422 vim_free(p);
16423 p = cpy;
16424 }
16425 }
16426 else
16427 {
16428 vim_free(p);
16429 p = vim_strsave(buf);
16430 }
16431 }
16432
16433 if (remain == NULL)
16434 break;
16435
16436 /* Append the first path component of "remain" to "p". */
16437 q = getnextcomp(remain + 1);
16438 len = q - remain - (*q != NUL);
16439 cpy = vim_strnsave(p, STRLEN(p) + len);
16440 if (cpy != NULL)
16441 {
16442 STRNCAT(cpy, remain, len);
16443 vim_free(p);
16444 p = cpy;
16445 }
16446 /* Shorten "remain". */
16447 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016448 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 else
16450 {
16451 vim_free(remain);
16452 remain = NULL;
16453 }
16454 }
16455
16456 /* If the result is a relative path name, make it explicitly relative to
16457 * the current directory if and only if the argument had this form. */
16458 if (!vim_ispathsep(*p))
16459 {
16460 if (is_relative_to_current
16461 && *p != NUL
16462 && !(p[0] == '.'
16463 && (p[1] == NUL
16464 || vim_ispathsep(p[1])
16465 || (p[1] == '.'
16466 && (p[2] == NUL
16467 || vim_ispathsep(p[2]))))))
16468 {
16469 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016470 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 if (cpy != NULL)
16472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 vim_free(p);
16474 p = cpy;
16475 }
16476 }
16477 else if (!is_relative_to_current)
16478 {
16479 /* Strip leading "./". */
16480 q = p;
16481 while (q[0] == '.' && vim_ispathsep(q[1]))
16482 q += 2;
16483 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016484 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 }
16486 }
16487
16488 /* Ensure that the result will have no trailing path separator
16489 * if the argument had none. But keep "/" or "//". */
16490 if (!has_trailing_pathsep)
16491 {
16492 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016493 if (after_pathsep(p, q))
16494 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 }
16496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016497 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 }
16499# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501# endif
16502#endif
16503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505
16506#ifdef HAVE_READLINK
16507fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016508 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511}
16512
16513/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 */
16516 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 typval_T *argvars;
16519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520{
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 list_T *l;
16522 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 if (argvars[0].v_type != VAR_LIST)
16525 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016526 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016527 && !tv_check_lock(l->lv_lock,
16528 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 {
16530 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016531 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016532 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 while (li != NULL)
16534 {
16535 ni = li->li_prev;
16536 list_append(l, li);
16537 li = ni;
16538 }
16539 rettv->vval.v_list = l;
16540 rettv->v_type = VAR_LIST;
16541 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016542 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016546#define SP_NOMOVE 0x01 /* don't move cursor */
16547#define SP_REPEAT 0x02 /* repeat to find outer pair */
16548#define SP_RETCOUNT 0x04 /* return matchcount */
16549#define SP_SETPCMARK 0x08 /* set previous context mark */
16550#define SP_START 0x10 /* accept match at start position */
16551#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16552#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016553#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016554
Bram Moolenaar33570922005-01-25 22:26:29 +000016555static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556
16557/*
16558 * Get flags for a search function.
16559 * Possibly sets "p_ws".
16560 * Returns BACKWARD, FORWARD or zero (for an error).
16561 */
16562 static int
16563get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016565 int *flagsp;
16566{
16567 int dir = FORWARD;
16568 char_u *flags;
16569 char_u nbuf[NUMBUFLEN];
16570 int mask;
16571
16572 if (varp->v_type != VAR_UNKNOWN)
16573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016574 flags = get_tv_string_buf_chk(varp, nbuf);
16575 if (flags == NULL)
16576 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 while (*flags != NUL)
16578 {
16579 switch (*flags)
16580 {
16581 case 'b': dir = BACKWARD; break;
16582 case 'w': p_ws = TRUE; break;
16583 case 'W': p_ws = FALSE; break;
16584 default: mask = 0;
16585 if (flagsp != NULL)
16586 switch (*flags)
16587 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016588 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016589 case 'e': mask = SP_END; break;
16590 case 'm': mask = SP_RETCOUNT; break;
16591 case 'n': mask = SP_NOMOVE; break;
16592 case 'p': mask = SP_SUBPAT; break;
16593 case 'r': mask = SP_REPEAT; break;
16594 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016595 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016596 }
16597 if (mask == 0)
16598 {
16599 EMSG2(_(e_invarg2), flags);
16600 dir = 0;
16601 }
16602 else
16603 *flagsp |= mask;
16604 }
16605 if (dir == 0)
16606 break;
16607 ++flags;
16608 }
16609 }
16610 return dir;
16611}
16612
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016614 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016616 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016617search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016619 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016620 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016622 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 char_u *pat;
16624 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016625 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 int save_p_ws = p_ws;
16627 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016628 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016629 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016630 proftime_T tm;
16631#ifdef FEAT_RELTIME
16632 long time_limit = 0;
16633#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016634 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016635 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016638 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016639 if (dir == 0)
16640 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016641 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016642 if (flags & SP_START)
16643 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016644 if (flags & SP_END)
16645 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016646 if (flags & SP_COLUMN)
16647 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648
Bram Moolenaar76929292008-01-06 19:07:36 +000016649 /* Optional arguments: line number to stop searching and timeout. */
16650 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016651 {
16652 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16653 if (lnum_stop < 0)
16654 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016655#ifdef FEAT_RELTIME
16656 if (argvars[3].v_type != VAR_UNKNOWN)
16657 {
16658 time_limit = get_tv_number_chk(&argvars[3], NULL);
16659 if (time_limit < 0)
16660 goto theend;
16661 }
16662#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 }
16664
Bram Moolenaar76929292008-01-06 19:07:36 +000016665#ifdef FEAT_RELTIME
16666 /* Set the time limit, if there is one. */
16667 profile_setlimit(time_limit, &tm);
16668#endif
16669
Bram Moolenaar231334e2005-07-25 20:46:57 +000016670 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016671 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016672 * Check to make sure only those flags are set.
16673 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16674 * flags cannot be set. Check for that condition also.
16675 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016676 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016677 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016680 goto theend;
16681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016683 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016684 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016685 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016686 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016688 if (flags & SP_SUBPAT)
16689 retval = subpatnum;
16690 else
16691 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016692 if (flags & SP_SETPCMARK)
16693 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016695 if (match_pos != NULL)
16696 {
16697 /* Store the match cursor position */
16698 match_pos->lnum = pos.lnum;
16699 match_pos->col = pos.col + 1;
16700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 /* "/$" will put the cursor after the end of the line, may need to
16702 * correct that here */
16703 check_cursor();
16704 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016705
16706 /* If 'n' flag is used: restore cursor position. */
16707 if (flags & SP_NOMOVE)
16708 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016709 else
16710 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016711theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016713
16714 return retval;
16715}
16716
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016717#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016718
16719/*
16720 * round() is not in C90, use ceil() or floor() instead.
16721 */
16722 float_T
16723vim_round(f)
16724 float_T f;
16725{
16726 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16727}
16728
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016729/*
16730 * "round({float})" function
16731 */
16732 static void
16733f_round(argvars, rettv)
16734 typval_T *argvars;
16735 typval_T *rettv;
16736{
16737 float_T f;
16738
16739 rettv->v_type = VAR_FLOAT;
16740 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016741 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016742 else
16743 rettv->vval.v_float = 0.0;
16744}
16745#endif
16746
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016747/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016748 * "screenattr()" function
16749 */
16750 static void
16751f_screenattr(argvars, rettv)
16752 typval_T *argvars UNUSED;
16753 typval_T *rettv;
16754{
16755 int row;
16756 int col;
16757 int c;
16758
16759 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16760 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16761 if (row < 0 || row >= screen_Rows
16762 || col < 0 || col >= screen_Columns)
16763 c = -1;
16764 else
16765 c = ScreenAttrs[LineOffset[row] + col];
16766 rettv->vval.v_number = c;
16767}
16768
16769/*
16770 * "screenchar()" function
16771 */
16772 static void
16773f_screenchar(argvars, rettv)
16774 typval_T *argvars UNUSED;
16775 typval_T *rettv;
16776{
16777 int row;
16778 int col;
16779 int off;
16780 int c;
16781
16782 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16783 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16784 if (row < 0 || row >= screen_Rows
16785 || col < 0 || col >= screen_Columns)
16786 c = -1;
16787 else
16788 {
16789 off = LineOffset[row] + col;
16790#ifdef FEAT_MBYTE
16791 if (enc_utf8 && ScreenLinesUC[off] != 0)
16792 c = ScreenLinesUC[off];
16793 else
16794#endif
16795 c = ScreenLines[off];
16796 }
16797 rettv->vval.v_number = c;
16798}
16799
16800/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016801 * "screencol()" function
16802 *
16803 * First column is 1 to be consistent with virtcol().
16804 */
16805 static void
16806f_screencol(argvars, rettv)
16807 typval_T *argvars UNUSED;
16808 typval_T *rettv;
16809{
16810 rettv->vval.v_number = screen_screencol() + 1;
16811}
16812
16813/*
16814 * "screenrow()" function
16815 */
16816 static void
16817f_screenrow(argvars, rettv)
16818 typval_T *argvars UNUSED;
16819 typval_T *rettv;
16820{
16821 rettv->vval.v_number = screen_screenrow() + 1;
16822}
16823
16824/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016825 * "search()" function
16826 */
16827 static void
16828f_search(argvars, rettv)
16829 typval_T *argvars;
16830 typval_T *rettv;
16831{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016832 int flags = 0;
16833
16834 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835}
16836
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016838 * "searchdecl()" function
16839 */
16840 static void
16841f_searchdecl(argvars, rettv)
16842 typval_T *argvars;
16843 typval_T *rettv;
16844{
16845 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016846 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016847 int error = FALSE;
16848 char_u *name;
16849
16850 rettv->vval.v_number = 1; /* default: FAIL */
16851
16852 name = get_tv_string_chk(&argvars[0]);
16853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016854 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016855 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016856 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16857 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16858 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016859 if (!error && name != NULL)
16860 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016861 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016862}
16863
16864/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016865 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016867 static int
16868searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016870 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871{
16872 char_u *spat, *mpat, *epat;
16873 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 int dir;
16876 int flags = 0;
16877 char_u nbuf1[NUMBUFLEN];
16878 char_u nbuf2[NUMBUFLEN];
16879 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016880 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016881 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016882 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 spat = get_tv_string_chk(&argvars[0]);
16886 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16887 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16888 if (spat == NULL || mpat == NULL || epat == NULL)
16889 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 /* Handle the optional fourth argument: flags */
16892 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016893 if (dir == 0)
16894 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016895
16896 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016897 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16898 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016899 if ((flags & (SP_END | SP_SUBPAT)) != 0
16900 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016901 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016902 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016903 goto theend;
16904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016906 /* Using 'r' implies 'W', otherwise it doesn't work. */
16907 if (flags & SP_REPEAT)
16908 p_ws = FALSE;
16909
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016910 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016911 if (argvars[3].v_type == VAR_UNKNOWN
16912 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 skip = (char_u *)"";
16914 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016917 if (argvars[5].v_type != VAR_UNKNOWN)
16918 {
16919 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16920 if (lnum_stop < 0)
16921 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016922#ifdef FEAT_RELTIME
16923 if (argvars[6].v_type != VAR_UNKNOWN)
16924 {
16925 time_limit = get_tv_number_chk(&argvars[6], NULL);
16926 if (time_limit < 0)
16927 goto theend;
16928 }
16929#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016930 }
16931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016932 if (skip == NULL)
16933 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016935 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016936 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016937
16938theend:
16939 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016940
16941 return retval;
16942}
16943
16944/*
16945 * "searchpair()" function
16946 */
16947 static void
16948f_searchpair(argvars, rettv)
16949 typval_T *argvars;
16950 typval_T *rettv;
16951{
16952 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16953}
16954
16955/*
16956 * "searchpairpos()" function
16957 */
16958 static void
16959f_searchpairpos(argvars, rettv)
16960 typval_T *argvars;
16961 typval_T *rettv;
16962{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016963 pos_T match_pos;
16964 int lnum = 0;
16965 int col = 0;
16966
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016967 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016968 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016969
16970 if (searchpair_cmn(argvars, &match_pos) > 0)
16971 {
16972 lnum = match_pos.lnum;
16973 col = match_pos.col;
16974 }
16975
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016976 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16977 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016978}
16979
16980/*
16981 * Search for a start/middle/end thing.
16982 * Used by searchpair(), see its documentation for the details.
16983 * Returns 0 or -1 for no match,
16984 */
16985 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016986do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16987 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016988 char_u *spat; /* start pattern */
16989 char_u *mpat; /* middle pattern */
16990 char_u *epat; /* end pattern */
16991 int dir; /* BACKWARD or FORWARD */
16992 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016993 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016994 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016995 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016996 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016997{
16998 char_u *save_cpo;
16999 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17000 long retval = 0;
17001 pos_T pos;
17002 pos_T firstpos;
17003 pos_T foundpos;
17004 pos_T save_cursor;
17005 pos_T save_pos;
17006 int n;
17007 int r;
17008 int nest = 1;
17009 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017010 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017011 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017012
17013 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17014 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017015 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017016
Bram Moolenaar76929292008-01-06 19:07:36 +000017017#ifdef FEAT_RELTIME
17018 /* Set the time limit, if there is one. */
17019 profile_setlimit(time_limit, &tm);
17020#endif
17021
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017022 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17023 * start/middle/end (pat3, for the top pair). */
17024 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17025 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17026 if (pat2 == NULL || pat3 == NULL)
17027 goto theend;
17028 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17029 if (*mpat == NUL)
17030 STRCPY(pat3, pat2);
17031 else
17032 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17033 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017034 if (flags & SP_START)
17035 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 save_cursor = curwin->w_cursor;
17038 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017039 clearpos(&firstpos);
17040 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 pat = pat3;
17042 for (;;)
17043 {
17044 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017045 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17047 /* didn't find it or found the first match again: FAIL */
17048 break;
17049
17050 if (firstpos.lnum == 0)
17051 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017052 if (equalpos(pos, foundpos))
17053 {
17054 /* Found the same position again. Can happen with a pattern that
17055 * has "\zs" at the end and searching backwards. Advance one
17056 * character and try again. */
17057 if (dir == BACKWARD)
17058 decl(&pos);
17059 else
17060 incl(&pos);
17061 }
17062 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017064 /* clear the start flag to avoid getting stuck here */
17065 options &= ~SEARCH_START;
17066
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 /* If the skip pattern matches, ignore this match. */
17068 if (*skip != NUL)
17069 {
17070 save_pos = curwin->w_cursor;
17071 curwin->w_cursor = pos;
17072 r = eval_to_bool(skip, &err, NULL, FALSE);
17073 curwin->w_cursor = save_pos;
17074 if (err)
17075 {
17076 /* Evaluating {skip} caused an error, break here. */
17077 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017078 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 break;
17080 }
17081 if (r)
17082 continue;
17083 }
17084
17085 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17086 {
17087 /* Found end when searching backwards or start when searching
17088 * forward: nested pair. */
17089 ++nest;
17090 pat = pat2; /* nested, don't search for middle */
17091 }
17092 else
17093 {
17094 /* Found end when searching forward or start when searching
17095 * backward: end of (nested) pair; or found middle in outer pair. */
17096 if (--nest == 1)
17097 pat = pat3; /* outer level, search for middle */
17098 }
17099
17100 if (nest == 0)
17101 {
17102 /* Found the match: return matchcount or line number. */
17103 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017104 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017106 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017107 if (flags & SP_SETPCMARK)
17108 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 curwin->w_cursor = pos;
17110 if (!(flags & SP_REPEAT))
17111 break;
17112 nest = 1; /* search for next unmatched */
17113 }
17114 }
17115
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017116 if (match_pos != NULL)
17117 {
17118 /* Store the match cursor position */
17119 match_pos->lnum = curwin->w_cursor.lnum;
17120 match_pos->col = curwin->w_cursor.col + 1;
17121 }
17122
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017124 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 curwin->w_cursor = save_cursor;
17126
17127theend:
17128 vim_free(pat2);
17129 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017130 if (p_cpo == empty_option)
17131 p_cpo = save_cpo;
17132 else
17133 /* Darn, evaluating the {skip} expression changed the value. */
17134 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017135
17136 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017139/*
17140 * "searchpos()" function
17141 */
17142 static void
17143f_searchpos(argvars, rettv)
17144 typval_T *argvars;
17145 typval_T *rettv;
17146{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017147 pos_T match_pos;
17148 int lnum = 0;
17149 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017150 int n;
17151 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017153 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017154 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017155
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017156 n = search_cmn(argvars, &match_pos, &flags);
17157 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017158 {
17159 lnum = match_pos.lnum;
17160 col = match_pos.col;
17161 }
17162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17164 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017165 if (flags & SP_SUBPAT)
17166 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017167}
17168
17169
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170 static void
17171f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017172 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175#ifdef FEAT_CLIENTSERVER
17176 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 char_u *server = get_tv_string_chk(&argvars[0]);
17178 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017181 if (server == NULL || reply == NULL)
17182 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183 if (check_restricted() || check_secure())
17184 return;
17185# ifdef FEAT_X11
17186 if (check_connection() == FAIL)
17187 return;
17188# endif
17189
17190 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017192 EMSG(_("E258: Unable to send to client"));
17193 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 rettv->vval.v_number = 0;
17196#else
17197 rettv->vval.v_number = -1;
17198#endif
17199}
17200
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 static void
17202f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017204 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205{
17206 char_u *r = NULL;
17207
17208#ifdef FEAT_CLIENTSERVER
17209# ifdef WIN32
17210 r = serverGetVimNames();
17211# else
17212 make_connection();
17213 if (X_DISPLAY != NULL)
17214 r = serverGetVimNames(X_DISPLAY);
17215# endif
17216#endif
17217 rettv->v_type = VAR_STRING;
17218 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219}
17220
17221/*
17222 * "setbufvar()" function
17223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017225f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017227 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228{
17229 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017232 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 char_u nbuf[NUMBUFLEN];
17234
17235 if (check_restricted() || check_secure())
17236 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017237 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17238 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017239 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 varp = &argvars[2];
17241
17242 if (buf != NULL && varname != NULL && varp != NULL)
17243 {
17244 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
17247 if (*varname == '&')
17248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017249 long numval;
17250 char_u *strval;
17251 int error = FALSE;
17252
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017254 numval = get_tv_number_chk(varp, &error);
17255 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 if (!error && strval != NULL)
17257 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 }
17259 else
17260 {
17261 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17262 if (bufvarname != NULL)
17263 {
17264 STRCPY(bufvarname, "b:");
17265 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017266 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 vim_free(bufvarname);
17268 }
17269 }
17270
17271 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274}
17275
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017276 static void
17277f_setcharsearch(argvars, rettv)
17278 typval_T *argvars;
17279 typval_T *rettv UNUSED;
17280{
17281 dict_T *d;
17282 dictitem_T *di;
17283 char_u *csearch;
17284
17285 if (argvars[0].v_type != VAR_DICT)
17286 {
17287 EMSG(_(e_dictreq));
17288 return;
17289 }
17290
17291 if ((d = argvars[0].vval.v_dict) != NULL)
17292 {
17293 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17294 if (csearch != NULL)
17295 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017296#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017297 if (enc_utf8)
17298 {
17299 int pcc[MAX_MCO];
17300 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017301
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017302 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17303 }
17304 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017305#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017306 set_last_csearch(PTR2CHAR(csearch),
17307 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017308 }
17309
17310 di = dict_find(d, (char_u *)"forward", -1);
17311 if (di != NULL)
17312 set_csearch_direction(get_tv_number(&di->di_tv)
17313 ? FORWARD : BACKWARD);
17314
17315 di = dict_find(d, (char_u *)"until", -1);
17316 if (di != NULL)
17317 set_csearch_until(!!get_tv_number(&di->di_tv));
17318 }
17319}
17320
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321/*
17322 * "setcmdpos()" function
17323 */
17324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017326 typval_T *argvars;
17327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017329 int pos = (int)get_tv_number(&argvars[0]) - 1;
17330
17331 if (pos >= 0)
17332 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333}
17334
17335/*
17336 * "setline()" function
17337 */
17338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017340 typval_T *argvars;
17341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342{
17343 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017344 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017345 list_T *l = NULL;
17346 listitem_T *li = NULL;
17347 long added = 0;
17348 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017350 lnum = get_tv_lnum(&argvars[0]);
17351 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017353 l = argvars[1].vval.v_list;
17354 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017356 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017358
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017359 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017360 for (;;)
17361 {
17362 if (l != NULL)
17363 {
17364 /* list argument, get next string */
17365 if (li == NULL)
17366 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017368 li = li->li_next;
17369 }
17370
17371 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017372 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017373 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017374
17375 /* When coming here from Insert mode, sync undo, so that this can be
17376 * undone separately from what was previously inserted. */
17377 if (u_sync_once == 2)
17378 {
17379 u_sync_once = 1; /* notify that u_sync() was called */
17380 u_sync(TRUE);
17381 }
17382
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017383 if (lnum <= curbuf->b_ml.ml_line_count)
17384 {
17385 /* existing line, replace it */
17386 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17387 {
17388 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017389 if (lnum == curwin->w_cursor.lnum)
17390 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017391 rettv->vval.v_number = 0; /* OK */
17392 }
17393 }
17394 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17395 {
17396 /* lnum is one past the last line, append the line */
17397 ++added;
17398 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17399 rettv->vval.v_number = 0; /* OK */
17400 }
17401
17402 if (l == NULL) /* only one string argument */
17403 break;
17404 ++lnum;
17405 }
17406
17407 if (added > 0)
17408 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017411static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17412
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017414 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017415 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017416 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017417set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017418 win_T *wp UNUSED;
17419 typval_T *list_arg UNUSED;
17420 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017421 typval_T *rettv;
17422{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017423#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017424 char_u *act;
17425 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017426#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017427
Bram Moolenaar2641f772005-03-25 21:58:17 +000017428 rettv->vval.v_number = -1;
17429
17430#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017431 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017432 EMSG(_(e_listreq));
17433 else
17434 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017435 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017436
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017437 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017438 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017439 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017440 if (act == NULL)
17441 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017442 if (*act == 'a' || *act == 'r')
17443 action = *act;
17444 }
17445
Bram Moolenaar81484f42012-12-05 15:16:47 +010017446 if (l != NULL && set_errorlist(wp, l, action,
17447 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017448 rettv->vval.v_number = 0;
17449 }
17450#endif
17451}
17452
17453/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017454 * "setloclist()" function
17455 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017456 static void
17457f_setloclist(argvars, rettv)
17458 typval_T *argvars;
17459 typval_T *rettv;
17460{
17461 win_T *win;
17462
17463 rettv->vval.v_number = -1;
17464
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017465 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017466 if (win != NULL)
17467 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17468}
17469
17470/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017471 * "setmatches()" function
17472 */
17473 static void
17474f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017475 typval_T *argvars UNUSED;
17476 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017477{
17478#ifdef FEAT_SEARCH_EXTRA
17479 list_T *l;
17480 listitem_T *li;
17481 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017482 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017483
17484 rettv->vval.v_number = -1;
17485 if (argvars[0].v_type != VAR_LIST)
17486 {
17487 EMSG(_(e_listreq));
17488 return;
17489 }
17490 if ((l = argvars[0].vval.v_list) != NULL)
17491 {
17492
17493 /* To some extent make sure that we are dealing with a list from
17494 * "getmatches()". */
17495 li = l->lv_first;
17496 while (li != NULL)
17497 {
17498 if (li->li_tv.v_type != VAR_DICT
17499 || (d = li->li_tv.vval.v_dict) == NULL)
17500 {
17501 EMSG(_(e_invarg));
17502 return;
17503 }
17504 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017505 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17506 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017507 && dict_find(d, (char_u *)"priority", -1) != NULL
17508 && dict_find(d, (char_u *)"id", -1) != NULL))
17509 {
17510 EMSG(_(e_invarg));
17511 return;
17512 }
17513 li = li->li_next;
17514 }
17515
17516 clear_matches(curwin);
17517 li = l->lv_first;
17518 while (li != NULL)
17519 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017520 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017521 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017522 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017523 char_u *group;
17524 int priority;
17525 int id;
17526 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017527
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017528 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017529 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17530 {
17531 if (s == NULL)
17532 {
17533 s = list_alloc();
17534 if (s == NULL)
17535 return;
17536 }
17537
17538 /* match from matchaddpos() */
17539 for (i = 1; i < 9; i++)
17540 {
17541 sprintf((char *)buf, (char *)"pos%d", i);
17542 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17543 {
17544 if (di->di_tv.v_type != VAR_LIST)
17545 return;
17546
17547 list_append_tv(s, &di->di_tv);
17548 s->lv_refcount++;
17549 }
17550 else
17551 break;
17552 }
17553 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017554
17555 group = get_dict_string(d, (char_u *)"group", FALSE);
17556 priority = (int)get_dict_number(d, (char_u *)"priority");
17557 id = (int)get_dict_number(d, (char_u *)"id");
17558 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17559 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17560 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017561 if (i == 0)
17562 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017563 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017564 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017565 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017566 }
17567 else
17568 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017569 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017570 list_unref(s);
17571 s = NULL;
17572 }
17573
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017574 li = li->li_next;
17575 }
17576 rettv->vval.v_number = 0;
17577 }
17578#endif
17579}
17580
17581/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017582 * "setpos()" function
17583 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017584 static void
17585f_setpos(argvars, rettv)
17586 typval_T *argvars;
17587 typval_T *rettv;
17588{
17589 pos_T pos;
17590 int fnum;
17591 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017592 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017593
Bram Moolenaar08250432008-02-13 11:42:46 +000017594 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017595 name = get_tv_string_chk(argvars);
17596 if (name != NULL)
17597 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017598 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017599 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017600 if (--pos.col < 0)
17601 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017602 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017603 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017604 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017605 if (fnum == curbuf->b_fnum)
17606 {
17607 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017608 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017609 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017610 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017611 curwin->w_set_curswant = FALSE;
17612 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017613 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017614 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017615 }
17616 else
17617 EMSG(_(e_invarg));
17618 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017619 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17620 {
17621 /* set mark */
17622 if (setmark_pos(name[1], &pos, fnum) == OK)
17623 rettv->vval.v_number = 0;
17624 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017625 else
17626 EMSG(_(e_invarg));
17627 }
17628 }
17629}
17630
17631/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017632 * "setqflist()" function
17633 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017634 static void
17635f_setqflist(argvars, rettv)
17636 typval_T *argvars;
17637 typval_T *rettv;
17638{
17639 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17640}
17641
17642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 * "setreg()" function
17644 */
17645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017646f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017647 typval_T *argvars;
17648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649{
17650 int regname;
17651 char_u *strregname;
17652 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017653 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 int append;
17655 char_u yank_type;
17656 long block_len;
17657
17658 block_len = -1;
17659 yank_type = MAUTO;
17660 append = FALSE;
17661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 if (strregname == NULL)
17666 return; /* type error; errmsg already given */
17667 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 if (regname == 0 || regname == '@')
17669 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017673 stropt = get_tv_string_chk(&argvars[2]);
17674 if (stropt == NULL)
17675 return; /* type error */
17676 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 switch (*stropt)
17678 {
17679 case 'a': case 'A': /* append */
17680 append = TRUE;
17681 break;
17682 case 'v': case 'c': /* character-wise selection */
17683 yank_type = MCHAR;
17684 break;
17685 case 'V': case 'l': /* line-wise selection */
17686 yank_type = MLINE;
17687 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 case 'b': case Ctrl_V: /* block-wise selection */
17689 yank_type = MBLOCK;
17690 if (VIM_ISDIGIT(stropt[1]))
17691 {
17692 ++stropt;
17693 block_len = getdigits(&stropt) - 1;
17694 --stropt;
17695 }
17696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 }
17698 }
17699
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017700 if (argvars[1].v_type == VAR_LIST)
17701 {
17702 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017703 char_u **allocval;
17704 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017705 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017706 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017707 int len = argvars[1].vval.v_list->lv_len;
17708 listitem_T *li;
17709
Bram Moolenaar7d647822014-04-05 21:28:56 +020017710 /* First half: use for pointers to result lines; second half: use for
17711 * pointers to allocated copies. */
17712 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017713 if (lstval == NULL)
17714 return;
17715 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017716 allocval = lstval + len + 2;
17717 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017718
17719 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17720 li = li->li_next)
17721 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017722 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017723 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017724 goto free_lstval;
17725 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017726 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017727 /* Need to make a copy, next get_tv_string_buf_chk() will
17728 * overwrite the string. */
17729 strval = vim_strsave(buf);
17730 if (strval == NULL)
17731 goto free_lstval;
17732 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017733 }
17734 *curval++ = strval;
17735 }
17736 *curval++ = NULL;
17737
17738 write_reg_contents_lst(regname, lstval, -1,
17739 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017740free_lstval:
17741 while (curallocval > allocval)
17742 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017743 vim_free(lstval);
17744 }
17745 else
17746 {
17747 strval = get_tv_string_chk(&argvars[1]);
17748 if (strval == NULL)
17749 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017750 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754}
17755
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017756/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017757 * "settabvar()" function
17758 */
17759 static void
17760f_settabvar(argvars, rettv)
17761 typval_T *argvars;
17762 typval_T *rettv;
17763{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017764#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017765 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017766 tabpage_T *tp;
17767#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017768 char_u *varname, *tabvarname;
17769 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017770
17771 rettv->vval.v_number = 0;
17772
17773 if (check_restricted() || check_secure())
17774 return;
17775
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017776#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017777 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017778#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017779 varname = get_tv_string_chk(&argvars[1]);
17780 varp = &argvars[2];
17781
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017782 if (varname != NULL && varp != NULL
17783#ifdef FEAT_WINDOWS
17784 && tp != NULL
17785#endif
17786 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017787 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017788#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017789 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017790 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017791#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017792
17793 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17794 if (tabvarname != NULL)
17795 {
17796 STRCPY(tabvarname, "t:");
17797 STRCPY(tabvarname + 2, varname);
17798 set_var(tabvarname, varp, TRUE);
17799 vim_free(tabvarname);
17800 }
17801
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017802#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017803 /* Restore current tabpage */
17804 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017805 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017806#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017807 }
17808}
17809
17810/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017811 * "settabwinvar()" function
17812 */
17813 static void
17814f_settabwinvar(argvars, rettv)
17815 typval_T *argvars;
17816 typval_T *rettv;
17817{
17818 setwinvar(argvars, rettv, 1);
17819}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820
17821/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017822 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017825f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017826 typval_T *argvars;
17827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017829 setwinvar(argvars, rettv, 0);
17830}
17831
17832/*
17833 * "setwinvar()" and "settabwinvar()" functions
17834 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017835
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017836 static void
17837setwinvar(argvars, rettv, off)
17838 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017839 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017840 int off;
17841{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 win_T *win;
17843#ifdef FEAT_WINDOWS
17844 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017845 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017846 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847#endif
17848 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017849 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017851 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852
17853 if (check_restricted() || check_secure())
17854 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017855
17856#ifdef FEAT_WINDOWS
17857 if (off == 1)
17858 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17859 else
17860 tp = curtab;
17861#endif
17862 win = find_win_by_nr(&argvars[off], tp);
17863 varname = get_tv_string_chk(&argvars[off + 1]);
17864 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865
17866 if (win != NULL && varname != NULL && varp != NULL)
17867 {
17868#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017869 need_switch_win = !(tp == curtab && win == curwin);
17870 if (!need_switch_win
17871 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017874 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017876 long numval;
17877 char_u *strval;
17878 int error = FALSE;
17879
17880 ++varname;
17881 numval = get_tv_number_chk(varp, &error);
17882 strval = get_tv_string_buf_chk(varp, nbuf);
17883 if (!error && strval != NULL)
17884 set_option_value(varname, numval, strval, OPT_LOCAL);
17885 }
17886 else
17887 {
17888 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17889 if (winvarname != NULL)
17890 {
17891 STRCPY(winvarname, "w:");
17892 STRCPY(winvarname + 2, varname);
17893 set_var(winvarname, varp, TRUE);
17894 vim_free(winvarname);
17895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 }
17897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017899 if (need_switch_win)
17900 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901#endif
17902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903}
17904
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017905#ifdef FEAT_CRYPT
17906/*
17907 * "sha256({string})" function
17908 */
17909 static void
17910f_sha256(argvars, rettv)
17911 typval_T *argvars;
17912 typval_T *rettv;
17913{
17914 char_u *p;
17915
17916 p = get_tv_string(&argvars[0]);
17917 rettv->vval.v_string = vim_strsave(
17918 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17919 rettv->v_type = VAR_STRING;
17920}
17921#endif /* FEAT_CRYPT */
17922
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017924 * "shellescape({string})" function
17925 */
17926 static void
17927f_shellescape(argvars, rettv)
17928 typval_T *argvars;
17929 typval_T *rettv;
17930{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017931 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017932 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017933 rettv->v_type = VAR_STRING;
17934}
17935
17936/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017937 * shiftwidth() function
17938 */
17939 static void
17940f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017941 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017942 typval_T *rettv;
17943{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017944 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017945}
17946
17947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 */
17950 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017952 typval_T *argvars;
17953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017955 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956
Bram Moolenaar0d660222005-01-07 21:51:51 +000017957 p = get_tv_string(&argvars[0]);
17958 rettv->vval.v_string = vim_strsave(p);
17959 simplify_filename(rettv->vval.v_string); /* simplify in place */
17960 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017963#ifdef FEAT_FLOAT
17964/*
17965 * "sin()" function
17966 */
17967 static void
17968f_sin(argvars, rettv)
17969 typval_T *argvars;
17970 typval_T *rettv;
17971{
17972 float_T f;
17973
17974 rettv->v_type = VAR_FLOAT;
17975 if (get_float_arg(argvars, &f) == OK)
17976 rettv->vval.v_float = sin(f);
17977 else
17978 rettv->vval.v_float = 0.0;
17979}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017980
17981/*
17982 * "sinh()" function
17983 */
17984 static void
17985f_sinh(argvars, rettv)
17986 typval_T *argvars;
17987 typval_T *rettv;
17988{
17989 float_T f;
17990
17991 rettv->v_type = VAR_FLOAT;
17992 if (get_float_arg(argvars, &f) == OK)
17993 rettv->vval.v_float = sinh(f);
17994 else
17995 rettv->vval.v_float = 0.0;
17996}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017997#endif
17998
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999static int
18000#ifdef __BORLANDC__
18001 _RTLENTRYF
18002#endif
18003 item_compare __ARGS((const void *s1, const void *s2));
18004static int
18005#ifdef __BORLANDC__
18006 _RTLENTRYF
18007#endif
18008 item_compare2 __ARGS((const void *s1, const void *s2));
18009
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018010/* struct used in the array that's given to qsort() */
18011typedef struct
18012{
18013 listitem_T *item;
18014 int idx;
18015} sortItem_T;
18016
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018018static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018019static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018021static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018023static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018024static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025#define ITEM_COMPARE_FAIL 999
18026
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018028 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018030 static int
18031#ifdef __BORLANDC__
18032_RTLENTRYF
18033#endif
18034item_compare(s1, s2)
18035 const void *s1;
18036 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018038 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018039 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018041 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 int res;
18043 char_u numbuf1[NUMBUFLEN];
18044 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018046 si1 = (sortItem_T *)s1;
18047 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018048 tv1 = &si1->item->li_tv;
18049 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018050
18051 if (item_compare_numbers)
18052 {
18053 long v1 = get_tv_number(tv1);
18054 long v2 = get_tv_number(tv2);
18055
18056 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18057 }
18058
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018059 /* tv2string() puts quotes around a string and allocates memory. Don't do
18060 * that for string variables. Use a single quote when comparing with a
18061 * non-string to do what the docs promise. */
18062 if (tv1->v_type == VAR_STRING)
18063 {
18064 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18065 p1 = (char_u *)"'";
18066 else
18067 p1 = tv1->vval.v_string;
18068 }
18069 else
18070 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18071 if (tv2->v_type == VAR_STRING)
18072 {
18073 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18074 p2 = (char_u *)"'";
18075 else
18076 p2 = tv2->vval.v_string;
18077 }
18078 else
18079 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018080 if (p1 == NULL)
18081 p1 = (char_u *)"";
18082 if (p2 == NULL)
18083 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018084 if (!item_compare_numeric)
18085 {
18086 if (item_compare_ic)
18087 res = STRICMP(p1, p2);
18088 else
18089 res = STRCMP(p1, p2);
18090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018092 {
18093 double n1, n2;
18094 n1 = strtod((char *)p1, (char **)&p1);
18095 n2 = strtod((char *)p2, (char **)&p2);
18096 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18097 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018098
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018099 /* When the result would be zero, compare the item indexes. Makes the
18100 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018101 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018102 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018103
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 vim_free(tofree1);
18105 vim_free(tofree2);
18106 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107}
18108
18109 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110#ifdef __BORLANDC__
18111_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113item_compare2(s1, s2)
18114 const void *s1;
18115 const void *s2;
18116{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018117 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018118 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018119 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018120 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018121 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018123 /* shortcut after failure in previous call; compare all items equal */
18124 if (item_compare_func_err)
18125 return 0;
18126
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018127 si1 = (sortItem_T *)s1;
18128 si2 = (sortItem_T *)s2;
18129
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018130 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018131 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018132 copy_tv(&si1->item->li_tv, &argv[0]);
18133 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018134
18135 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018136 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018137 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18138 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018139 clear_tv(&argv[0]);
18140 clear_tv(&argv[1]);
18141
18142 if (res == FAIL)
18143 res = ITEM_COMPARE_FAIL;
18144 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018145 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18146 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018147 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018149
18150 /* When the result would be zero, compare the pointers themselves. Makes
18151 * the sort stable. */
18152 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018153 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018154
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 return res;
18156}
18157
18158/*
18159 * "sort({list})" function
18160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018162do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 typval_T *argvars;
18164 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018165 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166{
Bram Moolenaar33570922005-01-25 22:26:29 +000018167 list_T *l;
18168 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018169 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 long len;
18171 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018174 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 else
18176 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018178 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018179 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18180 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 return;
18182 rettv->vval.v_list = l;
18183 rettv->v_type = VAR_LIST;
18184 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 len = list_len(l);
18187 if (len <= 1)
18188 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189
Bram Moolenaar0d660222005-01-07 21:51:51 +000018190 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018191 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018192 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018193 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018194 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 if (argvars[1].v_type != VAR_UNKNOWN)
18196 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018197 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018198 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018199 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200 else
18201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018202 int error = FALSE;
18203
18204 i = get_tv_number_chk(&argvars[1], &error);
18205 if (error)
18206 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018207 if (i == 1)
18208 item_compare_ic = TRUE;
18209 else
18210 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018211 if (item_compare_func != NULL)
18212 {
18213 if (STRCMP(item_compare_func, "n") == 0)
18214 {
18215 item_compare_func = NULL;
18216 item_compare_numeric = TRUE;
18217 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018218 else if (STRCMP(item_compare_func, "N") == 0)
18219 {
18220 item_compare_func = NULL;
18221 item_compare_numbers = TRUE;
18222 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018223 else if (STRCMP(item_compare_func, "i") == 0)
18224 {
18225 item_compare_func = NULL;
18226 item_compare_ic = TRUE;
18227 }
18228 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018230
18231 if (argvars[2].v_type != VAR_UNKNOWN)
18232 {
18233 /* optional third argument: {dict} */
18234 if (argvars[2].v_type != VAR_DICT)
18235 {
18236 EMSG(_(e_dictreq));
18237 return;
18238 }
18239 item_compare_selfdict = argvars[2].vval.v_dict;
18240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018244 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 if (ptrs == NULL)
18246 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247
Bram Moolenaar327aa022014-03-25 18:24:23 +010018248 i = 0;
18249 if (sort)
18250 {
18251 /* sort(): ptrs will be the list to sort */
18252 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018253 {
18254 ptrs[i].item = li;
18255 ptrs[i].idx = i;
18256 ++i;
18257 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018258
18259 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018260 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018261 /* test the compare function */
18262 if (item_compare_func != NULL
18263 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018265 EMSG(_("E702: Sort compare function failed"));
18266 else
18267 {
18268 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018269 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018270 item_compare_func == NULL ? item_compare : item_compare2);
18271
18272 if (!item_compare_func_err)
18273 {
18274 /* Clear the List and append the items in sorted order. */
18275 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18276 l->lv_len = 0;
18277 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018278 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018279 }
18280 }
18281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018284 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18285
18286 /* f_uniq(): ptrs will be a stack of items to remove */
18287 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018288 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018289 item_compare_func_ptr = item_compare_func
18290 ? item_compare2 : item_compare;
18291
18292 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18293 li = li->li_next)
18294 {
18295 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18296 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018297 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018298 if (item_compare_func_err)
18299 {
18300 EMSG(_("E882: Uniq compare function failed"));
18301 break;
18302 }
18303 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 if (!item_compare_func_err)
18306 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018307 while (--i >= 0)
18308 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018309 li = ptrs[i].item->li_next;
18310 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018311 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018312 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018313 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018314 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018315 list_fix_watch(l, li);
18316 listitem_free(li);
18317 l->lv_len--;
18318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018319 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018320 }
18321
18322 vim_free(ptrs);
18323 }
18324}
18325
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018326/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018327 * "sort({list})" function
18328 */
18329 static void
18330f_sort(argvars, rettv)
18331 typval_T *argvars;
18332 typval_T *rettv;
18333{
18334 do_sort_uniq(argvars, rettv, TRUE);
18335}
18336
18337/*
18338 * "uniq({list})" function
18339 */
18340 static void
18341f_uniq(argvars, rettv)
18342 typval_T *argvars;
18343 typval_T *rettv;
18344{
18345 do_sort_uniq(argvars, rettv, FALSE);
18346}
18347
18348/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018349 * "soundfold({word})" function
18350 */
18351 static void
18352f_soundfold(argvars, rettv)
18353 typval_T *argvars;
18354 typval_T *rettv;
18355{
18356 char_u *s;
18357
18358 rettv->v_type = VAR_STRING;
18359 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018360#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018361 rettv->vval.v_string = eval_soundfold(s);
18362#else
18363 rettv->vval.v_string = vim_strsave(s);
18364#endif
18365}
18366
18367/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018368 * "spellbadword()" function
18369 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018370 static void
18371f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018372 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018373 typval_T *rettv;
18374{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018375 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018376 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018377 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018378
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018380 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018381
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018382#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018383 if (argvars[0].v_type == VAR_UNKNOWN)
18384 {
18385 /* Find the start and length of the badly spelled word. */
18386 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18387 if (len != 0)
18388 word = ml_get_cursor();
18389 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018390 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018391 {
18392 char_u *str = get_tv_string_chk(&argvars[0]);
18393 int capcol = -1;
18394
18395 if (str != NULL)
18396 {
18397 /* Check the argument for spelling. */
18398 while (*str != NUL)
18399 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018400 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018401 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018402 {
18403 word = str;
18404 break;
18405 }
18406 str += len;
18407 }
18408 }
18409 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018410#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018412 list_append_string(rettv->vval.v_list, word, len);
18413 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018414 attr == HLF_SPB ? "bad" :
18415 attr == HLF_SPR ? "rare" :
18416 attr == HLF_SPL ? "local" :
18417 attr == HLF_SPC ? "caps" :
18418 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018419}
18420
18421/*
18422 * "spellsuggest()" function
18423 */
18424 static void
18425f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018426 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018427 typval_T *rettv;
18428{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018429#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018430 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018431 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018432 int maxcount;
18433 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018434 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018435 listitem_T *li;
18436 int need_capital = FALSE;
18437#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018438
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018439 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018440 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018441
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018442#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018443 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018444 {
18445 str = get_tv_string(&argvars[0]);
18446 if (argvars[1].v_type != VAR_UNKNOWN)
18447 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018448 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018449 if (maxcount <= 0)
18450 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018451 if (argvars[2].v_type != VAR_UNKNOWN)
18452 {
18453 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18454 if (typeerr)
18455 return;
18456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018457 }
18458 else
18459 maxcount = 25;
18460
Bram Moolenaar4770d092006-01-12 23:22:24 +000018461 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018462
18463 for (i = 0; i < ga.ga_len; ++i)
18464 {
18465 str = ((char_u **)ga.ga_data)[i];
18466
18467 li = listitem_alloc();
18468 if (li == NULL)
18469 vim_free(str);
18470 else
18471 {
18472 li->li_tv.v_type = VAR_STRING;
18473 li->li_tv.v_lock = 0;
18474 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018475 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018476 }
18477 }
18478 ga_clear(&ga);
18479 }
18480#endif
18481}
18482
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018484f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018485 typval_T *argvars;
18486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487{
18488 char_u *str;
18489 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018490 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018491 regmatch_T regmatch;
18492 char_u patbuf[NUMBUFLEN];
18493 char_u *save_cpo;
18494 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018495 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018496 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018497 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018498
18499 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18500 save_cpo = p_cpo;
18501 p_cpo = (char_u *)"";
18502
18503 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018504 if (argvars[1].v_type != VAR_UNKNOWN)
18505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18507 if (pat == NULL)
18508 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018510 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018511 }
18512 if (pat == NULL || *pat == NUL)
18513 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018514
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018515 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 if (typeerr)
18518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519
Bram Moolenaar0d660222005-01-07 21:51:51 +000018520 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18521 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018524 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018526 if (*str == NUL)
18527 match = FALSE; /* empty item at the end */
18528 else
18529 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018530 if (match)
18531 end = regmatch.startp[0];
18532 else
18533 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018534 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18535 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018537 if (list_append_string(rettv->vval.v_list, str,
18538 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540 }
18541 if (!match)
18542 break;
18543 /* Advance to just after the match. */
18544 if (regmatch.endp[0] > str)
18545 col = 0;
18546 else
18547 {
18548 /* Don't get stuck at the same match. */
18549#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018550 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018551#else
18552 col = 1;
18553#endif
18554 }
18555 str = regmatch.endp[0];
18556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557
Bram Moolenaar473de612013-06-08 18:19:48 +020018558 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562}
18563
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018564#ifdef FEAT_FLOAT
18565/*
18566 * "sqrt()" function
18567 */
18568 static void
18569f_sqrt(argvars, rettv)
18570 typval_T *argvars;
18571 typval_T *rettv;
18572{
18573 float_T f;
18574
18575 rettv->v_type = VAR_FLOAT;
18576 if (get_float_arg(argvars, &f) == OK)
18577 rettv->vval.v_float = sqrt(f);
18578 else
18579 rettv->vval.v_float = 0.0;
18580}
18581
18582/*
18583 * "str2float()" function
18584 */
18585 static void
18586f_str2float(argvars, rettv)
18587 typval_T *argvars;
18588 typval_T *rettv;
18589{
18590 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18591
18592 if (*p == '+')
18593 p = skipwhite(p + 1);
18594 (void)string2float(p, &rettv->vval.v_float);
18595 rettv->v_type = VAR_FLOAT;
18596}
18597#endif
18598
Bram Moolenaar2c932302006-03-18 21:42:09 +000018599/*
18600 * "str2nr()" function
18601 */
18602 static void
18603f_str2nr(argvars, rettv)
18604 typval_T *argvars;
18605 typval_T *rettv;
18606{
18607 int base = 10;
18608 char_u *p;
18609 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018610 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018611
18612 if (argvars[1].v_type != VAR_UNKNOWN)
18613 {
18614 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018615 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018616 {
18617 EMSG(_(e_invarg));
18618 return;
18619 }
18620 }
18621
18622 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018623 if (*p == '+')
18624 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018625 switch (base)
18626 {
18627 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18628 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18629 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18630 default: what = 0;
18631 }
18632 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018633 rettv->vval.v_number = n;
18634}
18635
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636#ifdef HAVE_STRFTIME
18637/*
18638 * "strftime({format}[, {time}])" function
18639 */
18640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 typval_T *argvars;
18643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644{
18645 char_u result_buf[256];
18646 struct tm *curtime;
18647 time_t seconds;
18648 char_u *p;
18649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018653 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 seconds = time(NULL);
18655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 curtime = localtime(&seconds);
18658 /* MSVC returns NULL for an invalid value of seconds. */
18659 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 else
18662 {
18663# ifdef FEAT_MBYTE
18664 vimconv_T conv;
18665 char_u *enc;
18666
18667 conv.vc_type = CONV_NONE;
18668 enc = enc_locale();
18669 convert_setup(&conv, p_enc, enc);
18670 if (conv.vc_type != CONV_NONE)
18671 p = string_convert(&conv, p, NULL);
18672# endif
18673 if (p != NULL)
18674 (void)strftime((char *)result_buf, sizeof(result_buf),
18675 (char *)p, curtime);
18676 else
18677 result_buf[0] = NUL;
18678
18679# ifdef FEAT_MBYTE
18680 if (conv.vc_type != CONV_NONE)
18681 vim_free(p);
18682 convert_setup(&conv, enc, p_enc);
18683 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 else
18686# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688
18689# ifdef FEAT_MBYTE
18690 /* Release conversion descriptors */
18691 convert_setup(&conv, NULL, NULL);
18692 vim_free(enc);
18693# endif
18694 }
18695}
18696#endif
18697
18698/*
18699 * "stridx()" function
18700 */
18701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018703 typval_T *argvars;
18704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705{
18706 char_u buf[NUMBUFLEN];
18707 char_u *needle;
18708 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018709 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018711 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018713 needle = get_tv_string_chk(&argvars[1]);
18714 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018715 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018716 if (needle == NULL || haystack == NULL)
18717 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
Bram Moolenaar33570922005-01-25 22:26:29 +000018719 if (argvars[2].v_type != VAR_UNKNOWN)
18720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018721 int error = FALSE;
18722
18723 start_idx = get_tv_number_chk(&argvars[2], &error);
18724 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018725 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018726 if (start_idx >= 0)
18727 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018728 }
18729
18730 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18731 if (pos != NULL)
18732 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733}
18734
18735/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018736 * "string()" function
18737 */
18738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018740 typval_T *argvars;
18741 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018742{
18743 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018744 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018747 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018748 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018749 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751}
18752
18753/*
18754 * "strlen()" function
18755 */
18756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 typval_T *argvars;
18759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018761 rettv->vval.v_number = (varnumber_T)(STRLEN(
18762 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763}
18764
18765/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018766 * "strchars()" function
18767 */
18768 static void
18769f_strchars(argvars, rettv)
18770 typval_T *argvars;
18771 typval_T *rettv;
18772{
18773 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018774 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018775#ifdef FEAT_MBYTE
18776 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018777 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018778#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018779
18780 if (argvars[1].v_type != VAR_UNKNOWN)
18781 skipcc = get_tv_number_chk(&argvars[1], NULL);
18782 if (skipcc < 0 || skipcc > 1)
18783 EMSG(_(e_invarg));
18784 else
18785 {
18786#ifdef FEAT_MBYTE
18787 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18788 while (*s != NUL)
18789 {
18790 func_mb_ptr2char_adv(&s);
18791 ++len;
18792 }
18793 rettv->vval.v_number = len;
18794#else
18795 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18796#endif
18797 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018798}
18799
18800/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018801 * "strdisplaywidth()" function
18802 */
18803 static void
18804f_strdisplaywidth(argvars, rettv)
18805 typval_T *argvars;
18806 typval_T *rettv;
18807{
18808 char_u *s = get_tv_string(&argvars[0]);
18809 int col = 0;
18810
18811 if (argvars[1].v_type != VAR_UNKNOWN)
18812 col = get_tv_number(&argvars[1]);
18813
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018814 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018815}
18816
18817/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018818 * "strwidth()" function
18819 */
18820 static void
18821f_strwidth(argvars, rettv)
18822 typval_T *argvars;
18823 typval_T *rettv;
18824{
18825 char_u *s = get_tv_string(&argvars[0]);
18826
18827 rettv->vval.v_number = (varnumber_T)(
18828#ifdef FEAT_MBYTE
18829 mb_string2cells(s, -1)
18830#else
18831 STRLEN(s)
18832#endif
18833 );
18834}
18835
18836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 * "strpart()" function
18838 */
18839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018840f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 typval_T *argvars;
18842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843{
18844 char_u *p;
18845 int n;
18846 int len;
18847 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018850 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 slen = (int)STRLEN(p);
18852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018853 n = get_tv_number_chk(&argvars[1], &error);
18854 if (error)
18855 len = 0;
18856 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018857 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 else
18859 len = slen - n; /* default len: all bytes that are available. */
18860
18861 /*
18862 * Only return the overlap between the specified part and the actual
18863 * string.
18864 */
18865 if (n < 0)
18866 {
18867 len += n;
18868 n = 0;
18869 }
18870 else if (n > slen)
18871 n = slen;
18872 if (len < 0)
18873 len = 0;
18874 else if (n + len > slen)
18875 len = slen - n;
18876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->v_type = VAR_STRING;
18878 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879}
18880
18881/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 * "strridx()" function
18883 */
18884 static void
18885f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018886 typval_T *argvars;
18887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888{
18889 char_u buf[NUMBUFLEN];
18890 char_u *needle;
18891 char_u *haystack;
18892 char_u *rest;
18893 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018894 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018896 needle = get_tv_string_chk(&argvars[1]);
18897 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898
18899 rettv->vval.v_number = -1;
18900 if (needle == NULL || haystack == NULL)
18901 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018902
18903 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018904 if (argvars[2].v_type != VAR_UNKNOWN)
18905 {
18906 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018907 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018908 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018909 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018910 }
18911 else
18912 end_idx = haystack_len;
18913
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018915 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018916 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018917 lastmatch = haystack + end_idx;
18918 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018920 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 for (rest = haystack; *rest != '\0'; ++rest)
18922 {
18923 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018924 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 break;
18926 lastmatch = rest;
18927 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018928 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929
18930 if (lastmatch == NULL)
18931 rettv->vval.v_number = -1;
18932 else
18933 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18934}
18935
18936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 * "strtrans()" function
18938 */
18939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 typval_T *argvars;
18942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018944 rettv->v_type = VAR_STRING;
18945 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946}
18947
18948/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 * "submatch()" function
18950 */
18951 static void
18952f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018953 typval_T *argvars;
18954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955{
Bram Moolenaar41571762014-04-02 19:00:58 +020018956 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018957 int no;
18958 int retList = 0;
18959
18960 no = (int)get_tv_number_chk(&argvars[0], &error);
18961 if (error)
18962 return;
18963 error = FALSE;
18964 if (argvars[1].v_type != VAR_UNKNOWN)
18965 retList = get_tv_number_chk(&argvars[1], &error);
18966 if (error)
18967 return;
18968
18969 if (retList == 0)
18970 {
18971 rettv->v_type = VAR_STRING;
18972 rettv->vval.v_string = reg_submatch(no);
18973 }
18974 else
18975 {
18976 rettv->v_type = VAR_LIST;
18977 rettv->vval.v_list = reg_submatch_list(no);
18978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979}
18980
18981/*
18982 * "substitute()" function
18983 */
18984 static void
18985f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018986 typval_T *argvars;
18987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988{
18989 char_u patbuf[NUMBUFLEN];
18990 char_u subbuf[NUMBUFLEN];
18991 char_u flagsbuf[NUMBUFLEN];
18992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018993 char_u *str = get_tv_string_chk(&argvars[0]);
18994 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18995 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18996 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18997
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018999 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19000 rettv->vval.v_string = NULL;
19001 else
19002 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003}
19004
19005/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019006 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019009f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019010 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012{
19013 int id = 0;
19014#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019015 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 long col;
19017 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019018 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019020 lnum = get_tv_lnum(argvars); /* -1 on type error */
19021 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19022 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019024 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019025 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019026 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027#endif
19028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030}
19031
19032/*
19033 * "synIDattr(id, what [, mode])" function
19034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019036f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039{
19040 char_u *p = NULL;
19041#ifdef FEAT_SYN_HL
19042 int id;
19043 char_u *what;
19044 char_u *mode;
19045 char_u modebuf[NUMBUFLEN];
19046 int modec;
19047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019048 id = get_tv_number(&argvars[0]);
19049 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019050 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019052 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019054 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 modec = 0; /* replace invalid with current */
19056 }
19057 else
19058 {
19059#ifdef FEAT_GUI
19060 if (gui.in_use)
19061 modec = 'g';
19062 else
19063#endif
19064 if (t_colors > 1)
19065 modec = 'c';
19066 else
19067 modec = 't';
19068 }
19069
19070
19071 switch (TOLOWER_ASC(what[0]))
19072 {
19073 case 'b':
19074 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19075 p = highlight_color(id, what, modec);
19076 else /* bold */
19077 p = highlight_has_attr(id, HL_BOLD, modec);
19078 break;
19079
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019080 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 p = highlight_color(id, what, modec);
19082 break;
19083
19084 case 'i':
19085 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19086 p = highlight_has_attr(id, HL_INVERSE, modec);
19087 else /* italic */
19088 p = highlight_has_attr(id, HL_ITALIC, modec);
19089 break;
19090
19091 case 'n': /* name */
19092 p = get_highlight_name(NULL, id - 1);
19093 break;
19094
19095 case 'r': /* reverse */
19096 p = highlight_has_attr(id, HL_INVERSE, modec);
19097 break;
19098
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019099 case 's':
19100 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19101 p = highlight_color(id, what, modec);
19102 else /* standout */
19103 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 break;
19105
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019106 case 'u':
19107 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19108 /* underline */
19109 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19110 else
19111 /* undercurl */
19112 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 break;
19114 }
19115
19116 if (p != NULL)
19117 p = vim_strsave(p);
19118#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119 rettv->v_type = VAR_STRING;
19120 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121}
19122
19123/*
19124 * "synIDtrans(id)" function
19125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019128 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130{
19131 int id;
19132
19133#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
19136 if (id > 0)
19137 id = syn_get_final_id(id);
19138 else
19139#endif
19140 id = 0;
19141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019142 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143}
19144
19145/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019146 * "synconcealed(lnum, col)" function
19147 */
19148 static void
19149f_synconcealed(argvars, rettv)
19150 typval_T *argvars UNUSED;
19151 typval_T *rettv;
19152{
19153#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19154 long lnum;
19155 long col;
19156 int syntax_flags = 0;
19157 int cchar;
19158 int matchid = 0;
19159 char_u str[NUMBUFLEN];
19160#endif
19161
19162 rettv->v_type = VAR_LIST;
19163 rettv->vval.v_list = NULL;
19164
19165#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19166 lnum = get_tv_lnum(argvars); /* -1 on type error */
19167 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19168
19169 vim_memset(str, NUL, sizeof(str));
19170
19171 if (rettv_list_alloc(rettv) != FAIL)
19172 {
19173 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19174 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19175 && curwin->w_p_cole > 0)
19176 {
19177 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19178 syntax_flags = get_syntax_info(&matchid);
19179
19180 /* get the conceal character */
19181 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19182 {
19183 cchar = syn_get_sub_char();
19184 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19185 cchar = lcs_conceal;
19186 if (cchar != NUL)
19187 {
19188# ifdef FEAT_MBYTE
19189 if (has_mbyte)
19190 (*mb_char2bytes)(cchar, str);
19191 else
19192# endif
19193 str[0] = cchar;
19194 }
19195 }
19196 }
19197
19198 list_append_number(rettv->vval.v_list,
19199 (syntax_flags & HL_CONCEAL) != 0);
19200 /* -1 to auto-determine strlen */
19201 list_append_string(rettv->vval.v_list, str, -1);
19202 list_append_number(rettv->vval.v_list, matchid);
19203 }
19204#endif
19205}
19206
19207/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019208 * "synstack(lnum, col)" function
19209 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019210 static void
19211f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019212 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019213 typval_T *rettv;
19214{
19215#ifdef FEAT_SYN_HL
19216 long lnum;
19217 long col;
19218 int i;
19219 int id;
19220#endif
19221
19222 rettv->v_type = VAR_LIST;
19223 rettv->vval.v_list = NULL;
19224
19225#ifdef FEAT_SYN_HL
19226 lnum = get_tv_lnum(argvars); /* -1 on type error */
19227 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19228
19229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019230 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019231 && rettv_list_alloc(rettv) != FAIL)
19232 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019233 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019234 for (i = 0; ; ++i)
19235 {
19236 id = syn_get_stack_item(i);
19237 if (id < 0)
19238 break;
19239 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19240 break;
19241 }
19242 }
19243#endif
19244}
19245
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019247get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 typval_T *argvars;
19249 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019250 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019252 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019254 char_u *infile = NULL;
19255 char_u buf[NUMBUFLEN];
19256 int err = FALSE;
19257 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019258 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019259 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019261 rettv->v_type = VAR_STRING;
19262 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019263 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019264 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019266 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019267 {
19268 /*
19269 * Write the string to a temp file, to be used for input of the shell
19270 * command.
19271 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019272 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019273 {
19274 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019275 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019276 }
19277
19278 fd = mch_fopen((char *)infile, WRITEBIN);
19279 if (fd == NULL)
19280 {
19281 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019282 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019283 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019284 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019285 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019286 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19287 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019288 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019289 else
19290 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019291 size_t len;
19292
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019293 p = get_tv_string_buf_chk(&argvars[1], buf);
19294 if (p == NULL)
19295 {
19296 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019297 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019298 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019299 len = STRLEN(p);
19300 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019301 err = TRUE;
19302 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019303 if (fclose(fd) != 0)
19304 err = TRUE;
19305 if (err)
19306 {
19307 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019309 }
19310 }
19311
Bram Moolenaar52a72462014-08-29 15:53:52 +020019312 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19313 * echoes typeahead, that messes up the display. */
19314 if (!msg_silent)
19315 flags += SHELL_COOKED;
19316
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 int len;
19320 listitem_T *li;
19321 char_u *s = NULL;
19322 char_u *start;
19323 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019324 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325
Bram Moolenaar52a72462014-08-29 15:53:52 +020019326 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019327 if (res == NULL)
19328 goto errret;
19329
19330 list = list_alloc();
19331 if (list == NULL)
19332 goto errret;
19333
19334 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019336 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019337 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019339 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019340
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019341 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019342 if (s == NULL)
19343 goto errret;
19344
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019345 for (p = s; start < end; ++p, ++start)
19346 *p = *start == NUL ? NL : *start;
19347 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019348
19349 li = listitem_alloc();
19350 if (li == NULL)
19351 {
19352 vim_free(s);
19353 goto errret;
19354 }
19355 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019356 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019357 li->li_tv.vval.v_string = s;
19358 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019360
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019361 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019362 rettv->v_type = VAR_LIST;
19363 rettv->vval.v_list = list;
19364 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019366 else
19367 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019368 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019369#ifdef USE_CR
19370 /* translate <CR> into <NL> */
19371 if (res != NULL)
19372 {
19373 char_u *s;
19374
19375 for (s = res; *s; ++s)
19376 {
19377 if (*s == CAR)
19378 *s = NL;
19379 }
19380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381#else
19382# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019383 /* translate <CR><NL> into <NL> */
19384 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 char_u *s, *d;
19387
19388 d = res;
19389 for (s = res; *s; ++s)
19390 {
19391 if (s[0] == CAR && s[1] == NL)
19392 ++s;
19393 *d++ = *s;
19394 }
19395 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397# endif
19398#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019399 rettv->vval.v_string = res;
19400 res = NULL;
19401 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019402
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019403errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019404 if (infile != NULL)
19405 {
19406 mch_remove(infile);
19407 vim_free(infile);
19408 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019409 if (res != NULL)
19410 vim_free(res);
19411 if (list != NULL)
19412 list_free(list, TRUE);
19413}
19414
19415/*
19416 * "system()" function
19417 */
19418 static void
19419f_system(argvars, rettv)
19420 typval_T *argvars;
19421 typval_T *rettv;
19422{
19423 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19424}
19425
19426/*
19427 * "systemlist()" function
19428 */
19429 static void
19430f_systemlist(argvars, rettv)
19431 typval_T *argvars;
19432 typval_T *rettv;
19433{
19434 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435}
19436
19437/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019438 * "tabpagebuflist()" function
19439 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019440 static void
19441f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019442 typval_T *argvars UNUSED;
19443 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019444{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019445#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019446 tabpage_T *tp;
19447 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019448
19449 if (argvars[0].v_type == VAR_UNKNOWN)
19450 wp = firstwin;
19451 else
19452 {
19453 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19454 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019455 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019456 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019457 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019458 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019459 for (; wp != NULL; wp = wp->w_next)
19460 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019461 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019462 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019463 }
19464#endif
19465}
19466
19467
19468/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019469 * "tabpagenr()" function
19470 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019471 static void
19472f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019473 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019474 typval_T *rettv;
19475{
19476 int nr = 1;
19477#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019478 char_u *arg;
19479
19480 if (argvars[0].v_type != VAR_UNKNOWN)
19481 {
19482 arg = get_tv_string_chk(&argvars[0]);
19483 nr = 0;
19484 if (arg != NULL)
19485 {
19486 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019487 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019488 else
19489 EMSG2(_(e_invexpr2), arg);
19490 }
19491 }
19492 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019493 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019494#endif
19495 rettv->vval.v_number = nr;
19496}
19497
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019498
19499#ifdef FEAT_WINDOWS
19500static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19501
19502/*
19503 * Common code for tabpagewinnr() and winnr().
19504 */
19505 static int
19506get_winnr(tp, argvar)
19507 tabpage_T *tp;
19508 typval_T *argvar;
19509{
19510 win_T *twin;
19511 int nr = 1;
19512 win_T *wp;
19513 char_u *arg;
19514
19515 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19516 if (argvar->v_type != VAR_UNKNOWN)
19517 {
19518 arg = get_tv_string_chk(argvar);
19519 if (arg == NULL)
19520 nr = 0; /* type error; errmsg already given */
19521 else if (STRCMP(arg, "$") == 0)
19522 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19523 else if (STRCMP(arg, "#") == 0)
19524 {
19525 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19526 if (twin == NULL)
19527 nr = 0;
19528 }
19529 else
19530 {
19531 EMSG2(_(e_invexpr2), arg);
19532 nr = 0;
19533 }
19534 }
19535
19536 if (nr > 0)
19537 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19538 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019539 {
19540 if (wp == NULL)
19541 {
19542 /* didn't find it in this tabpage */
19543 nr = 0;
19544 break;
19545 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019546 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019547 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019548 return nr;
19549}
19550#endif
19551
19552/*
19553 * "tabpagewinnr()" function
19554 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019555 static void
19556f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019557 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019558 typval_T *rettv;
19559{
19560 int nr = 1;
19561#ifdef FEAT_WINDOWS
19562 tabpage_T *tp;
19563
19564 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19565 if (tp == NULL)
19566 nr = 0;
19567 else
19568 nr = get_winnr(tp, &argvars[1]);
19569#endif
19570 rettv->vval.v_number = nr;
19571}
19572
19573
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019574/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019575 * "tagfiles()" function
19576 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019577 static void
19578f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019579 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019580 typval_T *rettv;
19581{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019582 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019583 tagname_T tn;
19584 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019585
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019586 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019587 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019588 fname = alloc(MAXPATHL);
19589 if (fname == NULL)
19590 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019592 for (first = TRUE; ; first = FALSE)
19593 if (get_tagfname(&tn, first, fname) == FAIL
19594 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019595 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019596 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019597 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019598}
19599
19600/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019601 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019602 */
19603 static void
19604f_taglist(argvars, rettv)
19605 typval_T *argvars;
19606 typval_T *rettv;
19607{
19608 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019609
19610 tag_pattern = get_tv_string(&argvars[0]);
19611
19612 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019613 if (*tag_pattern == NUL)
19614 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019616 if (rettv_list_alloc(rettv) == OK)
19617 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019618}
19619
19620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 * "tempname()" function
19622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627{
19628 static int x = 'A';
19629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019631 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632
19633 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19634 * names. Skip 'I' and 'O', they are used for shell redirection. */
19635 do
19636 {
19637 if (x == 'Z')
19638 x = '0';
19639 else if (x == '9')
19640 x = 'A';
19641 else
19642 {
19643#ifdef EBCDIC
19644 if (x == 'I')
19645 x = 'J';
19646 else if (x == 'R')
19647 x = 'S';
19648 else
19649#endif
19650 ++x;
19651 }
19652 } while (x == 'I' || x == 'O');
19653}
19654
19655/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019656 * "test(list)" function: Just checking the walls...
19657 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019658 static void
19659f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019660 typval_T *argvars UNUSED;
19661 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019662{
19663 /* Used for unit testing. Change the code below to your liking. */
19664#if 0
19665 listitem_T *li;
19666 list_T *l;
19667 char_u *bad, *good;
19668
19669 if (argvars[0].v_type != VAR_LIST)
19670 return;
19671 l = argvars[0].vval.v_list;
19672 if (l == NULL)
19673 return;
19674 li = l->lv_first;
19675 if (li == NULL)
19676 return;
19677 bad = get_tv_string(&li->li_tv);
19678 li = li->li_next;
19679 if (li == NULL)
19680 return;
19681 good = get_tv_string(&li->li_tv);
19682 rettv->vval.v_number = test_edit_score(bad, good);
19683#endif
19684}
19685
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019686#ifdef FEAT_FLOAT
19687/*
19688 * "tan()" function
19689 */
19690 static void
19691f_tan(argvars, rettv)
19692 typval_T *argvars;
19693 typval_T *rettv;
19694{
19695 float_T f;
19696
19697 rettv->v_type = VAR_FLOAT;
19698 if (get_float_arg(argvars, &f) == OK)
19699 rettv->vval.v_float = tan(f);
19700 else
19701 rettv->vval.v_float = 0.0;
19702}
19703
19704/*
19705 * "tanh()" function
19706 */
19707 static void
19708f_tanh(argvars, rettv)
19709 typval_T *argvars;
19710 typval_T *rettv;
19711{
19712 float_T f;
19713
19714 rettv->v_type = VAR_FLOAT;
19715 if (get_float_arg(argvars, &f) == OK)
19716 rettv->vval.v_float = tanh(f);
19717 else
19718 rettv->vval.v_float = 0.0;
19719}
19720#endif
19721
Bram Moolenaard52d9742005-08-21 22:20:28 +000019722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 * "tolower(string)" function
19724 */
19725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 typval_T *argvars;
19728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729{
19730 char_u *p;
19731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 p = vim_strsave(get_tv_string(&argvars[0]));
19733 rettv->v_type = VAR_STRING;
19734 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735
19736 if (p != NULL)
19737 while (*p != NUL)
19738 {
19739#ifdef FEAT_MBYTE
19740 int l;
19741
19742 if (enc_utf8)
19743 {
19744 int c, lc;
19745
19746 c = utf_ptr2char(p);
19747 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019748 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 /* TODO: reallocate string when byte count changes. */
19750 if (utf_char2len(lc) == l)
19751 utf_char2bytes(lc, p);
19752 p += l;
19753 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019754 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 p += l; /* skip multi-byte character */
19756 else
19757#endif
19758 {
19759 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19760 ++p;
19761 }
19762 }
19763}
19764
19765/*
19766 * "toupper(string)" function
19767 */
19768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 typval_T *argvars;
19771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019774 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775}
19776
19777/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019778 * "tr(string, fromstr, tostr)" function
19779 */
19780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 typval_T *argvars;
19783 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019784{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019785 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019786 char_u *fromstr;
19787 char_u *tostr;
19788 char_u *p;
19789#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019790 int inlen;
19791 int fromlen;
19792 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019793 int idx;
19794 char_u *cpstr;
19795 int cplen;
19796 int first = TRUE;
19797#endif
19798 char_u buf[NUMBUFLEN];
19799 char_u buf2[NUMBUFLEN];
19800 garray_T ga;
19801
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019802 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19804 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019805
19806 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 rettv->v_type = VAR_STRING;
19808 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019809 if (fromstr == NULL || tostr == NULL)
19810 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019811 ga_init2(&ga, (int)sizeof(char), 80);
19812
19813#ifdef FEAT_MBYTE
19814 if (!has_mbyte)
19815#endif
19816 /* not multi-byte: fromstr and tostr must be the same length */
19817 if (STRLEN(fromstr) != STRLEN(tostr))
19818 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019819#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019820error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019821#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019822 EMSG2(_(e_invarg2), fromstr);
19823 ga_clear(&ga);
19824 return;
19825 }
19826
19827 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019828 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019829 {
19830#ifdef FEAT_MBYTE
19831 if (has_mbyte)
19832 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019833 inlen = (*mb_ptr2len)(in_str);
19834 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019835 cplen = inlen;
19836 idx = 0;
19837 for (p = fromstr; *p != NUL; p += fromlen)
19838 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019839 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019840 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019841 {
19842 for (p = tostr; *p != NUL; p += tolen)
19843 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019844 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019845 if (idx-- == 0)
19846 {
19847 cplen = tolen;
19848 cpstr = p;
19849 break;
19850 }
19851 }
19852 if (*p == NUL) /* tostr is shorter than fromstr */
19853 goto error;
19854 break;
19855 }
19856 ++idx;
19857 }
19858
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019859 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019860 {
19861 /* Check that fromstr and tostr have the same number of
19862 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019863 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864 first = FALSE;
19865 for (p = tostr; *p != NUL; p += tolen)
19866 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019867 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019868 --idx;
19869 }
19870 if (idx != 0)
19871 goto error;
19872 }
19873
Bram Moolenaarcde88542015-08-11 19:14:00 +020019874 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019875 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019876 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019877
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019878 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019879 }
19880 else
19881#endif
19882 {
19883 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019884 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019885 if (p != NULL)
19886 ga_append(&ga, tostr[p - fromstr]);
19887 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019888 ga_append(&ga, *in_str);
19889 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019890 }
19891 }
19892
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019893 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019894 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019895 ga_append(&ga, NUL);
19896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019898}
19899
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019900#ifdef FEAT_FLOAT
19901/*
19902 * "trunc({float})" function
19903 */
19904 static void
19905f_trunc(argvars, rettv)
19906 typval_T *argvars;
19907 typval_T *rettv;
19908{
19909 float_T f;
19910
19911 rettv->v_type = VAR_FLOAT;
19912 if (get_float_arg(argvars, &f) == OK)
19913 /* trunc() is not in C90, use floor() or ceil() instead. */
19914 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19915 else
19916 rettv->vval.v_float = 0.0;
19917}
19918#endif
19919
Bram Moolenaar8299df92004-07-10 09:47:34 +000019920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 * "type(expr)" function
19922 */
19923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 typval_T *argvars;
19926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019928 int n;
19929
19930 switch (argvars[0].v_type)
19931 {
19932 case VAR_NUMBER: n = 0; break;
19933 case VAR_STRING: n = 1; break;
19934 case VAR_FUNC: n = 2; break;
19935 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019936 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019937#ifdef FEAT_FLOAT
19938 case VAR_FLOAT: n = 5; break;
19939#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019940 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19941 }
19942 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943}
19944
19945/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019946 * "undofile(name)" function
19947 */
19948 static void
19949f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019950 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019951 typval_T *rettv;
19952{
19953 rettv->v_type = VAR_STRING;
19954#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019955 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019956 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019957
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019958 if (*fname == NUL)
19959 {
19960 /* If there is no file name there will be no undo file. */
19961 rettv->vval.v_string = NULL;
19962 }
19963 else
19964 {
19965 char_u *ffname = FullName_save(fname, FALSE);
19966
19967 if (ffname != NULL)
19968 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19969 vim_free(ffname);
19970 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019971 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019972#else
19973 rettv->vval.v_string = NULL;
19974#endif
19975}
19976
19977/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019978 * "undotree()" function
19979 */
19980 static void
19981f_undotree(argvars, rettv)
19982 typval_T *argvars UNUSED;
19983 typval_T *rettv;
19984{
19985 if (rettv_dict_alloc(rettv) == OK)
19986 {
19987 dict_T *dict = rettv->vval.v_dict;
19988 list_T *list;
19989
Bram Moolenaar730cde92010-06-27 05:18:54 +020019990 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019991 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019992 dict_add_nr_str(dict, "save_last",
19993 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019994 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19995 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019996 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019997
19998 list = list_alloc();
19999 if (list != NULL)
20000 {
20001 u_eval_tree(curbuf->b_u_oldhead, list);
20002 dict_add_list(dict, "entries", list);
20003 }
20004 }
20005}
20006
20007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020008 * "values(dict)" function
20009 */
20010 static void
20011f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 typval_T *argvars;
20013 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020014{
20015 dict_list(argvars, rettv, 1);
20016}
20017
20018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 * "virtcol(string)" function
20020 */
20021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020023 typval_T *argvars;
20024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025{
20026 colnr_T vcol = 0;
20027 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020028 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020030 fp = var2fpos(&argvars[0], FALSE, &fnum);
20031 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20032 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 {
20034 getvvcol(curwin, fp, NULL, NULL, &vcol);
20035 ++vcol;
20036 }
20037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039}
20040
20041/*
20042 * "visualmode()" function
20043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020046 typval_T *argvars UNUSED;
20047 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 char_u str[2];
20050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 str[0] = curbuf->b_visual_mode_eval;
20053 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055
20056 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020057 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059}
20060
20061/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020062 * "wildmenumode()" function
20063 */
20064 static void
20065f_wildmenumode(argvars, rettv)
20066 typval_T *argvars UNUSED;
20067 typval_T *rettv UNUSED;
20068{
20069#ifdef FEAT_WILDMENU
20070 if (wild_menu_showing)
20071 rettv->vval.v_number = 1;
20072#endif
20073}
20074
20075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 * "winbufnr(nr)" function
20077 */
20078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 typval_T *argvars;
20081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082{
20083 win_T *wp;
20084
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020085 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090}
20091
20092/*
20093 * "wincol()" function
20094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020097 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099{
20100 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102}
20103
20104/*
20105 * "winheight(nr)" function
20106 */
20107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 typval_T *argvars;
20110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111{
20112 win_T *wp;
20113
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020114 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119}
20120
20121/*
20122 * "winline()" function
20123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020126 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128{
20129 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131}
20132
20133/*
20134 * "winnr()" function
20135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140{
20141 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020142
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020144 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147}
20148
20149/*
20150 * "winrestcmd()" function
20151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156{
20157#ifdef FEAT_WINDOWS
20158 win_T *wp;
20159 int winnr = 1;
20160 garray_T ga;
20161 char_u buf[50];
20162
20163 ga_init2(&ga, (int)sizeof(char), 70);
20164 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20165 {
20166 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20167 ga_concat(&ga, buf);
20168# ifdef FEAT_VERTSPLIT
20169 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20170 ga_concat(&ga, buf);
20171# endif
20172 ++winnr;
20173 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020174 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020178 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181}
20182
20183/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020184 * "winrestview()" function
20185 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020186 static void
20187f_winrestview(argvars, rettv)
20188 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020189 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020190{
20191 dict_T *dict;
20192
20193 if (argvars[0].v_type != VAR_DICT
20194 || (dict = argvars[0].vval.v_dict) == NULL)
20195 EMSG(_(e_invarg));
20196 else
20197 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020198 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20199 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20200 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20201 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020202#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020203 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20204 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020205#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020206 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20207 {
20208 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20209 curwin->w_set_curswant = FALSE;
20210 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020211
Bram Moolenaar82c25852014-05-28 16:47:16 +020020212 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20213 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020214#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020215 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20216 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020217#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020218 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20219 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20220 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20221 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020222
20223 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020224 win_new_height(curwin, curwin->w_height);
20225# ifdef FEAT_VERTSPLIT
20226 win_new_width(curwin, W_WIDTH(curwin));
20227# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020228 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020229
Bram Moolenaarb851a962014-10-31 15:45:52 +010020230 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020231 curwin->w_topline = 1;
20232 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20233 curwin->w_topline = curbuf->b_ml.ml_line_count;
20234#ifdef FEAT_DIFF
20235 check_topfill(curwin, TRUE);
20236#endif
20237 }
20238}
20239
20240/*
20241 * "winsaveview()" function
20242 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020243 static void
20244f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020245 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020246 typval_T *rettv;
20247{
20248 dict_T *dict;
20249
Bram Moolenaara800b422010-06-27 01:15:55 +020020250 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020251 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020252 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020253
20254 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20255 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20256#ifdef FEAT_VIRTUALEDIT
20257 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20258#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020259 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020260 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20261
20262 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20263#ifdef FEAT_DIFF
20264 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20265#endif
20266 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20267 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20268}
20269
20270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 * "winwidth(nr)" function
20272 */
20273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 typval_T *argvars;
20276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277{
20278 win_T *wp;
20279
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020280 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 else
20284#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288#endif
20289}
20290
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020292 * "wordcount()" function
20293 */
20294 static void
20295f_wordcount(argvars, rettv)
20296 typval_T *argvars UNUSED;
20297 typval_T *rettv;
20298{
20299 if (rettv_dict_alloc(rettv) == FAIL)
20300 return;
20301 cursor_pos_info(rettv->vval.v_dict);
20302}
20303
20304/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020305 * Write list of strings to file
20306 */
20307 static int
20308write_list(fd, list, binary)
20309 FILE *fd;
20310 list_T *list;
20311 int binary;
20312{
20313 listitem_T *li;
20314 int c;
20315 int ret = OK;
20316 char_u *s;
20317
20318 for (li = list->lv_first; li != NULL; li = li->li_next)
20319 {
20320 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20321 {
20322 if (*s == '\n')
20323 c = putc(NUL, fd);
20324 else
20325 c = putc(*s, fd);
20326 if (c == EOF)
20327 {
20328 ret = FAIL;
20329 break;
20330 }
20331 }
20332 if (!binary || li->li_next != NULL)
20333 if (putc('\n', fd) == EOF)
20334 {
20335 ret = FAIL;
20336 break;
20337 }
20338 if (ret == FAIL)
20339 {
20340 EMSG(_(e_write));
20341 break;
20342 }
20343 }
20344 return ret;
20345}
20346
20347/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020348 * "writefile()" function
20349 */
20350 static void
20351f_writefile(argvars, rettv)
20352 typval_T *argvars;
20353 typval_T *rettv;
20354{
20355 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020356 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020357 char_u *fname;
20358 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020359 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020360
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020361 if (check_restricted() || check_secure())
20362 return;
20363
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020364 if (argvars[0].v_type != VAR_LIST)
20365 {
20366 EMSG2(_(e_listarg), "writefile()");
20367 return;
20368 }
20369 if (argvars[0].vval.v_list == NULL)
20370 return;
20371
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020372 if (argvars[2].v_type != VAR_UNKNOWN)
20373 {
20374 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20375 binary = TRUE;
20376 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20377 append = TRUE;
20378 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020379
20380 /* Always open the file in binary mode, library functions have a mind of
20381 * their own about CR-LF conversion. */
20382 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020383 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20384 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020385 {
20386 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20387 ret = -1;
20388 }
20389 else
20390 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020391 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20392 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020393 fclose(fd);
20394 }
20395
20396 rettv->vval.v_number = ret;
20397}
20398
20399/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020400 * "xor(expr, expr)" function
20401 */
20402 static void
20403f_xor(argvars, rettv)
20404 typval_T *argvars;
20405 typval_T *rettv;
20406{
20407 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20408 ^ get_tv_number_chk(&argvars[1], NULL);
20409}
20410
20411
20412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020414 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 */
20416 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020417var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020419 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020420 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020422 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020424 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425
Bram Moolenaara5525202006-03-02 22:52:09 +000020426 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020427 if (varp->v_type == VAR_LIST)
20428 {
20429 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020430 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020431 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020432 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020433
20434 l = varp->vval.v_list;
20435 if (l == NULL)
20436 return NULL;
20437
20438 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020439 pos.lnum = list_find_nr(l, 0L, &error);
20440 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020441 return NULL; /* invalid line number */
20442
20443 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020444 pos.col = list_find_nr(l, 1L, &error);
20445 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020446 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020447 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020448
20449 /* We accept "$" for the column number: last column. */
20450 li = list_find(l, 1L);
20451 if (li != NULL && li->li_tv.v_type == VAR_STRING
20452 && li->li_tv.vval.v_string != NULL
20453 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20454 pos.col = len + 1;
20455
Bram Moolenaara5525202006-03-02 22:52:09 +000020456 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020457 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020458 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020459 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020460
Bram Moolenaara5525202006-03-02 22:52:09 +000020461#ifdef FEAT_VIRTUALEDIT
20462 /* Get the virtual offset. Defaults to zero. */
20463 pos.coladd = list_find_nr(l, 2L, &error);
20464 if (error)
20465 pos.coladd = 0;
20466#endif
20467
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020468 return &pos;
20469 }
20470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020471 name = get_tv_string_chk(varp);
20472 if (name == NULL)
20473 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020474 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020476 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20477 {
20478 if (VIsual_active)
20479 return &VIsual;
20480 return &curwin->w_cursor;
20481 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020482 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020484 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20486 return NULL;
20487 return pp;
20488 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020489
20490#ifdef FEAT_VIRTUALEDIT
20491 pos.coladd = 0;
20492#endif
20493
Bram Moolenaar477933c2007-07-17 14:32:23 +000020494 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020495 {
20496 pos.col = 0;
20497 if (name[1] == '0') /* "w0": first visible line */
20498 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020499 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020500 pos.lnum = curwin->w_topline;
20501 return &pos;
20502 }
20503 else if (name[1] == '$') /* "w$": last visible line */
20504 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020505 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020506 pos.lnum = curwin->w_botline - 1;
20507 return &pos;
20508 }
20509 }
20510 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020512 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 {
20514 pos.lnum = curbuf->b_ml.ml_line_count;
20515 pos.col = 0;
20516 }
20517 else
20518 {
20519 pos.lnum = curwin->w_cursor.lnum;
20520 pos.col = (colnr_T)STRLEN(ml_get_curline());
20521 }
20522 return &pos;
20523 }
20524 return NULL;
20525}
20526
20527/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020528 * Convert list in "arg" into a position and optional file number.
20529 * When "fnump" is NULL there is no file number, only 3 items.
20530 * Note that the column is passed on as-is, the caller may want to decrement
20531 * it to use 1 for the first column.
20532 * Return FAIL when conversion is not possible, doesn't check the position for
20533 * validity.
20534 */
20535 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020536list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020537 typval_T *arg;
20538 pos_T *posp;
20539 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020540 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020541{
20542 list_T *l = arg->vval.v_list;
20543 long i = 0;
20544 long n;
20545
Bram Moolenaar493c1782014-05-28 14:34:46 +020020546 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20547 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020548 if (arg->v_type != VAR_LIST
20549 || l == NULL
20550 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020551 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020552 return FAIL;
20553
20554 if (fnump != NULL)
20555 {
20556 n = list_find_nr(l, i++, NULL); /* fnum */
20557 if (n < 0)
20558 return FAIL;
20559 if (n == 0)
20560 n = curbuf->b_fnum; /* current buffer */
20561 *fnump = n;
20562 }
20563
20564 n = list_find_nr(l, i++, NULL); /* lnum */
20565 if (n < 0)
20566 return FAIL;
20567 posp->lnum = n;
20568
20569 n = list_find_nr(l, i++, NULL); /* col */
20570 if (n < 0)
20571 return FAIL;
20572 posp->col = n;
20573
20574#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020575 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020576 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020577 posp->coladd = 0;
20578 else
20579 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020580#endif
20581
Bram Moolenaar493c1782014-05-28 14:34:46 +020020582 if (curswantp != NULL)
20583 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20584
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020585 return OK;
20586}
20587
20588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 * Get the length of an environment variable name.
20590 * Advance "arg" to the first character after the name.
20591 * Return 0 for error.
20592 */
20593 static int
20594get_env_len(arg)
20595 char_u **arg;
20596{
20597 char_u *p;
20598 int len;
20599
20600 for (p = *arg; vim_isIDc(*p); ++p)
20601 ;
20602 if (p == *arg) /* no name found */
20603 return 0;
20604
20605 len = (int)(p - *arg);
20606 *arg = p;
20607 return len;
20608}
20609
20610/*
20611 * Get the length of the name of a function or internal variable.
20612 * "arg" is advanced to the first non-white character after the name.
20613 * Return 0 if something is wrong.
20614 */
20615 static int
20616get_id_len(arg)
20617 char_u **arg;
20618{
20619 char_u *p;
20620 int len;
20621
20622 /* Find the end of the name. */
20623 for (p = *arg; eval_isnamec(*p); ++p)
20624 ;
20625 if (p == *arg) /* no name found */
20626 return 0;
20627
20628 len = (int)(p - *arg);
20629 *arg = skipwhite(p);
20630
20631 return len;
20632}
20633
20634/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020635 * Get the length of the name of a variable or function.
20636 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020638 * Return -1 if curly braces expansion failed.
20639 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 * If the name contains 'magic' {}'s, expand them and return the
20641 * expanded name in an allocated string via 'alias' - caller must free.
20642 */
20643 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020644get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 char_u **arg;
20646 char_u **alias;
20647 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020648 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649{
20650 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 char_u *p;
20652 char_u *expr_start;
20653 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654
20655 *alias = NULL; /* default to no alias */
20656
20657 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20658 && (*arg)[2] == (int)KE_SNR)
20659 {
20660 /* hard coded <SNR>, already translated */
20661 *arg += 3;
20662 return get_id_len(arg) + 3;
20663 }
20664 len = eval_fname_script(*arg);
20665 if (len > 0)
20666 {
20667 /* literal "<SID>", "s:" or "<SNR>" */
20668 *arg += len;
20669 }
20670
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020672 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020674 p = find_name_end(*arg, &expr_start, &expr_end,
20675 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 if (expr_start != NULL)
20677 {
20678 char_u *temp_string;
20679
20680 if (!evaluate)
20681 {
20682 len += (int)(p - *arg);
20683 *arg = skipwhite(p);
20684 return len;
20685 }
20686
20687 /*
20688 * Include any <SID> etc in the expanded string:
20689 * Thus the -len here.
20690 */
20691 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20692 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020693 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 *alias = temp_string;
20695 *arg = skipwhite(p);
20696 return (int)STRLEN(temp_string);
20697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698
20699 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020700 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 EMSG2(_(e_invexpr2), *arg);
20702
20703 return len;
20704}
20705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706/*
20707 * Find the end of a variable or function name, taking care of magic braces.
20708 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20709 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020710 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020711 * Return a pointer to just after the name. Equal to "arg" if there is no
20712 * valid name.
20713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020715find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 char_u *arg;
20717 char_u **expr_start;
20718 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020719 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020721 int mb_nest = 0;
20722 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 char_u *p;
20724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020725 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020727 *expr_start = NULL;
20728 *expr_end = NULL;
20729 }
20730
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020731 /* Quick check for valid starting character. */
20732 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20733 return arg;
20734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 for (p = arg; *p != NUL
20736 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020737 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020738 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020739 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020740 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020741 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020742 if (*p == '\'')
20743 {
20744 /* skip over 'string' to avoid counting [ and ] inside it. */
20745 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20746 ;
20747 if (*p == NUL)
20748 break;
20749 }
20750 else if (*p == '"')
20751 {
20752 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20753 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20754 if (*p == '\\' && p[1] != NUL)
20755 ++p;
20756 if (*p == NUL)
20757 break;
20758 }
20759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020762 if (*p == '[')
20763 ++br_nest;
20764 else if (*p == ']')
20765 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020770 if (*p == '{')
20771 {
20772 mb_nest++;
20773 if (expr_start != NULL && *expr_start == NULL)
20774 *expr_start = p;
20775 }
20776 else if (*p == '}')
20777 {
20778 mb_nest--;
20779 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20780 *expr_end = p;
20781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
20784
20785 return p;
20786}
20787
20788/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020789 * Expands out the 'magic' {}'s in a variable/function name.
20790 * Note that this can call itself recursively, to deal with
20791 * constructs like foo{bar}{baz}{bam}
20792 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20793 * "in_start" ^
20794 * "expr_start" ^
20795 * "expr_end" ^
20796 * "in_end" ^
20797 *
20798 * Returns a new allocated string, which the caller must free.
20799 * Returns NULL for failure.
20800 */
20801 static char_u *
20802make_expanded_name(in_start, expr_start, expr_end, in_end)
20803 char_u *in_start;
20804 char_u *expr_start;
20805 char_u *expr_end;
20806 char_u *in_end;
20807{
20808 char_u c1;
20809 char_u *retval = NULL;
20810 char_u *temp_result;
20811 char_u *nextcmd = NULL;
20812
20813 if (expr_end == NULL || in_end == NULL)
20814 return NULL;
20815 *expr_start = NUL;
20816 *expr_end = NUL;
20817 c1 = *in_end;
20818 *in_end = NUL;
20819
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020820 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020821 if (temp_result != NULL && nextcmd == NULL)
20822 {
20823 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20824 + (in_end - expr_end) + 1));
20825 if (retval != NULL)
20826 {
20827 STRCPY(retval, in_start);
20828 STRCAT(retval, temp_result);
20829 STRCAT(retval, expr_end + 1);
20830 }
20831 }
20832 vim_free(temp_result);
20833
20834 *in_end = c1; /* put char back for error messages */
20835 *expr_start = '{';
20836 *expr_end = '}';
20837
20838 if (retval != NULL)
20839 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020840 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020841 if (expr_start != NULL)
20842 {
20843 /* Further expansion! */
20844 temp_result = make_expanded_name(retval, expr_start,
20845 expr_end, temp_result);
20846 vim_free(retval);
20847 retval = temp_result;
20848 }
20849 }
20850
20851 return retval;
20852}
20853
20854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020856 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 */
20858 static int
20859eval_isnamec(c)
20860 int c;
20861{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020862 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20863}
20864
20865/*
20866 * Return TRUE if character "c" can be used as the first character in a
20867 * variable or function name (excluding '{' and '}').
20868 */
20869 static int
20870eval_isnamec1(c)
20871 int c;
20872{
20873 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874}
20875
20876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 * Set number v: variable to "val".
20878 */
20879 void
20880set_vim_var_nr(idx, val)
20881 int idx;
20882 long val;
20883{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020884 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885}
20886
20887/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020888 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 */
20890 long
20891get_vim_var_nr(idx)
20892 int idx;
20893{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020894 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895}
20896
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020897/*
20898 * Get string v: variable value. Uses a static buffer, can only be used once.
20899 */
20900 char_u *
20901get_vim_var_str(idx)
20902 int idx;
20903{
20904 return get_tv_string(&vimvars[idx].vv_tv);
20905}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020906
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020908 * Get List v: variable value. Caller must take care of reference count when
20909 * needed.
20910 */
20911 list_T *
20912get_vim_var_list(idx)
20913 int idx;
20914{
20915 return vimvars[idx].vv_list;
20916}
20917
20918/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020919 * Set v:char to character "c".
20920 */
20921 void
20922set_vim_var_char(c)
20923 int c;
20924{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020925 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020926
20927#ifdef FEAT_MBYTE
20928 if (has_mbyte)
20929 buf[(*mb_char2bytes)(c, buf)] = NUL;
20930 else
20931#endif
20932 {
20933 buf[0] = c;
20934 buf[1] = NUL;
20935 }
20936 set_vim_var_string(VV_CHAR, buf, -1);
20937}
20938
20939/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020940 * Set v:count to "count" and v:count1 to "count1".
20941 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 */
20943 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020944set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 long count;
20946 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020947 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020949 if (set_prevcount)
20950 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020951 vimvars[VV_COUNT].vv_nr = count;
20952 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953}
20954
20955/*
20956 * Set string v: variable to a copy of "val".
20957 */
20958 void
20959set_vim_var_string(idx, val, len)
20960 int idx;
20961 char_u *val;
20962 int len; /* length of "val" to use or -1 (whole string) */
20963{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020964 /* Need to do this (at least) once, since we can't initialize a union.
20965 * Will always be invoked when "v:progname" is set. */
20966 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20967
Bram Moolenaare9a41262005-01-15 22:18:47 +000020968 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020970 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020972 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020974 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975}
20976
20977/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020978 * Set List v: variable to "val".
20979 */
20980 void
20981set_vim_var_list(idx, val)
20982 int idx;
20983 list_T *val;
20984{
20985 list_unref(vimvars[idx].vv_list);
20986 vimvars[idx].vv_list = val;
20987 if (val != NULL)
20988 ++val->lv_refcount;
20989}
20990
20991/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020992 * Set Dictionary v: variable to "val".
20993 */
20994 void
20995set_vim_var_dict(idx, val)
20996 int idx;
20997 dict_T *val;
20998{
20999 int todo;
21000 hashitem_T *hi;
21001
21002 dict_unref(vimvars[idx].vv_dict);
21003 vimvars[idx].vv_dict = val;
21004 if (val != NULL)
21005 {
21006 ++val->dv_refcount;
21007
21008 /* Set readonly */
21009 todo = (int)val->dv_hashtab.ht_used;
21010 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21011 {
21012 if (HASHITEM_EMPTY(hi))
21013 continue;
21014 --todo;
21015 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21016 }
21017 }
21018}
21019
21020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 * Set v:register if needed.
21022 */
21023 void
21024set_reg_var(c)
21025 int c;
21026{
21027 char_u regname;
21028
21029 if (c == 0 || c == ' ')
21030 regname = '"';
21031 else
21032 regname = c;
21033 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021034 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 set_vim_var_string(VV_REG, &regname, 1);
21036}
21037
21038/*
21039 * Get or set v:exception. If "oldval" == NULL, return the current value.
21040 * Otherwise, restore the value to "oldval" and return NULL.
21041 * Must always be called in pairs to save and restore v:exception! Does not
21042 * take care of memory allocations.
21043 */
21044 char_u *
21045v_exception(oldval)
21046 char_u *oldval;
21047{
21048 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021049 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050
Bram Moolenaare9a41262005-01-15 22:18:47 +000021051 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 return NULL;
21053}
21054
21055/*
21056 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21057 * Otherwise, restore the value to "oldval" and return NULL.
21058 * Must always be called in pairs to save and restore v:throwpoint! Does not
21059 * take care of memory allocations.
21060 */
21061 char_u *
21062v_throwpoint(oldval)
21063 char_u *oldval;
21064{
21065 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021066 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067
Bram Moolenaare9a41262005-01-15 22:18:47 +000021068 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 return NULL;
21070}
21071
21072#if defined(FEAT_AUTOCMD) || defined(PROTO)
21073/*
21074 * Set v:cmdarg.
21075 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21076 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21077 * Must always be called in pairs!
21078 */
21079 char_u *
21080set_cmdarg(eap, oldarg)
21081 exarg_T *eap;
21082 char_u *oldarg;
21083{
21084 char_u *oldval;
21085 char_u *newval;
21086 unsigned len;
21087
Bram Moolenaare9a41262005-01-15 22:18:47 +000021088 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021089 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021091 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021092 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 }
21095
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021096 if (eap->force_bin == FORCE_BIN)
21097 len = 6;
21098 else if (eap->force_bin == FORCE_NOBIN)
21099 len = 8;
21100 else
21101 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021102
21103 if (eap->read_edit)
21104 len += 7;
21105
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021106 if (eap->force_ff != 0)
21107 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21108# ifdef FEAT_MBYTE
21109 if (eap->force_enc != 0)
21110 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021111 if (eap->bad_char != 0)
21112 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021113# endif
21114
21115 newval = alloc(len + 1);
21116 if (newval == NULL)
21117 return NULL;
21118
21119 if (eap->force_bin == FORCE_BIN)
21120 sprintf((char *)newval, " ++bin");
21121 else if (eap->force_bin == FORCE_NOBIN)
21122 sprintf((char *)newval, " ++nobin");
21123 else
21124 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021125
21126 if (eap->read_edit)
21127 STRCAT(newval, " ++edit");
21128
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021129 if (eap->force_ff != 0)
21130 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21131 eap->cmd + eap->force_ff);
21132# ifdef FEAT_MBYTE
21133 if (eap->force_enc != 0)
21134 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21135 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021136 if (eap->bad_char == BAD_KEEP)
21137 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21138 else if (eap->bad_char == BAD_DROP)
21139 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21140 else if (eap->bad_char != 0)
21141 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021142# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021143 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021144 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145}
21146#endif
21147
21148/*
21149 * Get the value of internal variable "name".
21150 * Return OK or FAIL.
21151 */
21152 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021153get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 char_u *name;
21155 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021156 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021157 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021158 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021159 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160{
21161 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021162 typval_T *tv = NULL;
21163 typval_T atv;
21164 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166
21167 /* truncate the name, so that we can use strcmp() */
21168 cc = name[len];
21169 name[len] = NUL;
21170
21171 /*
21172 * Check for "b:changedtick".
21173 */
21174 if (STRCMP(name, "b:changedtick") == 0)
21175 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021176 atv.v_type = VAR_NUMBER;
21177 atv.vval.v_number = curbuf->b_changedtick;
21178 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 }
21180
21181 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 * Check for user-defined variables.
21183 */
21184 else
21185 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021186 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021188 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021190 if (dip != NULL)
21191 *dip = v;
21192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 }
21194
Bram Moolenaare9a41262005-01-15 22:18:47 +000021195 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021197 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021198 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 ret = FAIL;
21200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021202 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203
21204 name[len] = cc;
21205
21206 return ret;
21207}
21208
21209/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021210 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21211 * Also handle function call with Funcref variable: func(expr)
21212 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21213 */
21214 static int
21215handle_subscript(arg, rettv, evaluate, verbose)
21216 char_u **arg;
21217 typval_T *rettv;
21218 int evaluate; /* do more than finding the end */
21219 int verbose; /* give error messages */
21220{
21221 int ret = OK;
21222 dict_T *selfdict = NULL;
21223 char_u *s;
21224 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021225 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021226
21227 while (ret == OK
21228 && (**arg == '['
21229 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021230 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021231 && !vim_iswhite(*(*arg - 1)))
21232 {
21233 if (**arg == '(')
21234 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021235 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021236 if (evaluate)
21237 {
21238 functv = *rettv;
21239 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021240
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021241 /* Invoke the function. Recursive! */
21242 s = functv.vval.v_string;
21243 }
21244 else
21245 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021246 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021247 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21248 &len, evaluate, selfdict);
21249
21250 /* Clear the funcref afterwards, so that deleting it while
21251 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021252 if (evaluate)
21253 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021254
21255 /* Stop the expression evaluation when immediately aborting on
21256 * error, or when an interrupt occurred or an exception was thrown
21257 * but not caught. */
21258 if (aborting())
21259 {
21260 if (ret == OK)
21261 clear_tv(rettv);
21262 ret = FAIL;
21263 }
21264 dict_unref(selfdict);
21265 selfdict = NULL;
21266 }
21267 else /* **arg == '[' || **arg == '.' */
21268 {
21269 dict_unref(selfdict);
21270 if (rettv->v_type == VAR_DICT)
21271 {
21272 selfdict = rettv->vval.v_dict;
21273 if (selfdict != NULL)
21274 ++selfdict->dv_refcount;
21275 }
21276 else
21277 selfdict = NULL;
21278 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21279 {
21280 clear_tv(rettv);
21281 ret = FAIL;
21282 }
21283 }
21284 }
21285 dict_unref(selfdict);
21286 return ret;
21287}
21288
21289/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021290 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291 * value).
21292 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021293 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021294alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021295{
Bram Moolenaar33570922005-01-25 22:26:29 +000021296 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297}
21298
21299/*
21300 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 * The string "s" must have been allocated, it is consumed.
21302 * Return NULL for out of memory, the variable otherwise.
21303 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021304 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021305alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 char_u *s;
21307{
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 rettv = alloc_tv();
21311 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021313 rettv->v_type = VAR_STRING;
21314 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 }
21316 else
21317 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021318 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319}
21320
21321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021322 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021324 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327{
21328 if (varp != NULL)
21329 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021330 switch (varp->v_type)
21331 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021332 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 func_unref(varp->vval.v_string);
21334 /*FALLTHROUGH*/
21335 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021336 vim_free(varp->vval.v_string);
21337 break;
21338 case VAR_LIST:
21339 list_unref(varp->vval.v_list);
21340 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021341 case VAR_DICT:
21342 dict_unref(varp->vval.v_dict);
21343 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021344 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021345#ifdef FEAT_FLOAT
21346 case VAR_FLOAT:
21347#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021348 case VAR_UNKNOWN:
21349 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021350 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021351 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021352 break;
21353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 vim_free(varp);
21355 }
21356}
21357
21358/*
21359 * Free the memory for a variable value and set the value to NULL or 0.
21360 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021361 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021363 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364{
21365 if (varp != NULL)
21366 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021367 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021369 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 func_unref(varp->vval.v_string);
21371 /*FALLTHROUGH*/
21372 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021373 vim_free(varp->vval.v_string);
21374 varp->vval.v_string = NULL;
21375 break;
21376 case VAR_LIST:
21377 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021378 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021379 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021380 case VAR_DICT:
21381 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021382 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021383 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021384 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021385 varp->vval.v_number = 0;
21386 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021387#ifdef FEAT_FLOAT
21388 case VAR_FLOAT:
21389 varp->vval.v_float = 0.0;
21390 break;
21391#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 case VAR_UNKNOWN:
21393 break;
21394 default:
21395 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021397 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 }
21399}
21400
21401/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 * Set the value of a variable to NULL without freeing items.
21403 */
21404 static void
21405init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021406 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407{
21408 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021409 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410}
21411
21412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 * Get the number value of a variable.
21414 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021415 * For incompatible types, return 0.
21416 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21417 * caller of incompatible types: it sets *denote to TRUE if "denote"
21418 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 */
21420 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021422 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021424 int error = FALSE;
21425
21426 return get_tv_number_chk(varp, &error); /* return 0L on error */
21427}
21428
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021429 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021430get_tv_number_chk(varp, denote)
21431 typval_T *varp;
21432 int *denote;
21433{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021438 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021439 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021440#ifdef FEAT_FLOAT
21441 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021442 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021443 break;
21444#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021446 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021447 break;
21448 case VAR_STRING:
21449 if (varp->vval.v_string != NULL)
21450 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021451 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021452 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021453 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021454 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021455 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021456 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021457 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021458 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021459 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021460 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021463 if (denote == NULL) /* useful for values that must be unsigned */
21464 n = -1;
21465 else
21466 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468}
21469
21470/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021471 * Get the lnum from the first argument.
21472 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021473 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 */
21475 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021476get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478{
Bram Moolenaar33570922005-01-25 22:26:29 +000021479 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 linenr_T lnum;
21481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021482 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 if (lnum == 0) /* no valid number, try using line() */
21484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 rettv.v_type = VAR_NUMBER;
21486 f_line(argvars, &rettv);
21487 lnum = rettv.vval.v_number;
21488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 }
21490 return lnum;
21491}
21492
21493/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021494 * Get the lnum from the first argument.
21495 * Also accepts "$", then "buf" is used.
21496 * Returns 0 on error.
21497 */
21498 static linenr_T
21499get_tv_lnum_buf(argvars, buf)
21500 typval_T *argvars;
21501 buf_T *buf;
21502{
21503 if (argvars[0].v_type == VAR_STRING
21504 && argvars[0].vval.v_string != NULL
21505 && argvars[0].vval.v_string[0] == '$'
21506 && buf != NULL)
21507 return buf->b_ml.ml_line_count;
21508 return get_tv_number_chk(&argvars[0], NULL);
21509}
21510
21511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 * Get the string value of a variable.
21513 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021514 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21515 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 * If the String variable has never been set, return an empty string.
21517 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021518 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21519 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 */
21521 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021522get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524{
21525 static char_u mybuf[NUMBUFLEN];
21526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528}
21529
21530 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021531get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021533 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021535 char_u *res = get_tv_string_buf_chk(varp, buf);
21536
21537 return res != NULL ? res : (char_u *)"";
21538}
21539
Bram Moolenaar7d647822014-04-05 21:28:56 +020021540/*
21541 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21542 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021543 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021544get_tv_string_chk(varp)
21545 typval_T *varp;
21546{
21547 static char_u mybuf[NUMBUFLEN];
21548
21549 return get_tv_string_buf_chk(varp, mybuf);
21550}
21551
21552 static char_u *
21553get_tv_string_buf_chk(varp, buf)
21554 typval_T *varp;
21555 char_u *buf;
21556{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021557 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021559 case VAR_NUMBER:
21560 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21561 return buf;
21562 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021563 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021564 break;
21565 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021566 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021567 break;
21568 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021569 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021570 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021571#ifdef FEAT_FLOAT
21572 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021573 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021574 break;
21575#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 case VAR_STRING:
21577 if (varp->vval.v_string != NULL)
21578 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021579 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021580 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021584 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
21587/*
21588 * Find variable "name" in the list of variables.
21589 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021591 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021592 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021594 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021595find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021598 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021601 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602
Bram Moolenaara7043832005-01-21 11:56:39 +000021603 ht = find_var_ht(name, &varname);
21604 if (htp != NULL)
21605 *htp = ht;
21606 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021608 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609}
21610
21611/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021612 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021613 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021616find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021618 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021619 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021620 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021621{
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 hashitem_T *hi;
21623
21624 if (*varname == NUL)
21625 {
21626 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021627 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021629 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 case 'g': return &globvars_var;
21631 case 'v': return &vimvars_var;
21632 case 'b': return &curbuf->b_bufvar;
21633 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021634#ifdef FEAT_WINDOWS
21635 case 't': return &curtab->tp_winvar;
21636#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021637 case 'l': return current_funccal == NULL
21638 ? NULL : &current_funccal->l_vars_var;
21639 case 'a': return current_funccal == NULL
21640 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 }
21642 return NULL;
21643 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021644
21645 hi = hash_find(ht, varname);
21646 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 {
21648 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021649 * worked find the variable again. Don't auto-load a script if it was
21650 * loaded already, otherwise it would be loaded every time when
21651 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021652 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021653 {
21654 /* Note: script_autoload() may make "hi" invalid. It must either
21655 * be obtained again or not used. */
21656 if (!script_autoload(varname, FALSE) || aborting())
21657 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021658 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021659 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 if (HASHITEM_EMPTY(hi))
21661 return NULL;
21662 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021664}
21665
21666/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021668 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021669 * Set "varname" to the start of name without ':'.
21670 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021672find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 char_u *name;
21674 char_u **varname;
21675{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021676 hashitem_T *hi;
21677
Bram Moolenaar73627d02015-08-11 15:46:09 +020021678 if (name[0] == NUL)
21679 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 if (name[1] != ':')
21681 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021682 /* The name must not start with a colon or #. */
21683 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 return NULL;
21685 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021686
21687 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021688 hi = hash_find(&compat_hashtab, name);
21689 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021690 return &compat_hashtab;
21691
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 return &globvarht; /* global variable */
21694 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 }
21696 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021697 if (*name == 'g') /* global variable */
21698 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021699 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21700 */
21701 if (vim_strchr(name + 2, ':') != NULL
21702 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021703 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021705 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021707 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021708#ifdef FEAT_WINDOWS
21709 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021710 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021711#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021712 if (*name == 'v') /* v: variable */
21713 return &vimvarht;
21714 if (*name == 'a' && current_funccal != NULL) /* function argument */
21715 return &current_funccal->l_avars.dv_hashtab;
21716 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21717 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 if (*name == 's' /* script variable */
21719 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21720 return &SCRIPT_VARS(current_SID);
21721 return NULL;
21722}
21723
21724/*
21725 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021726 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 * Returns NULL when it doesn't exist.
21728 */
21729 char_u *
21730get_var_value(name)
21731 char_u *name;
21732{
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021735 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 if (v == NULL)
21737 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739}
21740
21741/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 * sourcing this script and when executing functions defined in the script.
21744 */
21745 void
21746new_script_vars(id)
21747 scid_T id;
21748{
Bram Moolenaara7043832005-01-21 11:56:39 +000021749 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021750 hashtab_T *ht;
21751 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021752
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021755 /* Re-allocating ga_data means that an ht_array pointing to
21756 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021758 for (i = 1; i <= ga_scripts.ga_len; ++i)
21759 {
21760 ht = &SCRIPT_VARS(i);
21761 if (ht->ht_mask == HT_INIT_SIZE - 1)
21762 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021763 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021764 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021765 }
21766
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 while (ga_scripts.ga_len < id)
21768 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021769 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021770 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021771 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 }
21774 }
21775}
21776
21777/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021778 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21779 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 */
21781 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021782init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 dict_T *dict;
21784 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021785 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021788 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021789 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021790 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021791 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 dict_var->di_tv.vval.v_dict = dict;
21793 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021794 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021795 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21796 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797}
21798
21799/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021800 * Unreference a dictionary initialized by init_var_dict().
21801 */
21802 void
21803unref_var_dict(dict)
21804 dict_T *dict;
21805{
21806 /* Now the dict needs to be freed if no one else is using it, go back to
21807 * normal reference counting. */
21808 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21809 dict_unref(dict);
21810}
21811
21812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021814 * Frees all allocated variables and the value they contain.
21815 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 */
21817 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021818vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021819 hashtab_T *ht;
21820{
21821 vars_clear_ext(ht, TRUE);
21822}
21823
21824/*
21825 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21826 */
21827 static void
21828vars_clear_ext(ht, free_val)
21829 hashtab_T *ht;
21830 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831{
Bram Moolenaara7043832005-01-21 11:56:39 +000021832 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021833 hashitem_T *hi;
21834 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021837 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021838 for (hi = ht->ht_array; todo > 0; ++hi)
21839 {
21840 if (!HASHITEM_EMPTY(hi))
21841 {
21842 --todo;
21843
Bram Moolenaar33570922005-01-25 22:26:29 +000021844 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021845 * ht_array might change then. hash_clear() takes care of it
21846 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 v = HI2DI(hi);
21848 if (free_val)
21849 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021850 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021852 }
21853 }
21854 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021855 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856}
21857
Bram Moolenaara7043832005-01-21 11:56:39 +000021858/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 * Delete a variable from hashtab "ht" at item "hi".
21860 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021863delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 hashtab_T *ht;
21865 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866{
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021868
21869 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 clear_tv(&di->di_tv);
21871 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872}
21873
21874/*
21875 * List the value of one internal variable.
21876 */
21877 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021878list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021881 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021883 char_u *tofree;
21884 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021885 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021886
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021887 current_copyID += COPYID_INC;
21888 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021889 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021890 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021891 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892}
21893
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021895list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 char_u *prefix;
21897 char_u *name;
21898 int type;
21899 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021900 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901{
Bram Moolenaar31859182007-08-14 20:41:13 +000021902 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21903 msg_start();
21904 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 if (name != NULL) /* "a:" vars don't have a name stored */
21906 msg_puts(name);
21907 msg_putchar(' ');
21908 msg_advance(22);
21909 if (type == VAR_NUMBER)
21910 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 else if (type == VAR_FUNC)
21912 msg_putchar('*');
21913 else if (type == VAR_LIST)
21914 {
21915 msg_putchar('[');
21916 if (*string == '[')
21917 ++string;
21918 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021919 else if (type == VAR_DICT)
21920 {
21921 msg_putchar('{');
21922 if (*string == '{')
21923 ++string;
21924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 else
21926 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021927
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021929
21930 if (type == VAR_FUNC)
21931 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021932 if (*first)
21933 {
21934 msg_clr_eos();
21935 *first = FALSE;
21936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937}
21938
21939/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021940 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 * If the variable already exists, the value is updated.
21942 * Otherwise the variable is created.
21943 */
21944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021945set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021947 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949{
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021952 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021954 ht = find_var_ht(name, &varname);
21955 if (ht == NULL || *varname == NUL)
21956 {
21957 EMSG2(_(e_illvar), name);
21958 return;
21959 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021960 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021961
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021962 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21963 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021964
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021968 if (var_check_ro(v->di_flags, name, FALSE)
21969 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 return;
21971 if (v->di_tv.v_type != tv->v_type
21972 && !((v->di_tv.v_type == VAR_STRING
21973 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021974 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021975 || tv->v_type == VAR_NUMBER))
21976#ifdef FEAT_FLOAT
21977 && !((v->di_tv.v_type == VAR_NUMBER
21978 || v->di_tv.v_type == VAR_FLOAT)
21979 && (tv->v_type == VAR_NUMBER
21980 || tv->v_type == VAR_FLOAT))
21981#endif
21982 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021983 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021984 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021985 return;
21986 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021987
21988 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021989 * Handle setting internal v: variables separately where needed to
21990 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 */
21992 if (ht == &vimvarht)
21993 {
21994 if (v->di_tv.v_type == VAR_STRING)
21995 {
21996 vim_free(v->di_tv.vval.v_string);
21997 if (copy || tv->v_type != VAR_STRING)
21998 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21999 else
22000 {
22001 /* Take over the string to avoid an extra alloc/free. */
22002 v->di_tv.vval.v_string = tv->vval.v_string;
22003 tv->vval.v_string = NULL;
22004 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022005 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022007 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022010 if (STRCMP(varname, "searchforward") == 0)
22011 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022012#ifdef FEAT_SEARCH_EXTRA
22013 else if (STRCMP(varname, "hlsearch") == 0)
22014 {
22015 no_hlsearch = !v->di_tv.vval.v_number;
22016 redraw_all_later(SOME_VALID);
22017 }
22018#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022019 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022021 else if (v->di_tv.v_type != tv->v_type)
22022 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 }
22024
22025 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 }
22027 else /* add a new variable */
22028 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022029 /* Can't add "v:" variable. */
22030 if (ht == &vimvarht)
22031 {
22032 EMSG2(_(e_illvar), name);
22033 return;
22034 }
22035
Bram Moolenaar92124a32005-06-17 22:03:40 +000022036 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022037 if (!valid_varname(varname))
22038 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022039
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022040 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22041 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022042 if (v == NULL)
22043 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022044 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022047 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 return;
22049 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022050 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022052
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022053 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022054 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022055 else
22056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022057 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022058 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022059 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061}
22062
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022064 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022065 * Also give an error message.
22066 */
22067 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022068var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022069 int flags;
22070 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022071 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022072{
22073 if (flags & DI_FLAGS_RO)
22074 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022075 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022076 return TRUE;
22077 }
22078 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22079 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022080 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 return TRUE;
22082 }
22083 return FALSE;
22084}
22085
22086/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022087 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22088 * Also give an error message.
22089 */
22090 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022091var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022092 int flags;
22093 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022094 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022095{
22096 if (flags & DI_FLAGS_FIX)
22097 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022098 EMSG2(_("E795: Cannot delete variable %s"),
22099 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022100 return TRUE;
22101 }
22102 return FALSE;
22103}
22104
22105/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022106 * Check if a funcref is assigned to a valid variable name.
22107 * Return TRUE and give an error if not.
22108 */
22109 static int
22110var_check_func_name(name, new_var)
22111 char_u *name; /* points to start of variable name */
22112 int new_var; /* TRUE when creating the variable */
22113{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022114 /* Allow for w: b: s: and t:. */
22115 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022116 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22117 ? name[2] : name[0]))
22118 {
22119 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22120 name);
22121 return TRUE;
22122 }
22123 /* Don't allow hiding a function. When "v" is not NULL we might be
22124 * assigning another function to the same var, the type is checked
22125 * below. */
22126 if (new_var && function_exists(name))
22127 {
22128 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22129 name);
22130 return TRUE;
22131 }
22132 return FALSE;
22133}
22134
22135/*
22136 * Check if a variable name is valid.
22137 * Return FALSE and give an error if not.
22138 */
22139 static int
22140valid_varname(varname)
22141 char_u *varname;
22142{
22143 char_u *p;
22144
22145 for (p = varname; *p != NUL; ++p)
22146 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22147 && *p != AUTOLOAD_CHAR)
22148 {
22149 EMSG2(_(e_illvar), varname);
22150 return FALSE;
22151 }
22152 return TRUE;
22153}
22154
22155/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022156 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022157 * Also give an error message, using "name" or _("name") when use_gettext is
22158 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022159 */
22160 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022161tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022162 int lock;
22163 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022164 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022165{
22166 if (lock & VAR_LOCKED)
22167 {
22168 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022169 name == NULL ? (char_u *)_("Unknown")
22170 : use_gettext ? (char_u *)_(name)
22171 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022172 return TRUE;
22173 }
22174 if (lock & VAR_FIXED)
22175 {
22176 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022177 name == NULL ? (char_u *)_("Unknown")
22178 : use_gettext ? (char_u *)_(name)
22179 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022180 return TRUE;
22181 }
22182 return FALSE;
22183}
22184
22185/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022189 * It is OK for "from" and "to" to point to the same item. This is used to
22190 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022191 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022192 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022193copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 typval_T *from;
22195 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022197 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022198 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022199 switch (from->v_type)
22200 {
22201 case VAR_NUMBER:
22202 to->vval.v_number = from->vval.v_number;
22203 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022204#ifdef FEAT_FLOAT
22205 case VAR_FLOAT:
22206 to->vval.v_float = from->vval.v_float;
22207 break;
22208#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022209 case VAR_STRING:
22210 case VAR_FUNC:
22211 if (from->vval.v_string == NULL)
22212 to->vval.v_string = NULL;
22213 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022214 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022216 if (from->v_type == VAR_FUNC)
22217 func_ref(to->vval.v_string);
22218 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022219 break;
22220 case VAR_LIST:
22221 if (from->vval.v_list == NULL)
22222 to->vval.v_list = NULL;
22223 else
22224 {
22225 to->vval.v_list = from->vval.v_list;
22226 ++to->vval.v_list->lv_refcount;
22227 }
22228 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022229 case VAR_DICT:
22230 if (from->vval.v_dict == NULL)
22231 to->vval.v_dict = NULL;
22232 else
22233 {
22234 to->vval.v_dict = from->vval.v_dict;
22235 ++to->vval.v_dict->dv_refcount;
22236 }
22237 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022240 break;
22241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242}
22243
22244/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022245 * Make a copy of an item.
22246 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022247 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22248 * reference to an already copied list/dict can be used.
22249 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022250 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022251 static int
22252item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 typval_T *from;
22254 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022255 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022256 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022257{
22258 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022259 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022260
Bram Moolenaar33570922005-01-25 22:26:29 +000022261 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022262 {
22263 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022264 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022265 }
22266 ++recurse;
22267
22268 switch (from->v_type)
22269 {
22270 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022271#ifdef FEAT_FLOAT
22272 case VAR_FLOAT:
22273#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022274 case VAR_STRING:
22275 case VAR_FUNC:
22276 copy_tv(from, to);
22277 break;
22278 case VAR_LIST:
22279 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022280 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022281 if (from->vval.v_list == NULL)
22282 to->vval.v_list = NULL;
22283 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22284 {
22285 /* use the copy made earlier */
22286 to->vval.v_list = from->vval.v_list->lv_copylist;
22287 ++to->vval.v_list->lv_refcount;
22288 }
22289 else
22290 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22291 if (to->vval.v_list == NULL)
22292 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022293 break;
22294 case VAR_DICT:
22295 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022296 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022297 if (from->vval.v_dict == NULL)
22298 to->vval.v_dict = NULL;
22299 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22300 {
22301 /* use the copy made earlier */
22302 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22303 ++to->vval.v_dict->dv_refcount;
22304 }
22305 else
22306 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22307 if (to->vval.v_dict == NULL)
22308 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022309 break;
22310 default:
22311 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022312 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022313 }
22314 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022315 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022316}
22317
22318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 * ":echo expr1 ..." print each argument separated with a space, add a
22320 * newline at the end.
22321 * ":echon expr1 ..." print each argument plain.
22322 */
22323 void
22324ex_echo(eap)
22325 exarg_T *eap;
22326{
22327 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022328 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022329 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 char_u *p;
22331 int needclr = TRUE;
22332 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022333 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334
22335 if (eap->skip)
22336 ++emsg_skip;
22337 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22338 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022339 /* If eval1() causes an error message the text from the command may
22340 * still need to be cleared. E.g., "echo 22,44". */
22341 need_clr_eos = needclr;
22342
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 {
22346 /*
22347 * Report the invalid expression unless the expression evaluation
22348 * has been cancelled due to an aborting error, an interrupt, or an
22349 * exception.
22350 */
22351 if (!aborting())
22352 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022353 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 break;
22355 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022356 need_clr_eos = FALSE;
22357
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 if (!eap->skip)
22359 {
22360 if (atstart)
22361 {
22362 atstart = FALSE;
22363 /* Call msg_start() after eval1(), evaluating the expression
22364 * may cause a message to appear. */
22365 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022366 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022367 /* Mark the saved text as finishing the line, so that what
22368 * follows is displayed on a new line when scrolling back
22369 * at the more prompt. */
22370 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 }
22374 else if (eap->cmdidx == CMD_echo)
22375 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022376 current_copyID += COPYID_INC;
22377 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022378 if (p != NULL)
22379 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022381 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022383 if (*p != TAB && needclr)
22384 {
22385 /* remove any text still there from the command */
22386 msg_clr_eos();
22387 needclr = FALSE;
22388 }
22389 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 }
22391 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022392 {
22393#ifdef FEAT_MBYTE
22394 if (has_mbyte)
22395 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022396 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022397
22398 (void)msg_outtrans_len_attr(p, i, echo_attr);
22399 p += i - 1;
22400 }
22401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022403 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022406 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022408 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 arg = skipwhite(arg);
22410 }
22411 eap->nextcmd = check_nextcmd(arg);
22412
22413 if (eap->skip)
22414 --emsg_skip;
22415 else
22416 {
22417 /* remove text that may still be there from the command */
22418 if (needclr)
22419 msg_clr_eos();
22420 if (eap->cmdidx == CMD_echo)
22421 msg_end();
22422 }
22423}
22424
22425/*
22426 * ":echohl {name}".
22427 */
22428 void
22429ex_echohl(eap)
22430 exarg_T *eap;
22431{
22432 int id;
22433
22434 id = syn_name2id(eap->arg);
22435 if (id == 0)
22436 echo_attr = 0;
22437 else
22438 echo_attr = syn_id2attr(id);
22439}
22440
22441/*
22442 * ":execute expr1 ..." execute the result of an expression.
22443 * ":echomsg expr1 ..." Print a message
22444 * ":echoerr expr1 ..." Print an error
22445 * Each gets spaces around each argument and a newline at the end for
22446 * echo commands
22447 */
22448 void
22449ex_execute(eap)
22450 exarg_T *eap;
22451{
22452 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 int ret = OK;
22455 char_u *p;
22456 garray_T ga;
22457 int len;
22458 int save_did_emsg;
22459
22460 ga_init2(&ga, 1, 80);
22461
22462 if (eap->skip)
22463 ++emsg_skip;
22464 while (*arg != NUL && *arg != '|' && *arg != '\n')
22465 {
22466 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022467 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 {
22469 /*
22470 * Report the invalid expression unless the expression evaluation
22471 * has been cancelled due to an aborting error, an interrupt, or an
22472 * exception.
22473 */
22474 if (!aborting())
22475 EMSG2(_(e_invexpr2), p);
22476 ret = FAIL;
22477 break;
22478 }
22479
22480 if (!eap->skip)
22481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022482 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 len = (int)STRLEN(p);
22484 if (ga_grow(&ga, len + 2) == FAIL)
22485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 ret = FAIL;
22488 break;
22489 }
22490 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 ga.ga_len += len;
22494 }
22495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 arg = skipwhite(arg);
22498 }
22499
22500 if (ret != FAIL && ga.ga_data != NULL)
22501 {
22502 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022505 out_flush();
22506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 else if (eap->cmdidx == CMD_echoerr)
22508 {
22509 /* We don't want to abort following commands, restore did_emsg. */
22510 save_did_emsg = did_emsg;
22511 EMSG((char_u *)ga.ga_data);
22512 if (!force_abort)
22513 did_emsg = save_did_emsg;
22514 }
22515 else if (eap->cmdidx == CMD_execute)
22516 do_cmdline((char_u *)ga.ga_data,
22517 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22518 }
22519
22520 ga_clear(&ga);
22521
22522 if (eap->skip)
22523 --emsg_skip;
22524
22525 eap->nextcmd = check_nextcmd(arg);
22526}
22527
22528/*
22529 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22530 * "arg" points to the "&" or '+' when called, to "option" when returning.
22531 * Returns NULL when no option name found. Otherwise pointer to the char
22532 * after the option name.
22533 */
22534 static char_u *
22535find_option_end(arg, opt_flags)
22536 char_u **arg;
22537 int *opt_flags;
22538{
22539 char_u *p = *arg;
22540
22541 ++p;
22542 if (*p == 'g' && p[1] == ':')
22543 {
22544 *opt_flags = OPT_GLOBAL;
22545 p += 2;
22546 }
22547 else if (*p == 'l' && p[1] == ':')
22548 {
22549 *opt_flags = OPT_LOCAL;
22550 p += 2;
22551 }
22552 else
22553 *opt_flags = 0;
22554
22555 if (!ASCII_ISALPHA(*p))
22556 return NULL;
22557 *arg = p;
22558
22559 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22560 p += 4; /* termcap option */
22561 else
22562 while (ASCII_ISALPHA(*p))
22563 ++p;
22564 return p;
22565}
22566
22567/*
22568 * ":function"
22569 */
22570 void
22571ex_function(eap)
22572 exarg_T *eap;
22573{
22574 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022575 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 int j;
22577 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022579 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 char_u *name = NULL;
22581 char_u *p;
22582 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022583 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 garray_T newargs;
22585 garray_T newlines;
22586 int varargs = FALSE;
22587 int mustend = FALSE;
22588 int flags = 0;
22589 ufunc_T *fp;
22590 int indent;
22591 int nesting;
22592 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022593 dictitem_T *v;
22594 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022595 static int func_nr = 0; /* number for nameless function */
22596 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022597 hashtab_T *ht;
22598 int todo;
22599 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022600 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601
22602 /*
22603 * ":function" without argument: list functions.
22604 */
22605 if (ends_excmd(*eap->arg))
22606 {
22607 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022608 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022609 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022610 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022611 {
22612 if (!HASHITEM_EMPTY(hi))
22613 {
22614 --todo;
22615 fp = HI2UF(hi);
22616 if (!isdigit(*fp->uf_name))
22617 list_func_head(fp, FALSE);
22618 }
22619 }
22620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 eap->nextcmd = check_nextcmd(eap->arg);
22622 return;
22623 }
22624
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022625 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022626 * ":function /pat": list functions matching pattern.
22627 */
22628 if (*eap->arg == '/')
22629 {
22630 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22631 if (!eap->skip)
22632 {
22633 regmatch_T regmatch;
22634
22635 c = *p;
22636 *p = NUL;
22637 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22638 *p = c;
22639 if (regmatch.regprog != NULL)
22640 {
22641 regmatch.rm_ic = p_ic;
22642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022643 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022644 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22645 {
22646 if (!HASHITEM_EMPTY(hi))
22647 {
22648 --todo;
22649 fp = HI2UF(hi);
22650 if (!isdigit(*fp->uf_name)
22651 && vim_regexec(&regmatch, fp->uf_name, 0))
22652 list_func_head(fp, FALSE);
22653 }
22654 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022655 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022656 }
22657 }
22658 if (*p == '/')
22659 ++p;
22660 eap->nextcmd = check_nextcmd(p);
22661 return;
22662 }
22663
22664 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 * Get the function name. There are these situations:
22666 * func normal function name
22667 * "name" == func, "fudi.fd_dict" == NULL
22668 * dict.func new dictionary entry
22669 * "name" == NULL, "fudi.fd_dict" set,
22670 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22671 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022672 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022673 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22674 * dict.func existing dict entry that's not a Funcref
22675 * "name" == NULL, "fudi.fd_dict" set,
22676 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022677 * s:func script-local function name
22678 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022681 name = trans_function_name(&p, eap->skip, 0, &fudi);
22682 paren = (vim_strchr(p, '(') != NULL);
22683 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 {
22685 /*
22686 * Return on an invalid expression in braces, unless the expression
22687 * evaluation has been cancelled due to an aborting error, an
22688 * interrupt, or an exception.
22689 */
22690 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022692 if (!eap->skip && fudi.fd_newkey != NULL)
22693 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022694 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 else
22698 eap->skip = TRUE;
22699 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022700
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 /* An error in a function call during evaluation of an expression in magic
22702 * braces should not cause the function not to be defined. */
22703 saved_did_emsg = did_emsg;
22704 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705
22706 /*
22707 * ":function func" with only function name: list function.
22708 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022709 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 {
22711 if (!ends_excmd(*skipwhite(p)))
22712 {
22713 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022714 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 }
22716 eap->nextcmd = check_nextcmd(p);
22717 if (eap->nextcmd != NULL)
22718 *p = NUL;
22719 if (!eap->skip && !got_int)
22720 {
22721 fp = find_func(name);
22722 if (fp != NULL)
22723 {
22724 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022725 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022727 if (FUNCLINE(fp, j) == NULL)
22728 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 msg_putchar('\n');
22730 msg_outnum((long)(j + 1));
22731 if (j < 9)
22732 msg_putchar(' ');
22733 if (j < 99)
22734 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022735 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 out_flush(); /* show a line at a time */
22737 ui_breakcheck();
22738 }
22739 if (!got_int)
22740 {
22741 msg_putchar('\n');
22742 msg_puts((char_u *)" endfunction");
22743 }
22744 }
22745 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022746 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022748 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 }
22750
22751 /*
22752 * ":function name(arg1, arg2)" Define function.
22753 */
22754 p = skipwhite(p);
22755 if (*p != '(')
22756 {
22757 if (!eap->skip)
22758 {
22759 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022760 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 }
22762 /* attempt to continue by skipping some text */
22763 if (vim_strchr(p, '(') != NULL)
22764 p = vim_strchr(p, '(');
22765 }
22766 p = skipwhite(p + 1);
22767
22768 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22769 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22770
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022771 if (!eap->skip)
22772 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022773 /* Check the name of the function. Unless it's a dictionary function
22774 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022775 if (name != NULL)
22776 arg = name;
22777 else
22778 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022779 if (arg != NULL && (fudi.fd_di == NULL
22780 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022781 {
22782 if (*arg == K_SPECIAL)
22783 j = 3;
22784 else
22785 j = 0;
22786 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22787 : eval_isnamec(arg[j])))
22788 ++j;
22789 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022790 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022791 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022792 /* Disallow using the g: dict. */
22793 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22794 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022795 }
22796
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 /*
22798 * Isolate the arguments: "arg1, arg2, ...)"
22799 */
22800 while (*p != ')')
22801 {
22802 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22803 {
22804 varargs = TRUE;
22805 p += 3;
22806 mustend = TRUE;
22807 }
22808 else
22809 {
22810 arg = p;
22811 while (ASCII_ISALNUM(*p) || *p == '_')
22812 ++p;
22813 if (arg == p || isdigit(*arg)
22814 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22815 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22816 {
22817 if (!eap->skip)
22818 EMSG2(_("E125: Illegal argument: %s"), arg);
22819 break;
22820 }
22821 if (ga_grow(&newargs, 1) == FAIL)
22822 goto erret;
22823 c = *p;
22824 *p = NUL;
22825 arg = vim_strsave(arg);
22826 if (arg == NULL)
22827 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022828
22829 /* Check for duplicate argument name. */
22830 for (i = 0; i < newargs.ga_len; ++i)
22831 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22832 {
22833 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022834 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022835 goto erret;
22836 }
22837
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22839 *p = c;
22840 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 if (*p == ',')
22842 ++p;
22843 else
22844 mustend = TRUE;
22845 }
22846 p = skipwhite(p);
22847 if (mustend && *p != ')')
22848 {
22849 if (!eap->skip)
22850 EMSG2(_(e_invarg2), eap->arg);
22851 break;
22852 }
22853 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022854 if (*p != ')')
22855 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 ++p; /* skip the ')' */
22857
Bram Moolenaare9a41262005-01-15 22:18:47 +000022858 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 for (;;)
22860 {
22861 p = skipwhite(p);
22862 if (STRNCMP(p, "range", 5) == 0)
22863 {
22864 flags |= FC_RANGE;
22865 p += 5;
22866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022867 else if (STRNCMP(p, "dict", 4) == 0)
22868 {
22869 flags |= FC_DICT;
22870 p += 4;
22871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 else if (STRNCMP(p, "abort", 5) == 0)
22873 {
22874 flags |= FC_ABORT;
22875 p += 5;
22876 }
22877 else
22878 break;
22879 }
22880
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022881 /* When there is a line break use what follows for the function body.
22882 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22883 if (*p == '\n')
22884 line_arg = p + 1;
22885 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 EMSG(_(e_trailing));
22887
22888 /*
22889 * Read the body of the function, until ":endfunction" is found.
22890 */
22891 if (KeyTyped)
22892 {
22893 /* Check if the function already exists, don't let the user type the
22894 * whole function before telling him it doesn't work! For a script we
22895 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 if (!eap->skip && !eap->forceit)
22897 {
22898 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22899 EMSG(_(e_funcdict));
22900 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022901 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022904 if (!eap->skip && did_emsg)
22905 goto erret;
22906
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 msg_putchar('\n'); /* don't overwrite the function name */
22908 cmdline_row = msg_row;
22909 }
22910
22911 indent = 2;
22912 nesting = 0;
22913 for (;;)
22914 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022915 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022916 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022917 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022918 saved_wait_return = FALSE;
22919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022921 sourcing_lnum_off = sourcing_lnum;
22922
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022923 if (line_arg != NULL)
22924 {
22925 /* Use eap->arg, split up in parts by line breaks. */
22926 theline = line_arg;
22927 p = vim_strchr(theline, '\n');
22928 if (p == NULL)
22929 line_arg += STRLEN(line_arg);
22930 else
22931 {
22932 *p = NUL;
22933 line_arg = p + 1;
22934 }
22935 }
22936 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 theline = getcmdline(':', 0L, indent);
22938 else
22939 theline = eap->getline(':', eap->cookie, indent);
22940 if (KeyTyped)
22941 lines_left = Rows - 1;
22942 if (theline == NULL)
22943 {
22944 EMSG(_("E126: Missing :endfunction"));
22945 goto erret;
22946 }
22947
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022948 /* Detect line continuation: sourcing_lnum increased more than one. */
22949 if (sourcing_lnum > sourcing_lnum_off + 1)
22950 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22951 else
22952 sourcing_lnum_off = 0;
22953
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 if (skip_until != NULL)
22955 {
22956 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22957 * don't check for ":endfunc". */
22958 if (STRCMP(theline, skip_until) == 0)
22959 {
22960 vim_free(skip_until);
22961 skip_until = NULL;
22962 }
22963 }
22964 else
22965 {
22966 /* skip ':' and blanks*/
22967 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22968 ;
22969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022970 /* Check for "endfunction". */
22971 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022973 if (line_arg == NULL)
22974 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 break;
22976 }
22977
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022978 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 * at "end". */
22980 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22981 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022982 else if (STRNCMP(p, "if", 2) == 0
22983 || STRNCMP(p, "wh", 2) == 0
22984 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 || STRNCMP(p, "try", 3) == 0)
22986 indent += 2;
22987
22988 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022989 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022991 if (*p == '!')
22992 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022994 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22995 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022997 ++nesting;
22998 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 }
23000 }
23001
23002 /* Check for ":append" or ":insert". */
23003 p = skip_range(p, NULL);
23004 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23005 || (p[0] == 'i'
23006 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23007 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23008 skip_until = vim_strsave((char_u *)".");
23009
23010 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23011 arg = skipwhite(skiptowhite(p));
23012 if (arg[0] == '<' && arg[1] =='<'
23013 && ((p[0] == 'p' && p[1] == 'y'
23014 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23015 || (p[0] == 'p' && p[1] == 'e'
23016 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23017 || (p[0] == 't' && p[1] == 'c'
23018 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023019 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23020 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23022 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023023 || (p[0] == 'm' && p[1] == 'z'
23024 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 ))
23026 {
23027 /* ":python <<" continues until a dot, like ":append" */
23028 p = skipwhite(arg + 2);
23029 if (*p == NUL)
23030 skip_until = vim_strsave((char_u *)".");
23031 else
23032 skip_until = vim_strsave(p);
23033 }
23034 }
23035
23036 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023037 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023038 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023039 if (line_arg == NULL)
23040 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023042 }
23043
23044 /* Copy the line to newly allocated memory. get_one_sourceline()
23045 * allocates 250 bytes per line, this saves 80% on average. The cost
23046 * is an extra alloc/free. */
23047 p = vim_strsave(theline);
23048 if (p != NULL)
23049 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023050 if (line_arg == NULL)
23051 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023052 theline = p;
23053 }
23054
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023055 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23056
23057 /* Add NULL lines for continuation lines, so that the line count is
23058 * equal to the index in the growarray. */
23059 while (sourcing_lnum_off-- > 0)
23060 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023061
23062 /* Check for end of eap->arg. */
23063 if (line_arg != NULL && *line_arg == NUL)
23064 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 }
23066
23067 /* Don't define the function when skipping commands or when an error was
23068 * detected. */
23069 if (eap->skip || did_emsg)
23070 goto erret;
23071
23072 /*
23073 * If there are no errors, add the function
23074 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023075 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023076 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023077 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023078 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023079 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023080 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023081 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023082 goto erret;
23083 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023085 fp = find_func(name);
23086 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023088 if (!eap->forceit)
23089 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023090 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023091 goto erret;
23092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023093 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023094 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023095 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023096 name);
23097 goto erret;
23098 }
23099 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023100 ga_clear_strings(&(fp->uf_args));
23101 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023102 vim_free(name);
23103 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 }
23106 else
23107 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023108 char numbuf[20];
23109
23110 fp = NULL;
23111 if (fudi.fd_newkey == NULL && !eap->forceit)
23112 {
23113 EMSG(_(e_funcdict));
23114 goto erret;
23115 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023116 if (fudi.fd_di == NULL)
23117 {
23118 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023119 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023120 goto erret;
23121 }
23122 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023123 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023124 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023125
23126 /* Give the function a sequential number. Can only be used with a
23127 * Funcref! */
23128 vim_free(name);
23129 sprintf(numbuf, "%d", ++func_nr);
23130 name = vim_strsave((char_u *)numbuf);
23131 if (name == NULL)
23132 goto erret;
23133 }
23134
23135 if (fp == NULL)
23136 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023137 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023138 {
23139 int slen, plen;
23140 char_u *scriptname;
23141
23142 /* Check that the autoload name matches the script name. */
23143 j = FAIL;
23144 if (sourcing_name != NULL)
23145 {
23146 scriptname = autoload_name(name);
23147 if (scriptname != NULL)
23148 {
23149 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023150 plen = (int)STRLEN(p);
23151 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023152 if (slen > plen && fnamecmp(p,
23153 sourcing_name + slen - plen) == 0)
23154 j = OK;
23155 vim_free(scriptname);
23156 }
23157 }
23158 if (j == FAIL)
23159 {
23160 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23161 goto erret;
23162 }
23163 }
23164
23165 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 if (fp == NULL)
23167 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023168
23169 if (fudi.fd_dict != NULL)
23170 {
23171 if (fudi.fd_di == NULL)
23172 {
23173 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023174 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175 if (fudi.fd_di == NULL)
23176 {
23177 vim_free(fp);
23178 goto erret;
23179 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023180 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23181 {
23182 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023183 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023184 goto erret;
23185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 }
23187 else
23188 /* overwrite existing dict entry */
23189 clear_tv(&fudi.fd_di->di_tv);
23190 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023191 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023192 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023193 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023194
23195 /* behave like "dict" was used */
23196 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023197 }
23198
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023201 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23202 {
23203 vim_free(fp);
23204 goto erret;
23205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023207 fp->uf_args = newargs;
23208 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023209#ifdef FEAT_PROFILE
23210 fp->uf_tml_count = NULL;
23211 fp->uf_tml_total = NULL;
23212 fp->uf_tml_self = NULL;
23213 fp->uf_profiling = FALSE;
23214 if (prof_def_func())
23215 func_do_profile(fp);
23216#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023217 fp->uf_varargs = varargs;
23218 fp->uf_flags = flags;
23219 fp->uf_calls = 0;
23220 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023221 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
23223erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 ga_clear_strings(&newargs);
23225 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023226ret_free:
23227 vim_free(skip_until);
23228 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023231 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232}
23233
23234/*
23235 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023236 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023238 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023239 * TFN_INT: internal function name OK
23240 * TFN_QUIET: be quiet
23241 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 * Advances "pp" to just after the function name (if no error).
23243 */
23244 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023245trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 char_u **pp;
23247 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023248 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023251 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 char_u *start;
23253 char_u *end;
23254 int lead;
23255 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023258
23259 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023260 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023262
23263 /* Check for hard coded <SNR>: already translated function ID (from a user
23264 * command). */
23265 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23266 && (*pp)[2] == (int)KE_SNR)
23267 {
23268 *pp += 3;
23269 len = get_id_len(pp) + 3;
23270 return vim_strnsave(start, len);
23271 }
23272
23273 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23274 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023276 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023278
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023279 /* Note that TFN_ flags use the same values as GLV_ flags. */
23280 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023281 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023282 if (end == start)
23283 {
23284 if (!skip)
23285 EMSG(_("E129: Function name required"));
23286 goto theend;
23287 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023288 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023289 {
23290 /*
23291 * Report an invalid expression in braces, unless the expression
23292 * evaluation has been cancelled due to an aborting error, an
23293 * interrupt, or an exception.
23294 */
23295 if (!aborting())
23296 {
23297 if (end != NULL)
23298 EMSG2(_(e_invarg2), start);
23299 }
23300 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023301 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023302 goto theend;
23303 }
23304
23305 if (lv.ll_tv != NULL)
23306 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023307 if (fdp != NULL)
23308 {
23309 fdp->fd_dict = lv.ll_dict;
23310 fdp->fd_newkey = lv.ll_newkey;
23311 lv.ll_newkey = NULL;
23312 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023313 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023314 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23315 {
23316 name = vim_strsave(lv.ll_tv->vval.v_string);
23317 *pp = end;
23318 }
23319 else
23320 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023321 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23322 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023323 EMSG(_(e_funcref));
23324 else
23325 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023326 name = NULL;
23327 }
23328 goto theend;
23329 }
23330
23331 if (lv.ll_name == NULL)
23332 {
23333 /* Error found, but continue after the function name. */
23334 *pp = end;
23335 goto theend;
23336 }
23337
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023338 /* Check if the name is a Funcref. If so, use the value. */
23339 if (lv.ll_exp_name != NULL)
23340 {
23341 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023342 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023343 if (name == lv.ll_exp_name)
23344 name = NULL;
23345 }
23346 else
23347 {
23348 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023349 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023350 if (name == *pp)
23351 name = NULL;
23352 }
23353 if (name != NULL)
23354 {
23355 name = vim_strsave(name);
23356 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023357 if (STRNCMP(name, "<SNR>", 5) == 0)
23358 {
23359 /* Change "<SNR>" to the byte sequence. */
23360 name[0] = K_SPECIAL;
23361 name[1] = KS_EXTRA;
23362 name[2] = (int)KE_SNR;
23363 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23364 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023365 goto theend;
23366 }
23367
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023368 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023369 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023370 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023371 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23372 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23373 {
23374 /* When there was "s:" already or the name expanded to get a
23375 * leading "s:" then remove it. */
23376 lv.ll_name += 2;
23377 len -= 2;
23378 lead = 2;
23379 }
23380 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023381 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023382 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023383 /* skip over "s:" and "g:" */
23384 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023385 lv.ll_name += 2;
23386 len = (int)(end - lv.ll_name);
23387 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023388
23389 /*
23390 * Copy the function name to allocated memory.
23391 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23392 * Accept <SNR>123_name() outside a script.
23393 */
23394 if (skip)
23395 lead = 0; /* do nothing */
23396 else if (lead > 0)
23397 {
23398 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023399 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23400 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023401 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023402 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023403 if (current_SID <= 0)
23404 {
23405 EMSG(_(e_usingsid));
23406 goto theend;
23407 }
23408 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23409 lead += (int)STRLEN(sid_buf);
23410 }
23411 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023412 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023413 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023414 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023415 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023416 goto theend;
23417 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023418 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023419 {
23420 char_u *cp = vim_strchr(lv.ll_name, ':');
23421
23422 if (cp != NULL && cp < end)
23423 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023424 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023425 goto theend;
23426 }
23427 }
23428
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023429 name = alloc((unsigned)(len + lead + 1));
23430 if (name != NULL)
23431 {
23432 if (lead > 0)
23433 {
23434 name[0] = K_SPECIAL;
23435 name[1] = KS_EXTRA;
23436 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023437 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023438 STRCPY(name + 3, sid_buf);
23439 }
23440 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023441 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023442 }
23443 *pp = end;
23444
23445theend:
23446 clear_lval(&lv);
23447 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448}
23449
23450/*
23451 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23452 * Return 2 if "p" starts with "s:".
23453 * Return 0 otherwise.
23454 */
23455 static int
23456eval_fname_script(p)
23457 char_u *p;
23458{
23459 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23460 || STRNICMP(p + 1, "SNR>", 4) == 0))
23461 return 5;
23462 if (p[0] == 's' && p[1] == ':')
23463 return 2;
23464 return 0;
23465}
23466
23467/*
23468 * Return TRUE if "p" starts with "<SID>" or "s:".
23469 * Only works if eval_fname_script() returned non-zero for "p"!
23470 */
23471 static int
23472eval_fname_sid(p)
23473 char_u *p;
23474{
23475 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23476}
23477
23478/*
23479 * List the head of the function: "name(arg1, arg2)".
23480 */
23481 static void
23482list_func_head(fp, indent)
23483 ufunc_T *fp;
23484 int indent;
23485{
23486 int j;
23487
23488 msg_start();
23489 if (indent)
23490 MSG_PUTS(" ");
23491 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023492 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 {
23494 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023495 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 }
23497 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023498 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023500 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 {
23502 if (j)
23503 MSG_PUTS(", ");
23504 msg_puts(FUNCARG(fp, j));
23505 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023506 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 {
23508 if (j)
23509 MSG_PUTS(", ");
23510 MSG_PUTS("...");
23511 }
23512 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023513 if (fp->uf_flags & FC_ABORT)
23514 MSG_PUTS(" abort");
23515 if (fp->uf_flags & FC_RANGE)
23516 MSG_PUTS(" range");
23517 if (fp->uf_flags & FC_DICT)
23518 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023519 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023520 if (p_verbose > 0)
23521 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522}
23523
23524/*
23525 * Find a function by name, return pointer to it in ufuncs.
23526 * Return NULL for unknown function.
23527 */
23528 static ufunc_T *
23529find_func(name)
23530 char_u *name;
23531{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023532 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023534 hi = hash_find(&func_hashtab, name);
23535 if (!HASHITEM_EMPTY(hi))
23536 return HI2UF(hi);
23537 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538}
23539
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023540#if defined(EXITFREE) || defined(PROTO)
23541 void
23542free_all_functions()
23543{
23544 hashitem_T *hi;
23545
23546 /* Need to start all over every time, because func_free() may change the
23547 * hash table. */
23548 while (func_hashtab.ht_used > 0)
23549 for (hi = func_hashtab.ht_array; ; ++hi)
23550 if (!HASHITEM_EMPTY(hi))
23551 {
23552 func_free(HI2UF(hi));
23553 break;
23554 }
23555}
23556#endif
23557
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023558 int
23559translated_function_exists(name)
23560 char_u *name;
23561{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023562 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023563 return find_internal_func(name) >= 0;
23564 return find_func(name) != NULL;
23565}
23566
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023567/*
23568 * Return TRUE if a function "name" exists.
23569 */
23570 static int
23571function_exists(name)
23572 char_u *name;
23573{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023574 char_u *nm = name;
23575 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023576 int n = FALSE;
23577
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023578 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23579 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023580 nm = skipwhite(nm);
23581
23582 /* Only accept "funcname", "funcname ", "funcname (..." and
23583 * "funcname(...", not "funcname!...". */
23584 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023585 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023586 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023587 return n;
23588}
23589
Bram Moolenaara1544c02013-05-30 12:35:52 +020023590 char_u *
23591get_expanded_name(name, check)
23592 char_u *name;
23593 int check;
23594{
23595 char_u *nm = name;
23596 char_u *p;
23597
23598 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23599
23600 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023601 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023602 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023603
Bram Moolenaara1544c02013-05-30 12:35:52 +020023604 vim_free(p);
23605 return NULL;
23606}
23607
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023608/*
23609 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023610 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23611 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023612 */
23613 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023614builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023615 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023616 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023617{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023618 char_u *p;
23619
23620 if (!ASCII_ISLOWER(name[0]))
23621 return FALSE;
23622 p = vim_strchr(name, AUTOLOAD_CHAR);
23623 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023624}
23625
Bram Moolenaar05159a02005-02-26 23:04:13 +000023626#if defined(FEAT_PROFILE) || defined(PROTO)
23627/*
23628 * Start profiling function "fp".
23629 */
23630 static void
23631func_do_profile(fp)
23632 ufunc_T *fp;
23633{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023634 int len = fp->uf_lines.ga_len;
23635
23636 if (len == 0)
23637 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023638 fp->uf_tm_count = 0;
23639 profile_zero(&fp->uf_tm_self);
23640 profile_zero(&fp->uf_tm_total);
23641 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023642 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023643 if (fp->uf_tml_total == NULL)
23644 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023645 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023646 if (fp->uf_tml_self == NULL)
23647 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023648 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023649 fp->uf_tml_idx = -1;
23650 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23651 || fp->uf_tml_self == NULL)
23652 return; /* out of memory */
23653
23654 fp->uf_profiling = TRUE;
23655}
23656
23657/*
23658 * Dump the profiling results for all functions in file "fd".
23659 */
23660 void
23661func_dump_profile(fd)
23662 FILE *fd;
23663{
23664 hashitem_T *hi;
23665 int todo;
23666 ufunc_T *fp;
23667 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023668 ufunc_T **sorttab;
23669 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023671 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023672 if (todo == 0)
23673 return; /* nothing to dump */
23674
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023675 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023676
Bram Moolenaar05159a02005-02-26 23:04:13 +000023677 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23678 {
23679 if (!HASHITEM_EMPTY(hi))
23680 {
23681 --todo;
23682 fp = HI2UF(hi);
23683 if (fp->uf_profiling)
23684 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023685 if (sorttab != NULL)
23686 sorttab[st_len++] = fp;
23687
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688 if (fp->uf_name[0] == K_SPECIAL)
23689 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23690 else
23691 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23692 if (fp->uf_tm_count == 1)
23693 fprintf(fd, "Called 1 time\n");
23694 else
23695 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23696 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23697 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23698 fprintf(fd, "\n");
23699 fprintf(fd, "count total (s) self (s)\n");
23700
23701 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23702 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023703 if (FUNCLINE(fp, i) == NULL)
23704 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023705 prof_func_line(fd, fp->uf_tml_count[i],
23706 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023707 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23708 }
23709 fprintf(fd, "\n");
23710 }
23711 }
23712 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023713
23714 if (sorttab != NULL && st_len > 0)
23715 {
23716 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23717 prof_total_cmp);
23718 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23719 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23720 prof_self_cmp);
23721 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23722 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023723
23724 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023725}
Bram Moolenaar73830342005-02-28 22:48:19 +000023726
23727 static void
23728prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23729 FILE *fd;
23730 ufunc_T **sorttab;
23731 int st_len;
23732 char *title;
23733 int prefer_self; /* when equal print only self time */
23734{
23735 int i;
23736 ufunc_T *fp;
23737
23738 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23739 fprintf(fd, "count total (s) self (s) function\n");
23740 for (i = 0; i < 20 && i < st_len; ++i)
23741 {
23742 fp = sorttab[i];
23743 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23744 prefer_self);
23745 if (fp->uf_name[0] == K_SPECIAL)
23746 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23747 else
23748 fprintf(fd, " %s()\n", fp->uf_name);
23749 }
23750 fprintf(fd, "\n");
23751}
23752
23753/*
23754 * Print the count and times for one function or function line.
23755 */
23756 static void
23757prof_func_line(fd, count, total, self, prefer_self)
23758 FILE *fd;
23759 int count;
23760 proftime_T *total;
23761 proftime_T *self;
23762 int prefer_self; /* when equal print only self time */
23763{
23764 if (count > 0)
23765 {
23766 fprintf(fd, "%5d ", count);
23767 if (prefer_self && profile_equal(total, self))
23768 fprintf(fd, " ");
23769 else
23770 fprintf(fd, "%s ", profile_msg(total));
23771 if (!prefer_self && profile_equal(total, self))
23772 fprintf(fd, " ");
23773 else
23774 fprintf(fd, "%s ", profile_msg(self));
23775 }
23776 else
23777 fprintf(fd, " ");
23778}
23779
23780/*
23781 * Compare function for total time sorting.
23782 */
23783 static int
23784#ifdef __BORLANDC__
23785_RTLENTRYF
23786#endif
23787prof_total_cmp(s1, s2)
23788 const void *s1;
23789 const void *s2;
23790{
23791 ufunc_T *p1, *p2;
23792
23793 p1 = *(ufunc_T **)s1;
23794 p2 = *(ufunc_T **)s2;
23795 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23796}
23797
23798/*
23799 * Compare function for self time sorting.
23800 */
23801 static int
23802#ifdef __BORLANDC__
23803_RTLENTRYF
23804#endif
23805prof_self_cmp(s1, s2)
23806 const void *s1;
23807 const void *s2;
23808{
23809 ufunc_T *p1, *p2;
23810
23811 p1 = *(ufunc_T **)s1;
23812 p2 = *(ufunc_T **)s2;
23813 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23814}
23815
Bram Moolenaar05159a02005-02-26 23:04:13 +000023816#endif
23817
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023818/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023819 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023820 * Return TRUE if a package was loaded.
23821 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023822 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023823script_autoload(name, reload)
23824 char_u *name;
23825 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023826{
23827 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023828 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023829 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023830 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023831
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023832 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023833 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023834 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023835 return FALSE;
23836
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023837 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023838
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023839 /* Find the name in the list of previously loaded package names. Skip
23840 * "autoload/", it's always the same. */
23841 for (i = 0; i < ga_loaded.ga_len; ++i)
23842 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23843 break;
23844 if (!reload && i < ga_loaded.ga_len)
23845 ret = FALSE; /* was loaded already */
23846 else
23847 {
23848 /* Remember the name if it wasn't loaded already. */
23849 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23850 {
23851 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23852 tofree = NULL;
23853 }
23854
23855 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023856 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023857 ret = TRUE;
23858 }
23859
23860 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023861 return ret;
23862}
23863
23864/*
23865 * Return the autoload script name for a function or variable name.
23866 * Returns NULL when out of memory.
23867 */
23868 static char_u *
23869autoload_name(name)
23870 char_u *name;
23871{
23872 char_u *p;
23873 char_u *scriptname;
23874
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023875 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023876 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23877 if (scriptname == NULL)
23878 return FALSE;
23879 STRCPY(scriptname, "autoload/");
23880 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023881 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023882 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023883 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023884 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023886}
23887
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23889
23890/*
23891 * Function given to ExpandGeneric() to obtain the list of user defined
23892 * function names.
23893 */
23894 char_u *
23895get_user_func_name(xp, idx)
23896 expand_T *xp;
23897 int idx;
23898{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023899 static long_u done;
23900 static hashitem_T *hi;
23901 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902
23903 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905 done = 0;
23906 hi = func_hashtab.ht_array;
23907 }
23908 if (done < func_hashtab.ht_used)
23909 {
23910 if (done++ > 0)
23911 ++hi;
23912 while (HASHITEM_EMPTY(hi))
23913 ++hi;
23914 fp = HI2UF(hi);
23915
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023916 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023917 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023918
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023919 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23920 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921
23922 cat_func_name(IObuff, fp);
23923 if (xp->xp_context != EXPAND_USER_FUNC)
23924 {
23925 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023926 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 STRCAT(IObuff, ")");
23928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 return IObuff;
23930 }
23931 return NULL;
23932}
23933
23934#endif /* FEAT_CMDL_COMPL */
23935
23936/*
23937 * Copy the function name of "fp" to buffer "buf".
23938 * "buf" must be able to hold the function name plus three bytes.
23939 * Takes care of script-local function names.
23940 */
23941 static void
23942cat_func_name(buf, fp)
23943 char_u *buf;
23944 ufunc_T *fp;
23945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023946 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 {
23948 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 }
23951 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023952 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953}
23954
23955/*
23956 * ":delfunction {name}"
23957 */
23958 void
23959ex_delfunction(eap)
23960 exarg_T *eap;
23961{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023962 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 char_u *p;
23964 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023965 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966
23967 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023968 name = trans_function_name(&p, eap->skip, 0, &fudi);
23969 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023971 {
23972 if (fudi.fd_dict != NULL && !eap->skip)
23973 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 if (!ends_excmd(*skipwhite(p)))
23977 {
23978 vim_free(name);
23979 EMSG(_(e_trailing));
23980 return;
23981 }
23982 eap->nextcmd = check_nextcmd(p);
23983 if (eap->nextcmd != NULL)
23984 *p = NUL;
23985
23986 if (!eap->skip)
23987 fp = find_func(name);
23988 vim_free(name);
23989
23990 if (!eap->skip)
23991 {
23992 if (fp == NULL)
23993 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023994 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 return;
23996 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023997 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 {
23999 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24000 return;
24001 }
24002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024005 /* Delete the dict item that refers to the function, it will
24006 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024007 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024009 else
24010 func_free(fp);
24011 }
24012}
24013
24014/*
24015 * Free a function and remove it from the list of functions.
24016 */
24017 static void
24018func_free(fp)
24019 ufunc_T *fp;
24020{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024021 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024022
24023 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024024 ga_clear_strings(&(fp->uf_args));
24025 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024026#ifdef FEAT_PROFILE
24027 vim_free(fp->uf_tml_count);
24028 vim_free(fp->uf_tml_total);
24029 vim_free(fp->uf_tml_self);
24030#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024031
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024032 /* remove the function from the function hashtable */
24033 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24034 if (HASHITEM_EMPTY(hi))
24035 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024036 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024037 hash_remove(&func_hashtab, hi);
24038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024039 vim_free(fp);
24040}
24041
24042/*
24043 * Unreference a Function: decrement the reference count and free it when it
24044 * becomes zero. Only for numbered functions.
24045 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024046 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024047func_unref(name)
24048 char_u *name;
24049{
24050 ufunc_T *fp;
24051
24052 if (name != NULL && isdigit(*name))
24053 {
24054 fp = find_func(name);
24055 if (fp == NULL)
24056 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024057 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 {
24059 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024060 * when "uf_calls" becomes zero. */
24061 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 func_free(fp);
24063 }
24064 }
24065}
24066
24067/*
24068 * Count a reference to a Function.
24069 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024070 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024071func_ref(name)
24072 char_u *name;
24073{
24074 ufunc_T *fp;
24075
24076 if (name != NULL && isdigit(*name))
24077 {
24078 fp = find_func(name);
24079 if (fp == NULL)
24080 EMSG2(_(e_intern2), "func_ref()");
24081 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024082 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 }
24084}
24085
24086/*
24087 * Call a user function.
24088 */
24089 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024090call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 ufunc_T *fp; /* pointer to function */
24092 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 typval_T *argvars; /* arguments */
24094 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 linenr_T firstline; /* first line of range */
24096 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024097 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098{
Bram Moolenaar33570922005-01-25 22:26:29 +000024099 char_u *save_sourcing_name;
24100 linenr_T save_sourcing_lnum;
24101 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024102 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024103 int save_did_emsg;
24104 static int depth = 0;
24105 dictitem_T *v;
24106 int fixvar_idx = 0; /* index in fixvar[] */
24107 int i;
24108 int ai;
24109 char_u numbuf[NUMBUFLEN];
24110 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024111 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024112#ifdef FEAT_PROFILE
24113 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024114 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116
24117 /* If depth of calling is getting too high, don't execute the function */
24118 if (depth >= p_mfd)
24119 {
24120 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024121 rettv->v_type = VAR_NUMBER;
24122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 return;
24124 }
24125 ++depth;
24126
24127 line_breakcheck(); /* check for CTRL-C hit */
24128
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024129 fc = (funccall_T *)alloc(sizeof(funccall_T));
24130 fc->caller = current_funccal;
24131 current_funccal = fc;
24132 fc->func = fp;
24133 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024134 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024135 fc->linenr = 0;
24136 fc->returned = FALSE;
24137 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024139 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24140 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141
Bram Moolenaar33570922005-01-25 22:26:29 +000024142 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024143 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024144 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24145 * each argument variable and saves a lot of time.
24146 */
24147 /*
24148 * Init l: variables.
24149 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024150 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024151 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024152 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024153 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24154 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024155 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024156 name = v->di_key;
24157 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024158 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024159 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024160 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024161 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024162 v->di_tv.vval.v_dict = selfdict;
24163 ++selfdict->dv_refcount;
24164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024165
Bram Moolenaar33570922005-01-25 22:26:29 +000024166 /*
24167 * Init a: variables.
24168 * Set a:0 to "argcount".
24169 * Set a:000 to a list with room for the "..." arguments.
24170 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024171 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024172 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024173 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024174 /* Use "name" to avoid a warning from some compiler that checks the
24175 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024176 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024177 name = v->di_key;
24178 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024180 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024181 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024182 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024183 v->di_tv.vval.v_list = &fc->l_varlist;
24184 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24185 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24186 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024187
24188 /*
24189 * Set a:firstline to "firstline" and a:lastline to "lastline".
24190 * Set a:name to named arguments.
24191 * Set a:N to the "..." arguments.
24192 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024193 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024194 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024195 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024196 (varnumber_T)lastline);
24197 for (i = 0; i < argcount; ++i)
24198 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024199 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 if (ai < 0)
24201 /* named argument a:name */
24202 name = FUNCARG(fp, i);
24203 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024204 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024205 /* "..." argument a:1, a:2, etc. */
24206 sprintf((char *)numbuf, "%d", ai + 1);
24207 name = numbuf;
24208 }
24209 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24210 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024211 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024212 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24213 }
24214 else
24215 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024216 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24217 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024218 if (v == NULL)
24219 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024220 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024221 }
24222 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024223 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024224
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024225 /* Note: the values are copied directly to avoid alloc/free.
24226 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024227 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024228 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024229
24230 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24231 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024232 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24233 fc->l_listitems[ai].li_tv = argvars[i];
24234 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024235 }
24236 }
24237
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 /* Don't redraw while executing the function. */
24239 ++RedrawingDisabled;
24240 save_sourcing_name = sourcing_name;
24241 save_sourcing_lnum = sourcing_lnum;
24242 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024243 /* need space for function name + ("function " + 3) or "[number]" */
24244 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24245 + STRLEN(fp->uf_name) + 20;
24246 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 if (sourcing_name != NULL)
24248 {
24249 if (save_sourcing_name != NULL
24250 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024251 sprintf((char *)sourcing_name, "%s[%d]..",
24252 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 else
24254 STRCPY(sourcing_name, "function ");
24255 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24256
24257 if (p_verbose >= 12)
24258 {
24259 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024260 verbose_enter_scroll();
24261
Bram Moolenaar555b2802005-05-19 21:08:39 +000024262 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 if (p_verbose >= 14)
24264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024266 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024267 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024268 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269
24270 msg_puts((char_u *)"(");
24271 for (i = 0; i < argcount; ++i)
24272 {
24273 if (i > 0)
24274 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024275 if (argvars[i].v_type == VAR_NUMBER)
24276 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 else
24278 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024279 /* Do not want errors such as E724 here. */
24280 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024281 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024282 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024283 if (s != NULL)
24284 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024285 if (vim_strsize(s) > MSG_BUF_CLEN)
24286 {
24287 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24288 s = buf;
24289 }
24290 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024291 vim_free(tofree);
24292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 }
24294 }
24295 msg_puts((char_u *)")");
24296 }
24297 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024298
24299 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 --no_wait_return;
24301 }
24302 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024304 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024305 {
24306 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24307 func_do_profile(fp);
24308 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024309 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024310 {
24311 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024312 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024313 profile_zero(&fp->uf_tm_children);
24314 }
24315 script_prof_save(&wait_start);
24316 }
24317#endif
24318
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 save_did_emsg = did_emsg;
24322 did_emsg = FALSE;
24323
24324 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024325 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24327
24328 --RedrawingDisabled;
24329
24330 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024331 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024333 clear_tv(rettv);
24334 rettv->v_type = VAR_NUMBER;
24335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 }
24337
Bram Moolenaar05159a02005-02-26 23:04:13 +000024338#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024339 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024340 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024341 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024342 profile_end(&call_start);
24343 profile_sub_wait(&wait_start, &call_start);
24344 profile_add(&fp->uf_tm_total, &call_start);
24345 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024346 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024347 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024348 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24349 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024350 }
24351 }
24352#endif
24353
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 /* when being verbose, mention the return value */
24355 if (p_verbose >= 12)
24356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024358 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024361 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024362 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024363 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024364 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024367 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024368 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024369 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024370 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024371
Bram Moolenaar555b2802005-05-19 21:08:39 +000024372 /* The value may be very long. Skip the middle part, so that we
24373 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024374 * truncate it at the end. Don't want errors such as E724 here. */
24375 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024376 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024377 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024378 if (s != NULL)
24379 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024380 if (vim_strsize(s) > MSG_BUF_CLEN)
24381 {
24382 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24383 s = buf;
24384 }
24385 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024386 vim_free(tofree);
24387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 }
24389 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024390
24391 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 --no_wait_return;
24393 }
24394
24395 vim_free(sourcing_name);
24396 sourcing_name = save_sourcing_name;
24397 sourcing_lnum = save_sourcing_lnum;
24398 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024399#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024400 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024401 script_prof_restore(&wait_start);
24402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403
24404 if (p_verbose >= 12 && sourcing_name != NULL)
24405 {
24406 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024407 verbose_enter_scroll();
24408
Bram Moolenaar555b2802005-05-19 21:08:39 +000024409 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024411
24412 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 --no_wait_return;
24414 }
24415
24416 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024417 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024419
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024420 /* If the a:000 list and the l: and a: dicts are not referenced we can
24421 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024422 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24423 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24424 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24425 {
24426 free_funccal(fc, FALSE);
24427 }
24428 else
24429 {
24430 hashitem_T *hi;
24431 listitem_T *li;
24432 int todo;
24433
24434 /* "fc" is still in use. This can happen when returning "a:000" or
24435 * assigning "l:" to a global variable.
24436 * Link "fc" in the list for garbage collection later. */
24437 fc->caller = previous_funccal;
24438 previous_funccal = fc;
24439
24440 /* Make a copy of the a: variables, since we didn't do that above. */
24441 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24442 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24443 {
24444 if (!HASHITEM_EMPTY(hi))
24445 {
24446 --todo;
24447 v = HI2DI(hi);
24448 copy_tv(&v->di_tv, &v->di_tv);
24449 }
24450 }
24451
24452 /* Make a copy of the a:000 items, since we didn't do that above. */
24453 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24454 copy_tv(&li->li_tv, &li->li_tv);
24455 }
24456}
24457
24458/*
24459 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024460 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024461 */
24462 static int
24463can_free_funccal(fc, copyID)
24464 funccall_T *fc;
24465 int copyID;
24466{
24467 return (fc->l_varlist.lv_copyID != copyID
24468 && fc->l_vars.dv_copyID != copyID
24469 && fc->l_avars.dv_copyID != copyID);
24470}
24471
24472/*
24473 * Free "fc" and what it contains.
24474 */
24475 static void
24476free_funccal(fc, free_val)
24477 funccall_T *fc;
24478 int free_val; /* a: vars were allocated */
24479{
24480 listitem_T *li;
24481
24482 /* The a: variables typevals may not have been allocated, only free the
24483 * allocated variables. */
24484 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24485
24486 /* free all l: variables */
24487 vars_clear(&fc->l_vars.dv_hashtab);
24488
24489 /* Free the a:000 variables if they were allocated. */
24490 if (free_val)
24491 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24492 clear_tv(&li->li_tv);
24493
24494 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495}
24496
24497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024498 * Add a number variable "name" to dict "dp" with value "nr".
24499 */
24500 static void
24501add_nr_var(dp, v, name, nr)
24502 dict_T *dp;
24503 dictitem_T *v;
24504 char *name;
24505 varnumber_T nr;
24506{
24507 STRCPY(v->di_key, name);
24508 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24509 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24510 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024511 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024512 v->di_tv.vval.v_number = nr;
24513}
24514
24515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 * ":return [expr]"
24517 */
24518 void
24519ex_return(eap)
24520 exarg_T *eap;
24521{
24522 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 int returning = FALSE;
24525
24526 if (current_funccal == NULL)
24527 {
24528 EMSG(_("E133: :return not inside a function"));
24529 return;
24530 }
24531
24532 if (eap->skip)
24533 ++emsg_skip;
24534
24535 eap->nextcmd = NULL;
24536 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024537 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 {
24539 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024540 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543 }
24544 /* It's safer to return also on error. */
24545 else if (!eap->skip)
24546 {
24547 /*
24548 * Return unless the expression evaluation has been cancelled due to an
24549 * aborting error, an interrupt, or an exception.
24550 */
24551 if (!aborting())
24552 returning = do_return(eap, FALSE, TRUE, NULL);
24553 }
24554
24555 /* When skipping or the return gets pending, advance to the next command
24556 * in this line (!returning). Otherwise, ignore the rest of the line.
24557 * Following lines will be ignored by get_func_line(). */
24558 if (returning)
24559 eap->nextcmd = NULL;
24560 else if (eap->nextcmd == NULL) /* no argument */
24561 eap->nextcmd = check_nextcmd(arg);
24562
24563 if (eap->skip)
24564 --emsg_skip;
24565}
24566
24567/*
24568 * Return from a function. Possibly makes the return pending. Also called
24569 * for a pending return at the ":endtry" or after returning from an extra
24570 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024571 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 * FALSE when the return gets pending.
24574 */
24575 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024576do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577 exarg_T *eap;
24578 int reanimate;
24579 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024580 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581{
24582 int idx;
24583 struct condstack *cstack = eap->cstack;
24584
24585 if (reanimate)
24586 /* Undo the return. */
24587 current_funccal->returned = FALSE;
24588
24589 /*
24590 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24591 * not in its finally clause (which then is to be executed next) is found.
24592 * In this case, make the ":return" pending for execution at the ":endtry".
24593 * Otherwise, return normally.
24594 */
24595 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24596 if (idx >= 0)
24597 {
24598 cstack->cs_pending[idx] = CSTP_RETURN;
24599
24600 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024601 /* A pending return again gets pending. "rettv" points to an
24602 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024604 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 else
24606 {
24607 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024610 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024612 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 {
24614 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024615 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024616 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 else
24618 EMSG(_(e_outofmem));
24619 }
24620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024621 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622
24623 if (reanimate)
24624 {
24625 /* The pending return value could be overwritten by a ":return"
24626 * without argument in a finally clause; reset the default
24627 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024628 current_funccal->rettv->v_type = VAR_NUMBER;
24629 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 }
24631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 }
24634 else
24635 {
24636 current_funccal->returned = TRUE;
24637
24638 /* If the return is carried out now, store the return value. For
24639 * a return immediately after reanimation, the value is already
24640 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024641 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024643 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024644 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024646 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 }
24648 }
24649
24650 return idx < 0;
24651}
24652
24653/*
24654 * Free the variable with a pending return value.
24655 */
24656 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024657discard_pending_return(rettv)
24658 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659{
Bram Moolenaar33570922005-01-25 22:26:29 +000024660 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661}
24662
24663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024664 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 * is an allocated string. Used by report_pending() for verbose messages.
24666 */
24667 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024668get_return_cmd(rettv)
24669 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024671 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024672 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024673 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024675 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024676 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024677 if (s == NULL)
24678 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024679
24680 STRCPY(IObuff, ":return ");
24681 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24682 if (STRLEN(s) + 8 >= IOSIZE)
24683 STRCPY(IObuff + IOSIZE - 4, "...");
24684 vim_free(tofree);
24685 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686}
24687
24688/*
24689 * Get next function line.
24690 * Called by do_cmdline() to get the next line.
24691 * Returns allocated string, or NULL for end of function.
24692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 char_u *
24694get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024695 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024697 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698{
Bram Moolenaar33570922005-01-25 22:26:29 +000024699 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024700 ufunc_T *fp = fcp->func;
24701 char_u *retval;
24702 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703
24704 /* If breakpoints have been added/deleted need to check for it. */
24705 if (fcp->dbg_tick != debug_tick)
24706 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 sourcing_lnum);
24709 fcp->dbg_tick = debug_tick;
24710 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024711#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024712 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024713 func_line_end(cookie);
24714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715
Bram Moolenaar05159a02005-02-26 23:04:13 +000024716 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024717 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24718 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 retval = NULL;
24720 else
24721 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024722 /* Skip NULL lines (continuation lines). */
24723 while (fcp->linenr < gap->ga_len
24724 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24725 ++fcp->linenr;
24726 if (fcp->linenr >= gap->ga_len)
24727 retval = NULL;
24728 else
24729 {
24730 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24731 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024733 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024734 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024735#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 }
24738
24739 /* Did we encounter a breakpoint? */
24740 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24741 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024742 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 sourcing_lnum);
24746 fcp->dbg_tick = debug_tick;
24747 }
24748
24749 return retval;
24750}
24751
Bram Moolenaar05159a02005-02-26 23:04:13 +000024752#if defined(FEAT_PROFILE) || defined(PROTO)
24753/*
24754 * Called when starting to read a function line.
24755 * "sourcing_lnum" must be correct!
24756 * When skipping lines it may not actually be executed, but we won't find out
24757 * until later and we need to store the time now.
24758 */
24759 void
24760func_line_start(cookie)
24761 void *cookie;
24762{
24763 funccall_T *fcp = (funccall_T *)cookie;
24764 ufunc_T *fp = fcp->func;
24765
24766 if (fp->uf_profiling && sourcing_lnum >= 1
24767 && sourcing_lnum <= fp->uf_lines.ga_len)
24768 {
24769 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024770 /* Skip continuation lines. */
24771 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24772 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773 fp->uf_tml_execed = FALSE;
24774 profile_start(&fp->uf_tml_start);
24775 profile_zero(&fp->uf_tml_children);
24776 profile_get_wait(&fp->uf_tml_wait);
24777 }
24778}
24779
24780/*
24781 * Called when actually executing a function line.
24782 */
24783 void
24784func_line_exec(cookie)
24785 void *cookie;
24786{
24787 funccall_T *fcp = (funccall_T *)cookie;
24788 ufunc_T *fp = fcp->func;
24789
24790 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24791 fp->uf_tml_execed = TRUE;
24792}
24793
24794/*
24795 * Called when done with a function line.
24796 */
24797 void
24798func_line_end(cookie)
24799 void *cookie;
24800{
24801 funccall_T *fcp = (funccall_T *)cookie;
24802 ufunc_T *fp = fcp->func;
24803
24804 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24805 {
24806 if (fp->uf_tml_execed)
24807 {
24808 ++fp->uf_tml_count[fp->uf_tml_idx];
24809 profile_end(&fp->uf_tml_start);
24810 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024811 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024812 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24813 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024814 }
24815 fp->uf_tml_idx = -1;
24816 }
24817}
24818#endif
24819
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820/*
24821 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024822 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 */
24824 int
24825func_has_ended(cookie)
24826 void *cookie;
24827{
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829
24830 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24831 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024832 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 || fcp->returned);
24834}
24835
24836/*
24837 * return TRUE if cookie indicates a function which "abort"s on errors.
24838 */
24839 int
24840func_has_abort(cookie)
24841 void *cookie;
24842{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024843 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844}
24845
24846#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24847typedef enum
24848{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024849 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24850 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24851 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852} var_flavour_T;
24853
24854static var_flavour_T var_flavour __ARGS((char_u *varname));
24855
24856 static var_flavour_T
24857var_flavour(varname)
24858 char_u *varname;
24859{
24860 char_u *p = varname;
24861
24862 if (ASCII_ISUPPER(*p))
24863 {
24864 while (*(++p))
24865 if (ASCII_ISLOWER(*p))
24866 return VAR_FLAVOUR_SESSION;
24867 return VAR_FLAVOUR_VIMINFO;
24868 }
24869 else
24870 return VAR_FLAVOUR_DEFAULT;
24871}
24872#endif
24873
24874#if defined(FEAT_VIMINFO) || defined(PROTO)
24875/*
24876 * Restore global vars that start with a capital from the viminfo file
24877 */
24878 int
24879read_viminfo_varlist(virp, writing)
24880 vir_T *virp;
24881 int writing;
24882{
24883 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024884 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024885 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886
24887 if (!writing && (find_viminfo_parameter('!') != NULL))
24888 {
24889 tab = vim_strchr(virp->vir_line + 1, '\t');
24890 if (tab != NULL)
24891 {
24892 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024893 switch (*tab)
24894 {
24895 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024896#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024897 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024898#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024899 case 'D': type = VAR_DICT; break;
24900 case 'L': type = VAR_LIST; break;
24901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902
24903 tab = vim_strchr(tab, '\t');
24904 if (tab != NULL)
24905 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024906 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024907 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024908 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024910#ifdef FEAT_FLOAT
24911 else if (type == VAR_FLOAT)
24912 (void)string2float(tab + 1, &tv.vval.v_float);
24913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024915 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024916 if (type == VAR_DICT || type == VAR_LIST)
24917 {
24918 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24919
24920 if (etv == NULL)
24921 /* Failed to parse back the dict or list, use it as a
24922 * string. */
24923 tv.v_type = VAR_STRING;
24924 else
24925 {
24926 vim_free(tv.vval.v_string);
24927 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024928 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024929 }
24930 }
24931
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024932 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024933
24934 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024935 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024936 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24937 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 }
24939 }
24940 }
24941
24942 return viminfo_readline(virp);
24943}
24944
24945/*
24946 * Write global vars that start with a capital to the viminfo file
24947 */
24948 void
24949write_viminfo_varlist(fp)
24950 FILE *fp;
24951{
Bram Moolenaar33570922005-01-25 22:26:29 +000024952 hashitem_T *hi;
24953 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024954 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024955 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024956 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024957 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024958 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959
24960 if (find_viminfo_parameter('!') == NULL)
24961 return;
24962
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024963 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024965 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024966 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024970 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024971 this_var = HI2DI(hi);
24972 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024975 {
24976 case VAR_STRING: s = "STR"; break;
24977 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024978#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024979 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024980#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024981 case VAR_DICT: s = "DIC"; break;
24982 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024983 default: continue;
24984 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024985 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024986 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024987 if (p != NULL)
24988 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024989 vim_free(tofree);
24990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 }
24992 }
24993}
24994#endif
24995
24996#if defined(FEAT_SESSION) || defined(PROTO)
24997 int
24998store_session_globals(fd)
24999 FILE *fd;
25000{
Bram Moolenaar33570922005-01-25 22:26:29 +000025001 hashitem_T *hi;
25002 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025003 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 char_u *p, *t;
25005
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025006 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025007 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025009 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025011 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025012 this_var = HI2DI(hi);
25013 if ((this_var->di_tv.v_type == VAR_NUMBER
25014 || this_var->di_tv.v_type == VAR_STRING)
25015 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025016 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025017 /* Escape special characters with a backslash. Turn a LF and
25018 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025019 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025020 (char_u *)"\\\"\n\r");
25021 if (p == NULL) /* out of memory */
25022 break;
25023 for (t = p; *t != NUL; ++t)
25024 if (*t == '\n')
25025 *t = 'n';
25026 else if (*t == '\r')
25027 *t = 'r';
25028 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 this_var->di_key,
25030 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25031 : ' ',
25032 p,
25033 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25034 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025035 || put_eol(fd) == FAIL)
25036 {
25037 vim_free(p);
25038 return FAIL;
25039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040 vim_free(p);
25041 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025042#ifdef FEAT_FLOAT
25043 else if (this_var->di_tv.v_type == VAR_FLOAT
25044 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25045 {
25046 float_T f = this_var->di_tv.vval.v_float;
25047 int sign = ' ';
25048
25049 if (f < 0)
25050 {
25051 f = -f;
25052 sign = '-';
25053 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025054 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025055 this_var->di_key, sign, f) < 0)
25056 || put_eol(fd) == FAIL)
25057 return FAIL;
25058 }
25059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 }
25061 }
25062 return OK;
25063}
25064#endif
25065
Bram Moolenaar661b1822005-07-28 22:36:45 +000025066/*
25067 * Display script name where an item was last set.
25068 * Should only be invoked when 'verbose' is non-zero.
25069 */
25070 void
25071last_set_msg(scriptID)
25072 scid_T scriptID;
25073{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025074 char_u *p;
25075
Bram Moolenaar661b1822005-07-28 22:36:45 +000025076 if (scriptID != 0)
25077 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025078 p = home_replace_save(NULL, get_scriptname(scriptID));
25079 if (p != NULL)
25080 {
25081 verbose_enter();
25082 MSG_PUTS(_("\n\tLast set from "));
25083 MSG_PUTS(p);
25084 vim_free(p);
25085 verbose_leave();
25086 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025087 }
25088}
25089
Bram Moolenaard812df62008-11-09 12:46:09 +000025090/*
25091 * List v:oldfiles in a nice way.
25092 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025093 void
25094ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025095 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025096{
25097 list_T *l = vimvars[VV_OLDFILES].vv_list;
25098 listitem_T *li;
25099 int nr = 0;
25100
25101 if (l == NULL)
25102 msg((char_u *)_("No old files"));
25103 else
25104 {
25105 msg_start();
25106 msg_scroll = TRUE;
25107 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25108 {
25109 msg_outnum((long)++nr);
25110 MSG_PUTS(": ");
25111 msg_outtrans(get_tv_string(&li->li_tv));
25112 msg_putchar('\n');
25113 out_flush(); /* output one line at a time */
25114 ui_breakcheck();
25115 }
25116 /* Assume "got_int" was set to truncate the listing. */
25117 got_int = FALSE;
25118
25119#ifdef FEAT_BROWSE_CMD
25120 if (cmdmod.browse)
25121 {
25122 quit_more = FALSE;
25123 nr = prompt_for_number(FALSE);
25124 msg_starthere();
25125 if (nr > 0)
25126 {
25127 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25128 (long)nr);
25129
25130 if (p != NULL)
25131 {
25132 p = expand_env_save(p);
25133 eap->arg = p;
25134 eap->cmdidx = CMD_edit;
25135 cmdmod.browse = FALSE;
25136 do_exedit(eap, NULL);
25137 vim_free(p);
25138 }
25139 }
25140 }
25141#endif
25142 }
25143}
25144
Bram Moolenaar53744302015-07-17 17:38:22 +020025145/* reset v:option_new, v:option_old and v:option_type */
25146 void
25147reset_v_option_vars()
25148{
25149 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25150 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25151 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25152}
25153
25154
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155#endif /* FEAT_EVAL */
25156
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025158#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159
25160#ifdef WIN3264
25161/*
25162 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25163 */
25164static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25165static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25166static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25167
25168/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025169 * Get the short path (8.3) for the filename in "fnamep".
25170 * Only works for a valid file name.
25171 * When the path gets longer "fnamep" is changed and the allocated buffer
25172 * is put in "bufp".
25173 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25174 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 */
25176 static int
25177get_short_pathname(fnamep, bufp, fnamelen)
25178 char_u **fnamep;
25179 char_u **bufp;
25180 int *fnamelen;
25181{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 char_u *newbuf;
25184
25185 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186 l = GetShortPathName(*fnamep, *fnamep, len);
25187 if (l > len - 1)
25188 {
25189 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025190 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191 newbuf = vim_strnsave(*fnamep, l);
25192 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194
25195 vim_free(*bufp);
25196 *fnamep = *bufp = newbuf;
25197
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025198 /* Really should always succeed, as the buffer is big enough. */
25199 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 }
25201
25202 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025203 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204}
25205
25206/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025207 * Get the short path (8.3) for the filename in "fname". The converted
25208 * path is returned in "bufp".
25209 *
25210 * Some of the directories specified in "fname" may not exist. This function
25211 * will shorten the existing directories at the beginning of the path and then
25212 * append the remaining non-existing path.
25213 *
25214 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025215 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025216 * bufp - Pointer to an allocated buffer for the filename.
25217 * fnamelen - Length of the filename pointed to by fname
25218 *
25219 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 */
25221 static int
25222shortpath_for_invalid_fname(fname, bufp, fnamelen)
25223 char_u **fname;
25224 char_u **bufp;
25225 int *fnamelen;
25226{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025227 char_u *short_fname, *save_fname, *pbuf_unused;
25228 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025230 int old_len, len;
25231 int new_len, sfx_len;
25232 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233
25234 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025235 old_len = *fnamelen;
25236 save_fname = vim_strnsave(*fname, old_len);
25237 pbuf_unused = NULL;
25238 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 endp = save_fname + old_len - 1; /* Find the end of the copy */
25241 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025243 /*
25244 * Try shortening the supplied path till it succeeds by removing one
25245 * directory at a time from the tail of the path.
25246 */
25247 len = 0;
25248 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025250 /* go back one path-separator */
25251 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25252 --endp;
25253 if (endp <= save_fname)
25254 break; /* processed the complete path */
25255
25256 /*
25257 * Replace the path separator with a NUL and try to shorten the
25258 * resulting path.
25259 */
25260 ch = *endp;
25261 *endp = 0;
25262 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025263 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025264 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25265 {
25266 retval = FAIL;
25267 goto theend;
25268 }
25269 *endp = ch; /* preserve the string */
25270
25271 if (len > 0)
25272 break; /* successfully shortened the path */
25273
25274 /* failed to shorten the path. Skip the path separator */
25275 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 }
25277
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025278 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025280 /*
25281 * Succeeded in shortening the path. Now concatenate the shortened
25282 * path with the remaining path at the tail.
25283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025285 /* Compute the length of the new path. */
25286 sfx_len = (int)(save_endp - endp) + 1;
25287 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025289 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025291 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 /* There is not enough space in the currently allocated string,
25294 * copy it to a buffer big enough. */
25295 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025297 {
25298 retval = FAIL;
25299 goto theend;
25300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301 }
25302 else
25303 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025304 /* Transfer short_fname to the main buffer (it's big enough),
25305 * unless get_short_pathname() did its work in-place. */
25306 *fname = *bufp = save_fname;
25307 if (short_fname != save_fname)
25308 vim_strncpy(save_fname, short_fname, len);
25309 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025311
25312 /* concat the not-shortened part of the path */
25313 vim_strncpy(*fname + len, endp, sfx_len);
25314 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025316
25317theend:
25318 vim_free(pbuf_unused);
25319 vim_free(save_fname);
25320
25321 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322}
25323
25324/*
25325 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025326 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 */
25328 static int
25329shortpath_for_partial(fnamep, bufp, fnamelen)
25330 char_u **fnamep;
25331 char_u **bufp;
25332 int *fnamelen;
25333{
25334 int sepcount, len, tflen;
25335 char_u *p;
25336 char_u *pbuf, *tfname;
25337 int hasTilde;
25338
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025339 /* Count up the path separators from the RHS.. so we know which part
25340 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025342 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 if (vim_ispathsep(*p))
25344 ++sepcount;
25345
25346 /* Need full path first (use expand_env() to remove a "~/") */
25347 hasTilde = (**fnamep == '~');
25348 if (hasTilde)
25349 pbuf = tfname = expand_env_save(*fnamep);
25350 else
25351 pbuf = tfname = FullName_save(*fnamep, FALSE);
25352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025353 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357
25358 if (len == 0)
25359 {
25360 /* Don't have a valid filename, so shorten the rest of the
25361 * path if we can. This CAN give us invalid 8.3 filenames, but
25362 * there's not a lot of point in guessing what it might be.
25363 */
25364 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025365 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 }
25368
25369 /* Count the paths backward to find the beginning of the desired string. */
25370 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025371 {
25372#ifdef FEAT_MBYTE
25373 if (has_mbyte)
25374 p -= mb_head_off(tfname, p);
25375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 if (vim_ispathsep(*p))
25377 {
25378 if (sepcount == 0 || (hasTilde && sepcount == 1))
25379 break;
25380 else
25381 sepcount --;
25382 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 if (hasTilde)
25385 {
25386 --p;
25387 if (p >= tfname)
25388 *p = '~';
25389 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 }
25392 else
25393 ++p;
25394
25395 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25396 vim_free(*bufp);
25397 *fnamelen = (int)STRLEN(p);
25398 *bufp = pbuf;
25399 *fnamep = p;
25400
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025401 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402}
25403#endif /* WIN3264 */
25404
25405/*
25406 * Adjust a filename, according to a string of modifiers.
25407 * *fnamep must be NUL terminated when called. When returning, the length is
25408 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025409 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 * When there is an error, *fnamep is set to NULL.
25411 */
25412 int
25413modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25414 char_u *src; /* string with modifiers */
25415 int *usedlen; /* characters after src that are used */
25416 char_u **fnamep; /* file name so far */
25417 char_u **bufp; /* buffer for allocated file name or NULL */
25418 int *fnamelen; /* length of fnamep */
25419{
25420 int valid = 0;
25421 char_u *tail;
25422 char_u *s, *p, *pbuf;
25423 char_u dirname[MAXPATHL];
25424 int c;
25425 int has_fullname = 0;
25426#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025427 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 int has_shortname = 0;
25429#endif
25430
25431repeat:
25432 /* ":p" - full path/file_name */
25433 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25434 {
25435 has_fullname = 1;
25436
25437 valid |= VALID_PATH;
25438 *usedlen += 2;
25439
25440 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25441 if ((*fnamep)[0] == '~'
25442#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25443 && ((*fnamep)[1] == '/'
25444# ifdef BACKSLASH_IN_FILENAME
25445 || (*fnamep)[1] == '\\'
25446# endif
25447 || (*fnamep)[1] == NUL)
25448
25449#endif
25450 )
25451 {
25452 *fnamep = expand_env_save(*fnamep);
25453 vim_free(*bufp); /* free any allocated file name */
25454 *bufp = *fnamep;
25455 if (*fnamep == NULL)
25456 return -1;
25457 }
25458
25459 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025460 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 {
25462 if (vim_ispathsep(*p)
25463 && p[1] == '.'
25464 && (p[2] == NUL
25465 || vim_ispathsep(p[2])
25466 || (p[2] == '.'
25467 && (p[3] == NUL || vim_ispathsep(p[3])))))
25468 break;
25469 }
25470
25471 /* FullName_save() is slow, don't use it when not needed. */
25472 if (*p != NUL || !vim_isAbsName(*fnamep))
25473 {
25474 *fnamep = FullName_save(*fnamep, *p != NUL);
25475 vim_free(*bufp); /* free any allocated file name */
25476 *bufp = *fnamep;
25477 if (*fnamep == NULL)
25478 return -1;
25479 }
25480
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025481#ifdef WIN3264
25482# if _WIN32_WINNT >= 0x0500
25483 if (vim_strchr(*fnamep, '~') != NULL)
25484 {
25485 /* Expand 8.3 filename to full path. Needed to make sure the same
25486 * file does not have two different names.
25487 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25488 p = alloc(_MAX_PATH + 1);
25489 if (p != NULL)
25490 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025491 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025492 {
25493 vim_free(*bufp);
25494 *bufp = *fnamep = p;
25495 }
25496 else
25497 vim_free(p);
25498 }
25499 }
25500# endif
25501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025502 /* Append a path separator to a directory. */
25503 if (mch_isdir(*fnamep))
25504 {
25505 /* Make room for one or two extra characters. */
25506 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25507 vim_free(*bufp); /* free any allocated file name */
25508 *bufp = *fnamep;
25509 if (*fnamep == NULL)
25510 return -1;
25511 add_pathsep(*fnamep);
25512 }
25513 }
25514
25515 /* ":." - path relative to the current directory */
25516 /* ":~" - path relative to the home directory */
25517 /* ":8" - shortname path - postponed till after */
25518 while (src[*usedlen] == ':'
25519 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25520 {
25521 *usedlen += 2;
25522 if (c == '8')
25523 {
25524#ifdef WIN3264
25525 has_shortname = 1; /* Postpone this. */
25526#endif
25527 continue;
25528 }
25529 pbuf = NULL;
25530 /* Need full path first (use expand_env() to remove a "~/") */
25531 if (!has_fullname)
25532 {
25533 if (c == '.' && **fnamep == '~')
25534 p = pbuf = expand_env_save(*fnamep);
25535 else
25536 p = pbuf = FullName_save(*fnamep, FALSE);
25537 }
25538 else
25539 p = *fnamep;
25540
25541 has_fullname = 0;
25542
25543 if (p != NULL)
25544 {
25545 if (c == '.')
25546 {
25547 mch_dirname(dirname, MAXPATHL);
25548 s = shorten_fname(p, dirname);
25549 if (s != NULL)
25550 {
25551 *fnamep = s;
25552 if (pbuf != NULL)
25553 {
25554 vim_free(*bufp); /* free any allocated file name */
25555 *bufp = pbuf;
25556 pbuf = NULL;
25557 }
25558 }
25559 }
25560 else
25561 {
25562 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25563 /* Only replace it when it starts with '~' */
25564 if (*dirname == '~')
25565 {
25566 s = vim_strsave(dirname);
25567 if (s != NULL)
25568 {
25569 *fnamep = s;
25570 vim_free(*bufp);
25571 *bufp = s;
25572 }
25573 }
25574 }
25575 vim_free(pbuf);
25576 }
25577 }
25578
25579 tail = gettail(*fnamep);
25580 *fnamelen = (int)STRLEN(*fnamep);
25581
25582 /* ":h" - head, remove "/file_name", can be repeated */
25583 /* Don't remove the first "/" or "c:\" */
25584 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25585 {
25586 valid |= VALID_HEAD;
25587 *usedlen += 2;
25588 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025589 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025590 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 *fnamelen = (int)(tail - *fnamep);
25592#ifdef VMS
25593 if (*fnamelen > 0)
25594 *fnamelen += 1; /* the path separator is part of the path */
25595#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025596 if (*fnamelen == 0)
25597 {
25598 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25599 p = vim_strsave((char_u *)".");
25600 if (p == NULL)
25601 return -1;
25602 vim_free(*bufp);
25603 *bufp = *fnamep = tail = p;
25604 *fnamelen = 1;
25605 }
25606 else
25607 {
25608 while (tail > s && !after_pathsep(s, tail))
25609 mb_ptr_back(*fnamep, tail);
25610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 }
25612
25613 /* ":8" - shortname */
25614 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25615 {
25616 *usedlen += 2;
25617#ifdef WIN3264
25618 has_shortname = 1;
25619#endif
25620 }
25621
25622#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025623 /*
25624 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625 */
25626 if (has_shortname)
25627 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025628 /* Copy the string if it is shortened by :h and when it wasn't copied
25629 * yet, because we are going to change it in place. Avoids changing
25630 * the buffer name for "%:8". */
25631 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 {
25633 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025634 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 return -1;
25636 vim_free(*bufp);
25637 *bufp = *fnamep = p;
25638 }
25639
25640 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025641 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642 if (!has_fullname && !vim_isAbsName(*fnamep))
25643 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025644 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 return -1;
25646 }
25647 else
25648 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025649 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650
Bram Moolenaardc935552011-08-17 15:23:23 +020025651 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025653 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 return -1;
25655
25656 if (l == 0)
25657 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025658 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025660 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 return -1;
25662 }
25663 *fnamelen = l;
25664 }
25665 }
25666#endif /* WIN3264 */
25667
25668 /* ":t" - tail, just the basename */
25669 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25670 {
25671 *usedlen += 2;
25672 *fnamelen -= (int)(tail - *fnamep);
25673 *fnamep = tail;
25674 }
25675
25676 /* ":e" - extension, can be repeated */
25677 /* ":r" - root, without extension, can be repeated */
25678 while (src[*usedlen] == ':'
25679 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25680 {
25681 /* find a '.' in the tail:
25682 * - for second :e: before the current fname
25683 * - otherwise: The last '.'
25684 */
25685 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25686 s = *fnamep - 2;
25687 else
25688 s = *fnamep + *fnamelen - 1;
25689 for ( ; s > tail; --s)
25690 if (s[0] == '.')
25691 break;
25692 if (src[*usedlen + 1] == 'e') /* :e */
25693 {
25694 if (s > tail)
25695 {
25696 *fnamelen += (int)(*fnamep - (s + 1));
25697 *fnamep = s + 1;
25698#ifdef VMS
25699 /* cut version from the extension */
25700 s = *fnamep + *fnamelen - 1;
25701 for ( ; s > *fnamep; --s)
25702 if (s[0] == ';')
25703 break;
25704 if (s > *fnamep)
25705 *fnamelen = s - *fnamep;
25706#endif
25707 }
25708 else if (*fnamep <= tail)
25709 *fnamelen = 0;
25710 }
25711 else /* :r */
25712 {
25713 if (s > tail) /* remove one extension */
25714 *fnamelen = (int)(s - *fnamep);
25715 }
25716 *usedlen += 2;
25717 }
25718
25719 /* ":s?pat?foo?" - substitute */
25720 /* ":gs?pat?foo?" - global substitute */
25721 if (src[*usedlen] == ':'
25722 && (src[*usedlen + 1] == 's'
25723 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25724 {
25725 char_u *str;
25726 char_u *pat;
25727 char_u *sub;
25728 int sep;
25729 char_u *flags;
25730 int didit = FALSE;
25731
25732 flags = (char_u *)"";
25733 s = src + *usedlen + 2;
25734 if (src[*usedlen + 1] == 'g')
25735 {
25736 flags = (char_u *)"g";
25737 ++s;
25738 }
25739
25740 sep = *s++;
25741 if (sep)
25742 {
25743 /* find end of pattern */
25744 p = vim_strchr(s, sep);
25745 if (p != NULL)
25746 {
25747 pat = vim_strnsave(s, (int)(p - s));
25748 if (pat != NULL)
25749 {
25750 s = p + 1;
25751 /* find end of substitution */
25752 p = vim_strchr(s, sep);
25753 if (p != NULL)
25754 {
25755 sub = vim_strnsave(s, (int)(p - s));
25756 str = vim_strnsave(*fnamep, *fnamelen);
25757 if (sub != NULL && str != NULL)
25758 {
25759 *usedlen = (int)(p + 1 - src);
25760 s = do_string_sub(str, pat, sub, flags);
25761 if (s != NULL)
25762 {
25763 *fnamep = s;
25764 *fnamelen = (int)STRLEN(s);
25765 vim_free(*bufp);
25766 *bufp = s;
25767 didit = TRUE;
25768 }
25769 }
25770 vim_free(sub);
25771 vim_free(str);
25772 }
25773 vim_free(pat);
25774 }
25775 }
25776 /* after using ":s", repeat all the modifiers */
25777 if (didit)
25778 goto repeat;
25779 }
25780 }
25781
Bram Moolenaar26df0922014-02-23 23:39:13 +010025782 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25783 {
25784 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25785 if (p == NULL)
25786 return -1;
25787 vim_free(*bufp);
25788 *bufp = *fnamep = p;
25789 *fnamelen = (int)STRLEN(p);
25790 *usedlen += 2;
25791 }
25792
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 return valid;
25794}
25795
25796/*
25797 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25798 * "flags" can be "g" to do a global substitute.
25799 * Returns an allocated string, NULL for error.
25800 */
25801 char_u *
25802do_string_sub(str, pat, sub, flags)
25803 char_u *str;
25804 char_u *pat;
25805 char_u *sub;
25806 char_u *flags;
25807{
25808 int sublen;
25809 regmatch_T regmatch;
25810 int i;
25811 int do_all;
25812 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025813 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 garray_T ga;
25815 char_u *ret;
25816 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025817 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
25819 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25820 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025821 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822
25823 ga_init2(&ga, 1, 200);
25824
25825 do_all = (flags[0] == 'g');
25826
25827 regmatch.rm_ic = p_ic;
25828 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25829 if (regmatch.regprog != NULL)
25830 {
25831 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025832 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25834 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025835 /* Skip empty match except for first match. */
25836 if (regmatch.startp[0] == regmatch.endp[0])
25837 {
25838 if (zero_width == regmatch.startp[0])
25839 {
25840 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025841 i = MB_PTR2LEN(tail);
25842 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25843 (size_t)i);
25844 ga.ga_len += i;
25845 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025846 continue;
25847 }
25848 zero_width = regmatch.startp[0];
25849 }
25850
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851 /*
25852 * Get some space for a temporary buffer to do the substitution
25853 * into. It will contain:
25854 * - The text up to where the match is.
25855 * - The substituted text.
25856 * - The text after the match.
25857 */
25858 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025859 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25861 {
25862 ga_clear(&ga);
25863 break;
25864 }
25865
25866 /* copy the text up to where the match is */
25867 i = (int)(regmatch.startp[0] - tail);
25868 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25869 /* add the substituted text */
25870 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25871 + ga.ga_len + i, TRUE, TRUE, FALSE);
25872 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025873 tail = regmatch.endp[0];
25874 if (*tail == NUL)
25875 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876 if (!do_all)
25877 break;
25878 }
25879
25880 if (ga.ga_data != NULL)
25881 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25882
Bram Moolenaar473de612013-06-08 18:19:48 +020025883 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 }
25885
25886 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25887 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025888 if (p_cpo == empty_option)
25889 p_cpo = save_cpo;
25890 else
25891 /* Darn, evaluating {sub} expression changed the value. */
25892 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893
25894 return ret;
25895}
25896
25897#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */