blob: feaa71c33cf9e669c9bc5bf931b230274409cb6a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100470static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200474static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100476static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100493static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200529static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200562static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200566static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000574static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000575static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200576static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100588static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000591static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000605static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100610static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200628#ifdef FEAT_LUA
629static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200636static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
645static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
649static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100653static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000654static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000659static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200661#ifdef FEAT_PYTHON3
662static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
663#endif
664#ifdef FEAT_PYTHON
665static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000668static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000669static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#ifdef FEAT_FLOAT
682static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200684static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100686static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000689static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000691static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200696static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000699static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000700static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000701static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000702static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200704static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000705static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100707#ifdef FEAT_CRYPT
708static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
709#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000710static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200711static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200715static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
723static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000726static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728#ifdef HAVE_STRFTIME
729static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
731static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200737static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200738static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000744static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200745static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200747static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000748static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000749static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000751static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000752static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000754static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200755#ifdef FEAT_FLOAT
756static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
763static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200766static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200767static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100768static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100772static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000779static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100783static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100784static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785
Bram Moolenaar493c1782014-05-28 14:34:46 +0200786static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000787static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static int get_env_len __ARGS((char_u **arg));
789static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000790static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000791static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
792#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
793#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
794 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000795static 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 +0000796static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200798static 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 +0000799static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static typval_T *alloc_tv __ARGS((void));
801static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static void init_tv __ARGS((typval_T *varp));
803static long get_tv_number __ARGS((typval_T *varp));
804static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000805static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000806static char_u *get_tv_string __ARGS((typval_T *varp));
807static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000808static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100809static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
810static 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 +0000811static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
812static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
813static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000814static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
815static 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 +0000816static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200817static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
818static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200819static int var_check_func_name __ARGS((char_u *name, int new_var));
820static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200821static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000822static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
824static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
825static int eval_fname_script __ARGS((char_u *p));
826static int eval_fname_sid __ARGS((char_u *p));
827static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static ufunc_T *find_func __ARGS((char_u *name));
829static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200830static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831#ifdef FEAT_PROFILE
832static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000833static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
834static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
835static int
836# ifdef __BORLANDC__
837 _RTLENTRYF
838# endif
839 prof_total_cmp __ARGS((const void *s1, const void *s2));
840static int
841# ifdef __BORLANDC__
842 _RTLENTRYF
843# endif
844 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200846static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000847static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000848static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000850static 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 +0000851static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
852static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000853static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000854static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
855static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000856static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000857static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000858static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200859static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200860static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200862
863#ifdef EBCDIC
864static int compare_func_name __ARGS((const void *s1, const void *s2));
865static void sortFunctions __ARGS(());
866#endif
867
Bram Moolenaar33570922005-01-25 22:26:29 +0000868/*
869 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000870 */
871 void
872eval_init()
873{
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 int i;
875 struct vimvar *p;
876
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200877 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
878 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200879 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000880 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
883 for (i = 0; i < VV_LEN; ++i)
884 {
885 p = &vimvars[i];
886 STRCPY(p->vv_di.di_key, p->vv_name);
887 if (p->vv_flags & VV_RO)
888 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
889 else if (p->vv_flags & VV_RO_SBX)
890 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
891 else
892 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000893
894 /* add to v: scope dict, unless the value is not always available */
895 if (p->vv_type != VAR_UNKNOWN)
896 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000897 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000898 /* add to compat scope dict */
899 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000900 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000901 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100902 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200903 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100904 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200905 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906
907#ifdef EBCDIC
908 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100909 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910 */
911 sortFunctions();
912#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000913}
914
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915#if defined(EXITFREE) || defined(PROTO)
916 void
917eval_clear()
918{
919 int i;
920 struct vimvar *p;
921
922 for (i = 0; i < VV_LEN; ++i)
923 {
924 p = &vimvars[i];
925 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000927 vim_free(p->vv_str);
928 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000929 }
930 else if (p->vv_di.di_tv.v_type == VAR_LIST)
931 {
932 list_unref(p->vv_list);
933 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000934 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935 }
936 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000937 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938 hash_clear(&compat_hashtab);
939
Bram Moolenaard9fba312005-06-26 22:34:35 +0000940 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100941# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200942 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100943# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000944
945 /* global variables */
946 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000947
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000948 /* autoloaded script names */
949 ga_clear_strings(&ga_loaded);
950
Bram Moolenaarcca74132013-09-25 21:00:28 +0200951 /* Script-local variables. First clear all the variables and in a second
952 * loop free the scriptvar_T, because a variable in one script might hold
953 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200954 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200955 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200956 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200958 ga_clear(&ga_scripts);
959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000960 /* unreferenced lists and dicts */
961 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000962
963 /* functions */
964 free_all_functions();
965 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966}
967#endif
968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * Return the name of the executed function.
971 */
972 char_u *
973func_name(cookie)
974 void *cookie;
975{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000976 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Return the address holding the next breakpoint line for a funccall cookie.
981 */
982 linenr_T *
983func_breakpoint(cookie)
984 void *cookie;
985{
Bram Moolenaar33570922005-01-25 22:26:29 +0000986 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989/*
990 * Return the address holding the debug tick for a funccall cookie.
991 */
992 int *
993func_dbg_tick(cookie)
994 void *cookie;
995{
Bram Moolenaar33570922005-01-25 22:26:29 +0000996 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
998
999/*
1000 * Return the nesting level for a funccall cookie.
1001 */
1002 int
1003func_level(cookie)
1004 void *cookie;
1005{
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001010funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001012/* pointer to list of previously used funccal, still around because some
1013 * item in it is still being used. */
1014funccall_T *previous_funccal = NULL;
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*
1017 * Return TRUE when a function was ended by a ":return" command.
1018 */
1019 int
1020current_func_returned()
1021{
1022 return current_funccal->returned;
1023}
1024
1025
1026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Set an internal variable to a string value. Creates the variable if it does
1028 * not already exist.
1029 */
1030 void
1031set_internal_string_var(name, value)
1032 char_u *name;
1033 char_u *value;
1034{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001035 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 val = vim_strsave(value);
1039 if (val != NULL)
1040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001042 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001044 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001045 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
1047 }
1048}
1049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052static char_u *redir_endp = NULL;
1053static char_u *redir_varname = NULL;
1054
1055/*
1056 * Start recording command output to a variable
1057 * Returns OK if successfully completed the setup. FAIL otherwise.
1058 */
1059 int
1060var_redir_start(name, append)
1061 char_u *name;
1062 int append; /* append to an existing variable */
1063{
1064 int save_emsg;
1065 int err;
1066 typval_T tv;
1067
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001069 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 {
1071 EMSG(_(e_invarg));
1072 return FAIL;
1073 }
1074
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 redir_varname = vim_strsave(name);
1077 if (redir_varname == NULL)
1078 return FAIL;
1079
1080 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1081 if (redir_lval == NULL)
1082 {
1083 var_redir_stop();
1084 return FAIL;
1085 }
1086
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001087 /* The output is stored in growarray "redir_ga" until redirection ends. */
1088 ga_init2(&redir_ga, (int)sizeof(char), 500);
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001091 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001092 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1094 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 if (redir_endp != NULL && *redir_endp != NUL)
1097 /* Trailing characters are present after the variable name */
1098 EMSG(_(e_trailing));
1099 else
1100 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001101 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 var_redir_stop();
1103 return FAIL;
1104 }
1105
1106 /* check if we can write to the variable: set it to or append an empty
1107 * string */
1108 save_emsg = did_emsg;
1109 did_emsg = FALSE;
1110 tv.v_type = VAR_STRING;
1111 tv.vval.v_string = (char_u *)"";
1112 if (append)
1113 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1114 else
1115 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001116 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001118 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 if (err)
1120 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001121 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
1123 return FAIL;
1124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 * Append "value[value_len]" to the variable set by var_redir_start().
1131 * The actual appending is postponed until redirection ends, because the value
1132 * appended may in fact be the string we write to, changing it may cause freed
1133 * memory to be used:
1134 * :redir => foo
1135 * :let foo
1136 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 */
1138 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144
1145 if (redir_lval == NULL)
1146 return;
1147
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001149 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154 {
1155 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001156 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 }
1158 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160}
1161
1162/*
1163 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 */
1166 void
1167var_redir_stop()
1168{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 typval_T tv;
1170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 if (redir_lval != NULL)
1172 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001173 /* If there was no error: assign the text to the variable. */
1174 if (redir_endp != NULL)
1175 {
1176 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1177 tv.v_type = VAR_STRING;
1178 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001179 /* Call get_lval() again, if it's inside a Dict or List it may
1180 * have changed. */
1181 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001182 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001183 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1184 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1185 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001186 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* free the collected output */
1189 vim_free(redir_ga.ga_data);
1190 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 vim_free(redir_lval);
1193 redir_lval = NULL;
1194 }
1195 vim_free(redir_varname);
1196 redir_varname = NULL;
1197}
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199# if defined(FEAT_MBYTE) || defined(PROTO)
1200 int
1201eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1202 char_u *enc_from;
1203 char_u *enc_to;
1204 char_u *fname_from;
1205 char_u *fname_to;
1206{
1207 int err = FALSE;
1208
1209 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1210 set_vim_var_string(VV_CC_TO, enc_to, -1);
1211 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1212 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1213 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1214 err = TRUE;
1215 set_vim_var_string(VV_CC_FROM, NULL, -1);
1216 set_vim_var_string(VV_CC_TO, NULL, -1);
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1219
1220 if (err)
1221 return FAIL;
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1227 int
1228eval_printexpr(fname, args)
1229 char_u *fname;
1230 char_u *args;
1231{
1232 int err = FALSE;
1233
1234 set_vim_var_string(VV_FNAME_IN, fname, -1);
1235 set_vim_var_string(VV_CMDARG, args, -1);
1236 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1237 err = TRUE;
1238 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1239 set_vim_var_string(VV_CMDARG, NULL, -1);
1240
1241 if (err)
1242 {
1243 mch_remove(fname);
1244 return FAIL;
1245 }
1246 return OK;
1247}
1248# endif
1249
1250# if defined(FEAT_DIFF) || defined(PROTO)
1251 void
1252eval_diff(origfile, newfile, outfile)
1253 char_u *origfile;
1254 char_u *newfile;
1255 char_u *outfile;
1256{
1257 int err = FALSE;
1258
1259 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1260 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1261 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1262 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1263 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1264 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1265 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1266}
1267
1268 void
1269eval_patch(origfile, difffile, outfile)
1270 char_u *origfile;
1271 char_u *difffile;
1272 char_u *outfile;
1273{
1274 int err;
1275
1276 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1277 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1278 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1279 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1280 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1281 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1282 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1283}
1284# endif
1285
1286/*
1287 * Top level evaluation function, returning a boolean.
1288 * Sets "error" to TRUE if there was an error.
1289 * Return TRUE or FALSE.
1290 */
1291 int
1292eval_to_bool(arg, error, nextcmd, skip)
1293 char_u *arg;
1294 int *error;
1295 char_u **nextcmd;
1296 int skip; /* only parse, don't execute */
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 int retval = FALSE;
1300
1301 if (skip)
1302 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 else
1306 {
1307 *error = FALSE;
1308 if (!skip)
1309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001310 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314 if (skip)
1315 --emsg_skip;
1316
1317 return retval;
1318}
1319
1320/*
1321 * Top level evaluation function, returning a string. If "skip" is TRUE,
1322 * only parsing to "nextcmd" is done, without reporting errors. Return
1323 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1324 */
1325 char_u *
1326eval_to_string_skip(arg, nextcmd, skip)
1327 char_u *arg;
1328 char_u **nextcmd;
1329 int skip; /* only parse, don't execute */
1330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *retval;
1333
1334 if (skip)
1335 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 retval = NULL;
1338 else
1339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 retval = vim_strsave(get_tv_string(&tv));
1341 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 if (skip)
1344 --emsg_skip;
1345
1346 return retval;
1347}
1348
1349/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001350 * Skip over an expression at "*pp".
1351 * Return FAIL for an error, OK otherwise.
1352 */
1353 int
1354skip_expr(pp)
1355 char_u **pp;
1356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001358
1359 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001360 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001361}
1362
1363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 * When "convert" is TRUE convert a List into a sequence of lines and convert
1366 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 * Return pointer to allocated memory, or NULL for failure.
1368 */
1369 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001370eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *arg;
1372 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001377 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001378#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001379 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 retval = NULL;
1384 else
1385 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001386 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 {
1388 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001389 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001390 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 if (tv.vval.v_list->lv_len > 0)
1393 ga_append(&ga, NL);
1394 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 ga_append(&ga, NUL);
1396 retval = (char_u *)ga.ga_data;
1397 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398#ifdef FEAT_FLOAT
1399 else if (convert && tv.v_type == VAR_FLOAT)
1400 {
1401 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1402 retval = vim_strsave(numbuf);
1403 }
1404#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 else
1406 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409
1410 return retval;
1411}
1412
1413/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001414 * Call eval_to_string() without using current local variables and using
1415 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
1417 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001418eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 char_u *arg;
1420 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 char_u *retval;
1424 void *save_funccalp;
1425
1426 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001427 if (use_sandbox)
1428 ++sandbox;
1429 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001431 if (use_sandbox)
1432 --sandbox;
1433 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 restore_funccal(save_funccalp);
1435 return retval;
1436}
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438/*
1439 * Top level evaluation function, returning a number.
1440 * Evaluates "expr" silently.
1441 * Returns -1 for an error.
1442 */
1443 int
1444eval_to_number(expr)
1445 char_u *expr;
1446{
Bram Moolenaar33570922005-01-25 22:26:29 +00001447 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001449 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 ++emsg_off;
1452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 retval = -1;
1455 else
1456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001457 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460 --emsg_off;
1461
1462 return retval;
1463}
1464
Bram Moolenaara40058a2005-07-11 22:42:07 +00001465/*
1466 * Prepare v: variable "idx" to be used.
1467 * Save the current typeval in "save_tv".
1468 * When not used yet add the variable to the v: hashtable.
1469 */
1470 static void
1471prepare_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 *save_tv = vimvars[idx].vv_tv;
1476 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1477 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1478}
1479
1480/*
1481 * Restore v: variable "idx" to typeval "save_tv".
1482 * When no longer defined, remove the variable from the v: hashtable.
1483 */
1484 static void
1485restore_vimvar(idx, save_tv)
1486 int idx;
1487 typval_T *save_tv;
1488{
1489 hashitem_T *hi;
1490
Bram Moolenaara40058a2005-07-11 22:42:07 +00001491 vimvars[idx].vv_tv = *save_tv;
1492 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1493 {
1494 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1495 if (HASHITEM_EMPTY(hi))
1496 EMSG2(_(e_intern2), "restore_vimvar()");
1497 else
1498 hash_remove(&vimvarht, hi);
1499 }
1500}
1501
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001502#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001503/*
1504 * Evaluate an expression to a list with suggestions.
1505 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001506 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001507 */
1508 list_T *
1509eval_spell_expr(badword, expr)
1510 char_u *badword;
1511 char_u *expr;
1512{
1513 typval_T save_val;
1514 typval_T rettv;
1515 list_T *list = NULL;
1516 char_u *p = skipwhite(expr);
1517
1518 /* Set "v:val" to the bad word. */
1519 prepare_vimvar(VV_VAL, &save_val);
1520 vimvars[VV_VAL].vv_type = VAR_STRING;
1521 vimvars[VV_VAL].vv_str = badword;
1522 if (p_verbose == 0)
1523 ++emsg_off;
1524
1525 if (eval1(&p, &rettv, TRUE) == OK)
1526 {
1527 if (rettv.v_type != VAR_LIST)
1528 clear_tv(&rettv);
1529 else
1530 list = rettv.vval.v_list;
1531 }
1532
1533 if (p_verbose == 0)
1534 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535 restore_vimvar(VV_VAL, &save_val);
1536
1537 return list;
1538}
1539
1540/*
1541 * "list" is supposed to contain two items: a word and a number. Return the
1542 * word in "pp" and the number as the return value.
1543 * Return -1 if anything isn't right.
1544 * Used to get the good word and score from the eval_spell_expr() result.
1545 */
1546 int
1547get_spellword(list, pp)
1548 list_T *list;
1549 char_u **pp;
1550{
1551 listitem_T *li;
1552
1553 li = list->lv_first;
1554 if (li == NULL)
1555 return -1;
1556 *pp = get_tv_string(&li->li_tv);
1557
1558 li = li->li_next;
1559 if (li == NULL)
1560 return -1;
1561 return get_tv_number(&li->li_tv);
1562}
1563#endif
1564
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001566 * Top level evaluation function.
1567 * Returns an allocated typval_T with the result.
1568 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001569 */
1570 typval_T *
1571eval_expr(arg, nextcmd)
1572 char_u *arg;
1573 char_u **nextcmd;
1574{
1575 typval_T *tv;
1576
1577 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001578 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001579 {
1580 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001581 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001582 }
1583
1584 return tv;
1585}
1586
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 * Uses argv[argc] for the function arguments. Only Number and String
1591 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001594 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001595call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 char_u *func;
1597 int argc;
1598 char_u **argv;
1599 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar33570922005-01-25 22:26:29 +00001603 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 long n;
1605 int len;
1606 int i;
1607 int doesrange;
1608 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001611 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 for (i = 0; i < argc; i++)
1616 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001617 /* Pass a NULL or empty argument as an empty string */
1618 if (argv[i] == NULL || *argv[i] == NUL)
1619 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001620 argvars[i].v_type = VAR_STRING;
1621 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001622 continue;
1623 }
1624
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001625 if (str_arg_only)
1626 len = 0;
1627 else
1628 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001629 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (len != 0 && len == (int)STRLEN(argv[i]))
1631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001632 argvars[i].v_type = VAR_NUMBER;
1633 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 else
1636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001637 argvars[i].v_type = VAR_STRING;
1638 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 }
1641
1642 if (safe)
1643 {
1644 save_funccalp = save_funccal();
1645 ++sandbox;
1646 }
1647
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1649 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (safe)
1653 {
1654 --sandbox;
1655 restore_funccal(save_funccalp);
1656 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 vim_free(argvars);
1658
1659 if (ret == FAIL)
1660 clear_tv(rettv);
1661
1662 return ret;
1663}
1664
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001665/*
1666 * Call vimL function "func" and return the result as a number.
1667 * Returns -1 when calling the function fails.
1668 * Uses argv[argc] for the function arguments.
1669 */
1670 long
1671call_func_retnr(func, argc, argv, safe)
1672 char_u *func;
1673 int argc;
1674 char_u **argv;
1675 int safe; /* use the sandbox */
1676{
1677 typval_T rettv;
1678 long retval;
1679
1680 /* All arguments are passed as strings, no conversion to number. */
1681 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1682 return -1;
1683
1684 retval = get_tv_number_chk(&rettv, NULL);
1685 clear_tv(&rettv);
1686 return retval;
1687}
1688
1689#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1690 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1691
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694 * Call vimL function "func" and return the result as a string.
1695 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 * Uses argv[argc] for the function arguments.
1697 */
1698 void *
1699call_func_retstr(func, argc, argv, safe)
1700 char_u *func;
1701 int argc;
1702 char_u **argv;
1703 int safe; /* use the sandbox */
1704{
1705 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001706 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 return NULL;
1711
1712 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return retval;
1715}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001716# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001718/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001719 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 */
1723 void *
1724call_func_retlist(func, argc, argv, safe)
1725 char_u *func;
1726 int argc;
1727 char_u **argv;
1728 int safe; /* use the sandbox */
1729{
1730 typval_T rettv;
1731
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001732 /* All arguments are passed as strings, no conversion to number. */
1733 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 return NULL;
1735
1736 if (rettv.v_type != VAR_LIST)
1737 {
1738 clear_tv(&rettv);
1739 return NULL;
1740 }
1741
1742 return rettv.vval.v_list;
1743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
1745
1746/*
1747 * Save the current function call pointer, and set it to NULL.
1748 * Used when executing autocommands and for ":source".
1749 */
1750 void *
1751save_funccal()
1752{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001753 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 current_funccal = NULL;
1756 return (void *)fc;
1757}
1758
1759 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001760restore_funccal(vfc)
1761 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
1775prof_child_enter(tm)
1776 proftime_T *tm; /* place to store waittime */
1777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
1790prof_child_exit(tm)
1791 proftime_T *tm; /* where waittime was stored */
1792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
1813eval_foldexpr(arg, cp)
1814 char_u *arg;
1815 int *cp;
1816{
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 int retval;
1819 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001820 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1821 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001824 if (use_sandbox)
1825 ++sandbox;
1826 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 retval = 0;
1830 else
1831 {
1832 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 if (tv.v_type == VAR_NUMBER)
1834 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001835 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 retval = 0;
1837 else
1838 {
1839 /* If the result is a string, check if there is a non-digit before
1840 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (!VIM_ISDIGIT(*s) && *s != '-')
1843 *cp = *s++;
1844 retval = atol((char *)s);
1845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 --sandbox;
1851 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 return retval;
1854}
1855#endif
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 * ":let" list all variable values
1859 * ":let var1 var2" list variable values
1860 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 * ":let var += expr" assignment command.
1862 * ":let var -= expr" assignment command.
1863 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 */
1866 void
1867ex_let(eap)
1868 exarg_T *eap;
1869{
1870 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 int var_count = 0;
1875 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 argend = skip_var_list(arg, &var_count, &semicolon);
1881 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001883 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1884 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001885 expr = skipwhite(argend);
1886 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1887 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001889 /*
1890 * ":let" without "=": list variables
1891 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 if (*arg == '[')
1893 EMSG(_(e_invarg));
1894 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_glob_vars(&first);
1901 list_buf_vars(&first);
1902 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_script_vars(&first);
1907 list_func_vars(&first);
1908 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 eap->nextcmd = check_nextcmd(arg);
1911 }
1912 else
1913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 op[0] = '=';
1915 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1919 op[0] = *expr; /* +=, -= or .= */
1920 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001922 else
1923 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (eap->skip)
1929 {
1930 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 --emsg_skip;
1933 }
1934 else if (i != FAIL)
1935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 }
1941}
1942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943/*
1944 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1945 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 * When "nextchars" is not NULL it points to a string with characters that
1947 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1948 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 * Returns OK or FAIL;
1950 */
1951 static int
1952ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1953 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001954 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 int copy; /* copy values from "tv", don't move */
1956 int semicolon; /* from skip_var_list() */
1957 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959{
1960 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 listitem_T *item;
1964 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
1966 if (*arg != '[')
1967 {
1968 /*
1969 * ":let var = expr" or ":for var in list"
1970 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 return FAIL;
1973 return OK;
1974 }
1975
1976 /*
1977 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1978 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001979 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
1981 EMSG(_(e_listreq));
1982 return FAIL;
1983 }
1984
1985 i = list_len(l);
1986 if (semicolon == 0 && var_count < i)
1987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001988 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 }
1991 if (var_count - semicolon > i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996
1997 item = l->lv_first;
1998 while (*arg != ']')
1999 {
2000 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 item = item->li_next;
2003 if (arg == NULL)
2004 return FAIL;
2005
2006 arg = skipwhite(arg);
2007 if (*arg == ';')
2008 {
2009 /* Put the rest of the list (may be empty) in the var after ';'.
2010 * Create a new list for this. */
2011 l = list_alloc();
2012 if (l == NULL)
2013 return FAIL;
2014 while (item != NULL)
2015 {
2016 list_append_tv(l, &item->li_tv);
2017 item = item->li_next;
2018 }
2019
2020 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002021 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 ltv.vval.v_list = l;
2023 l->lv_refcount = 1;
2024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2026 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 clear_tv(&ltv);
2028 if (arg == NULL)
2029 return FAIL;
2030 break;
2031 }
2032 else if (*arg != ',' && *arg != ']')
2033 {
2034 EMSG2(_(e_intern2), "ex_let_vars()");
2035 return FAIL;
2036 }
2037 }
2038
2039 return OK;
2040}
2041
2042/*
2043 * Skip over assignable variable "var" or list of variables "[var, var]".
2044 * Used for ":let varvar = expr" and ":for varvar in expr".
2045 * For "[var, var]" increment "*var_count" for each variable.
2046 * for "[var, var; var]" set "semicolon".
2047 * Return NULL for an error.
2048 */
2049 static char_u *
2050skip_var_list(arg, var_count, semicolon)
2051 char_u *arg;
2052 int *var_count;
2053 int *semicolon;
2054{
2055 char_u *p, *s;
2056
2057 if (*arg == '[')
2058 {
2059 /* "[var, var]": find the matching ']'. */
2060 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002061 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 {
2063 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2064 s = skip_var_one(p);
2065 if (s == p)
2066 {
2067 EMSG2(_(e_invarg2), p);
2068 return NULL;
2069 }
2070 ++*var_count;
2071
2072 p = skipwhite(s);
2073 if (*p == ']')
2074 break;
2075 else if (*p == ';')
2076 {
2077 if (*semicolon == 1)
2078 {
2079 EMSG(_("Double ; in list of variables"));
2080 return NULL;
2081 }
2082 *semicolon = 1;
2083 }
2084 else if (*p != ',')
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 }
2090 return p + 1;
2091 }
2092 else
2093 return skip_var_one(arg);
2094}
2095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002097 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100 static char_u *
2101skip_var_one(arg)
2102 char_u *arg;
2103{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002104 if (*arg == '@' && arg[1] != NUL)
2105 return arg + 2;
2106 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2107 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002108}
2109
Bram Moolenaara7043832005-01-21 11:56:39 +00002110/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 * List variables for hashtab "ht" with prefix "prefix".
2112 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120{
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashitem_T *hi;
2122 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123 int todo;
2124
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002125 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002126 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2127 {
2128 if (!HASHITEM_EMPTY(hi))
2129 {
2130 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 di = HI2DI(hi);
2132 if (empty || di->di_tv.v_type != VAR_STRING
2133 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 }
2136 }
2137}
2138
2139/*
2140 * List global variables.
2141 */
2142 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143list_glob_vars(first)
2144 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002147}
2148
2149/*
2150 * List buffer variables.
2151 */
2152 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153list_buf_vars(first)
2154 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156 char_u numbuf[NUMBUFLEN];
2157
Bram Moolenaar429fa852013-04-15 12:27:36 +02002158 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160
2161 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2163 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
2166/*
2167 * List window variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_win_vars(first)
2171 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002173 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002177#ifdef FEAT_WINDOWS
2178/*
2179 * List tab page variables.
2180 */
2181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_tab_vars(first)
2183 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002184{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187}
2188#endif
2189
Bram Moolenaara7043832005-01-21 11:56:39 +00002190/*
2191 * List Vim variables.
2192 */
2193 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194list_vim_vars(first)
2195 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198}
2199
2200/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201 * List script-local variables, if there is a script.
2202 */
2203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_script_vars(first)
2205 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206{
2207 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2209 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210}
2211
2212/*
2213 * List function variables, if there is a function.
2214 */
2215 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216list_func_vars(first)
2217 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218{
2219 if (current_funccal != NULL)
2220 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222}
2223
2224/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 * List variables in "arg".
2226 */
2227 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 exarg_T *eap;
2230 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232{
2233 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 char_u *name_start;
2237 char_u *arg_subsc;
2238 char_u *tofree;
2239 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240
2241 while (!ends_excmd(*arg) && !got_int)
2242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002245 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2247 {
2248 emsg_severe = TRUE;
2249 EMSG(_(e_trailing));
2250 break;
2251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 /* get_name_len() takes care of expanding curly braces */
2256 name_start = name = arg;
2257 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2258 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 /* This is mainly to keep test 49 working: when expanding
2261 * curly braces fails overrule the exception error message. */
2262 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 emsg_severe = TRUE;
2265 EMSG2(_(e_invarg2), arg);
2266 break;
2267 }
2268 error = TRUE;
2269 }
2270 else
2271 {
2272 if (tofree != NULL)
2273 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002274 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 else
2277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 /* handle d.key, l[idx], f(expr) */
2279 arg_subsc = arg;
2280 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 case 'g': list_glob_vars(first); break;
2289 case 'b': list_buf_vars(first); break;
2290 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'v': list_vim_vars(first); break;
2295 case 's': list_script_vars(first); break;
2296 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 default:
2298 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 }
2301 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 {
2303 char_u numbuf[NUMBUFLEN];
2304 char_u *tf;
2305 int c;
2306 char_u *s;
2307
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002308 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 c = *arg;
2310 *arg = NUL;
2311 list_one_var_a((char_u *)"",
2312 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 tv.v_type,
2314 s == NULL ? (char_u *)"" : s,
2315 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 *arg = c;
2317 vim_free(tf);
2318 }
2319 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
2322 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323
2324 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326
2327 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
2329
2330 return arg;
2331}
2332
2333/*
2334 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2335 * Returns a pointer to the char just after the var name.
2336 * Returns NULL if there is an error.
2337 */
2338 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002341 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002343 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345{
2346 int c1;
2347 char_u *name;
2348 char_u *p;
2349 char_u *arg_end = NULL;
2350 int len;
2351 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353
2354 /*
2355 * ":let $VAR = expr": Set environment variable.
2356 */
2357 if (*arg == '$')
2358 {
2359 /* Find the end of the name. */
2360 ++arg;
2361 name = arg;
2362 len = get_env_len(&arg);
2363 if (len == 0)
2364 EMSG2(_(e_invarg2), name - 1);
2365 else
2366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 if (op != NULL && (*op == '+' || *op == '-'))
2368 EMSG2(_(e_letwrong), op);
2369 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002372 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 {
2374 c1 = name[len];
2375 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 p = get_tv_string_chk(tv);
2377 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 {
2379 int mustfree = FALSE;
2380 char_u *s = vim_getenv(name, &mustfree);
2381
2382 if (s != NULL)
2383 {
2384 p = tofree = concat_str(s, p);
2385 if (mustfree)
2386 vim_free(s);
2387 }
2388 }
2389 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 if (STRICMP(name, "HOME") == 0)
2393 init_homedir();
2394 else if (didset_vim && STRICMP(name, "VIM") == 0)
2395 didset_vim = FALSE;
2396 else if (didset_vimruntime
2397 && STRICMP(name, "VIMRUNTIME") == 0)
2398 didset_vimruntime = FALSE;
2399 arg_end = arg;
2400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 }
2404 }
2405 }
2406
2407 /*
2408 * ":let &option = expr": Set option value.
2409 * ":let &l:option = expr": Set local option value.
2410 * ":let &g:option = expr": Set global option value.
2411 */
2412 else if (*arg == '&')
2413 {
2414 /* Find the end of the name. */
2415 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002416 if (p == NULL || (endchars != NULL
2417 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 EMSG(_(e_letunexp));
2419 else
2420 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 long n;
2422 int opt_type;
2423 long numval;
2424 char_u *stringval = NULL;
2425 char_u *s;
2426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 c1 = *p;
2428 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429
2430 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 s = get_tv_string_chk(tv); /* != NULL if number or string */
2432 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 {
2434 opt_type = get_option_value(arg, &numval,
2435 &stringval, opt_flags);
2436 if ((opt_type == 1 && *op == '.')
2437 || (opt_type == 0 && *op != '.'))
2438 EMSG2(_(e_letwrong), op);
2439 else
2440 {
2441 if (opt_type == 1) /* number */
2442 {
2443 if (*op == '+')
2444 n = numval + n;
2445 else
2446 n = numval - n;
2447 }
2448 else if (opt_type == 0 && stringval != NULL) /* string */
2449 {
2450 s = concat_str(stringval, s);
2451 vim_free(stringval);
2452 stringval = s;
2453 }
2454 }
2455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (s != NULL)
2457 {
2458 set_option_value(arg, n, s, opt_flags);
2459 arg_end = p;
2460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 }
2464 }
2465
2466 /*
2467 * ":let @r = expr": Set register contents.
2468 */
2469 else if (*arg == '@')
2470 {
2471 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (op != NULL && (*op == '+' || *op == '-'))
2473 EMSG2(_(e_letwrong), op);
2474 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002479 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 char_u *s;
2481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 p = get_tv_string_chk(tv);
2483 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002485 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (s != NULL)
2487 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 vim_free(s);
2490 }
2491 }
2492 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 arg_end = arg + 1;
2496 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
2499 }
2500
2501 /*
2502 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002505 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002509 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2513 EMSG(_(e_letunexp));
2514 else
2515 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 arg_end = p;
2518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
2522
2523 else
2524 EMSG2(_(e_invarg2), arg);
2525
2526 return arg_end;
2527}
2528
2529/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2531 */
2532 static int
2533check_changedtick(arg)
2534 char_u *arg;
2535{
2536 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2537 {
2538 EMSG2(_(e_readonlyvar), arg);
2539 return TRUE;
2540 }
2541 return FALSE;
2542}
2543
2544/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Get an lval: variable, Dict item or List item that can be assigned a value
2546 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2547 * "name.key", "name.key[expr]" etc.
2548 * Indexing only works if "name" is an existing List or Dictionary.
2549 * "name" points to the start of the name.
2550 * If "rettv" is not NULL it points to the value to be assigned.
2551 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2552 * wrong; must end in space or cmd separator.
2553 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002554 * flags:
2555 * GLV_QUIET: do not give error messages
2556 * GLV_NO_AUTOLOAD: do not use script autoloading
2557 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002559 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 */
2562 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 typval_T *rettv;
2566 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 int unlet;
2568 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002570 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 char_u *expr_start, *expr_end;
2574 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 dictitem_T *v;
2576 typval_T var1;
2577 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002583 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587
2588 if (skip)
2589 {
2590 /* When skipping just find the end of the name. */
2591 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002592 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 }
2594
2595 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002596 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (expr_start != NULL)
2598 {
2599 /* Don't expand the name when we already know there is an error. */
2600 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2601 && *p != '[' && *p != '.')
2602 {
2603 EMSG(_(e_trailing));
2604 return NULL;
2605 }
2606
2607 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2608 if (lp->ll_exp_name == NULL)
2609 {
2610 /* Report an invalid expression in braces, unless the
2611 * expression evaluation has been cancelled due to an
2612 * aborting error, an interrupt, or an exception. */
2613 if (!aborting() && !quiet)
2614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002615 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 EMSG2(_(e_invarg2), name);
2617 return NULL;
2618 }
2619 }
2620 lp->ll_name = lp->ll_exp_name;
2621 }
2622 else
2623 lp->ll_name = name;
2624
2625 /* Without [idx] or .key we are done. */
2626 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2627 return p;
2628
2629 cc = *p;
2630 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002631 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (v == NULL && !quiet)
2633 EMSG2(_(e_undefvar), lp->ll_name);
2634 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 if (v == NULL)
2636 return NULL;
2637
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 /*
2639 * Loop until no more [idx] or .key is following.
2640 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2645 && !(lp->ll_tv->v_type == VAR_DICT
2646 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E689: Can only index a List or Dictionary"));
2650 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
2655 EMSG(_("E708: [:] must come last"));
2656 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 len = -1;
2660 if (*p == '.')
2661 {
2662 key = p + 1;
2663 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2664 ;
2665 if (len == 0)
2666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_(e_emptykey));
2669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 p = key + len;
2672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 else
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (*p == ':')
2678 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 else
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 empty1 = FALSE;
2682 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002684 if (get_tv_string_chk(&var1) == NULL)
2685 {
2686 /* not a number or string */
2687 clear_tv(&var1);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691
2692 /* Optionally get the second index [ :expr]. */
2693 if (*p == ':')
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (rettv != NULL && (rettv->v_type != VAR_LIST
2704 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
2707 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712 p = skipwhite(p + 1);
2713 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 else
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002724 if (get_tv_string_chk(&var2) == NULL)
2725 {
2726 /* not a number or string */
2727 if (!empty1)
2728 clear_tv(&var1);
2729 clear_tv(&var2);
2730 return NULL;
2731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 if (!empty1)
2743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
2748
2749 /* Skip to past ']'. */
2750 ++p;
2751 }
2752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 {
2755 if (len == -1)
2756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002758 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 if (*key == NUL)
2760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
2762 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 lp->ll_list = NULL;
2768 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002769 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002770
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 /* When assigning to a scope dictionary check that a function and
2772 * variable name is valid (only variable name unless it is l: or
2773 * g: dictionary). Disallow overwriting a builtin function. */
2774 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 int prevval;
2777 int wrong;
2778
2779 if (len != -1)
2780 {
2781 prevval = key[len];
2782 key[len] = NUL;
2783 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002784 else
2785 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2787 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 || !valid_varname(key);
2790 if (len != -1)
2791 key[len] = prevval;
2792 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 return NULL;
2794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 /* Can't add "v:" variable. */
2799 if (lp->ll_dict == &vimvardict)
2800 {
2801 EMSG2(_(e_illvar), name);
2802 return NULL;
2803 }
2804
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002805 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 if (len == -1)
2811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 }
2814 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 if (len == -1)
2819 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 p = NULL;
2822 break;
2823 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002825 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 return NULL;
2827
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832 else
2833 {
2834 /*
2835 * Get the number and item for the only or first index of the List.
2836 */
2837 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 else
2840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var1);
2843 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_list = lp->ll_tv->vval.v_list;
2846 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2847 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002849 if (lp->ll_n1 < 0)
2850 {
2851 lp->ll_n1 = 0;
2852 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2853 }
2854 }
2855 if (lp->ll_li == NULL)
2856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 if (!quiet)
2860 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 }
2863
2864 /*
2865 * May need to find the item or absolute index for the second
2866 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 * When no index given: "lp->ll_empty2" is TRUE.
2868 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002872 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 {
2879 if (!quiet)
2880 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2887 if (lp->ll_n1 < 0)
2888 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2889 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002895 }
2896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 return p;
2902}
2903
2904/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 */
2907 static void
2908clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910{
2911 vim_free(lp->ll_exp_name);
2912 vim_free(lp->ll_newkey);
2913}
2914
2915/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 */
2920 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927{
2928 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 listitem_T *ri;
2930 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931
2932 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 cc = *endp;
2937 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 if (op != NULL && *op != '=')
2939 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002944 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 if ((di == NULL
2948 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2949 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2950 FALSE)))
2951 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 set_var(lp->ll_name, &tv, FALSE);
2953 clear_tv(&tv);
2954 }
2955 }
2956 else
2957 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 else if (tv_check_lock(lp->ll_newkey == NULL
2962 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002963 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 else if (lp->ll_range)
2966 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002967 listitem_T *ll_li = lp->ll_li;
2968 int ll_n1 = lp->ll_n1;
2969
2970 /*
2971 * Check whether any of the list items is locked
2972 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002973 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002975 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 return;
2977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2979 break;
2980 ll_li = ll_li->li_next;
2981 ++ll_n1;
2982 }
2983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 /*
2985 * Assign the List values to the list items.
2986 */
2987 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 if (op != NULL && *op != '=')
2990 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2991 else
2992 {
2993 clear_tv(&lp->ll_li->li_tv);
2994 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2995 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 ri = ri->li_next;
2997 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2998 break;
2999 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003002 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 ri = NULL;
3005 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 lp->ll_li = lp->ll_li->li_next;
3009 ++lp->ll_n1;
3010 }
3011 if (ri != NULL)
3012 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 else if (lp->ll_empty2
3014 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 : lp->ll_n1 != lp->ll_n2)
3016 EMSG(_("E711: List value has not enough items"));
3017 }
3018 else
3019 {
3020 /*
3021 * Assign to a List or Dictionary item.
3022 */
3023 if (lp->ll_newkey != NULL)
3024 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 if (op != NULL && *op != '=')
3026 {
3027 EMSG2(_(e_letwrong), op);
3028 return;
3029 }
3030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003032 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 if (di == NULL)
3034 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003035 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3036 {
3037 vim_free(di);
3038 return;
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003041 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 else if (op != NULL && *op != '=')
3043 {
3044 tv_op(lp->ll_tv, rettv, op);
3045 return;
3046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /*
3051 * Assign the value to the variable or list item.
3052 */
3053 if (copy)
3054 copy_tv(rettv, lp->ll_tv);
3055 else
3056 {
3057 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003058 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060 }
3061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062}
3063
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3066 * Returns OK or FAIL.
3067 */
3068 static int
3069tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 typval_T *tv1;
3071 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 char_u *op;
3073{
3074 long n;
3075 char_u numbuf[NUMBUFLEN];
3076 char_u *s;
3077
3078 /* Can't do anything with a Funcref or a Dict on the right. */
3079 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3080 {
3081 switch (tv1->v_type)
3082 {
3083 case VAR_DICT:
3084 case VAR_FUNC:
3085 break;
3086
3087 case VAR_LIST:
3088 if (*op != '+' || tv2->v_type != VAR_LIST)
3089 break;
3090 /* List += List */
3091 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3092 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3093 return OK;
3094
3095 case VAR_NUMBER:
3096 case VAR_STRING:
3097 if (tv2->v_type == VAR_LIST)
3098 break;
3099 if (*op == '+' || *op == '-')
3100 {
3101 /* nr += nr or nr -= nr*/
3102 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#ifdef FEAT_FLOAT
3104 if (tv2->v_type == VAR_FLOAT)
3105 {
3106 float_T f = n;
3107
3108 if (*op == '+')
3109 f += tv2->vval.v_float;
3110 else
3111 f -= tv2->vval.v_float;
3112 clear_tv(tv1);
3113 tv1->v_type = VAR_FLOAT;
3114 tv1->vval.v_float = f;
3115 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#endif
3118 {
3119 if (*op == '+')
3120 n += get_tv_number(tv2);
3121 else
3122 n -= get_tv_number(tv2);
3123 clear_tv(tv1);
3124 tv1->v_type = VAR_NUMBER;
3125 tv1->vval.v_number = n;
3126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 }
3128 else
3129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130 if (tv2->v_type == VAR_FLOAT)
3131 break;
3132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 /* str .= str */
3134 s = get_tv_string(tv1);
3135 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_STRING;
3138 tv1->vval.v_string = s;
3139 }
3140 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141
3142#ifdef FEAT_FLOAT
3143 case VAR_FLOAT:
3144 {
3145 float_T f;
3146
3147 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3148 && tv2->v_type != VAR_NUMBER
3149 && tv2->v_type != VAR_STRING))
3150 break;
3151 if (tv2->v_type == VAR_FLOAT)
3152 f = tv2->vval.v_float;
3153 else
3154 f = get_tv_number(tv2);
3155 if (*op == '+')
3156 tv1->vval.v_float += f;
3157 else
3158 tv1->vval.v_float -= f;
3159 }
3160 return OK;
3161#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003162 }
3163 }
3164
3165 EMSG2(_(e_letwrong), op);
3166 return FAIL;
3167}
3168
3169/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 * Add a watcher to a list.
3171 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003172 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 list_T *l;
3175 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176{
3177 lw->lw_next = l->lv_watch;
3178 l->lv_watch = lw;
3179}
3180
3181/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003182 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * No warning when it isn't found...
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 list_T *l;
3188 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 lwp = &l->lv_watch;
3193 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3194 {
3195 if (lw == lwrem)
3196 {
3197 *lwp = lw->lw_next;
3198 break;
3199 }
3200 lwp = &lw->lw_next;
3201 }
3202}
3203
3204/*
3205 * Just before removing an item from a list: advance watchers to the next
3206 * item.
3207 */
3208 static void
3209list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 list_T *l;
3211 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3216 if (lw->lw_item == item)
3217 lw->lw_item = item->li_next;
3218}
3219
3220/*
3221 * Evaluate the expression used in a ":for var in expr" command.
3222 * "arg" points to "var".
3223 * Set "*errp" to TRUE for an error, FALSE otherwise;
3224 * Return a pointer that holds the info. Null when there is an error.
3225 */
3226 void *
3227eval_for_line(arg, errp, nextcmdp, skip)
3228 char_u *arg;
3229 int *errp;
3230 char_u **nextcmdp;
3231 int skip;
3232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T tv;
3236 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 *errp = TRUE; /* default: there is an error */
3239
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 if (fi == NULL)
3242 return NULL;
3243
3244 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3245 if (expr == NULL)
3246 return fi;
3247
3248 expr = skipwhite(expr);
3249 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003251 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 return fi;
3253 }
3254
3255 if (skip)
3256 ++emsg_skip;
3257 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3258 {
3259 *errp = FALSE;
3260 if (!skip)
3261 {
3262 l = tv.vval.v_list;
3263 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 clear_tv(&tv);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 else
3269 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003270 /* No need to increment the refcount, it's already set for the
3271 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 fi->fi_list = l;
3273 list_add_watch(l, &fi->fi_lw);
3274 fi->fi_lw.lw_item = l->lv_first;
3275 }
3276 }
3277 }
3278 if (skip)
3279 --emsg_skip;
3280
3281 return fi;
3282}
3283
3284/*
3285 * Use the first item in a ":for" list. Advance to the next.
3286 * Assign the values to the variable (list). "arg" points to the first one.
3287 * Return TRUE when a valid item was found, FALSE when at end of list or
3288 * something wrong.
3289 */
3290 int
3291next_for_item(fi_void, arg)
3292 void *fi_void;
3293 char_u *arg;
3294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
3299 item = fi->fi_lw.lw_item;
3300 if (item == NULL)
3301 result = FALSE;
3302 else
3303 {
3304 fi->fi_lw.lw_item = item->li_next;
3305 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3306 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3307 }
3308 return result;
3309}
3310
3311/*
3312 * Free the structure used to store info used by ":for".
3313 */
3314 void
3315free_for_info(fi_void)
3316 void *fi_void;
3317{
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003320 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 list_unref(fi->fi_list);
3324 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 vim_free(fi);
3326}
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3329
3330 void
3331set_context_for_expression(xp, arg, cmdidx)
3332 expand_T *xp;
3333 char_u *arg;
3334 cmdidx_T cmdidx;
3335{
3336 int got_eq = FALSE;
3337 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 if (cmdidx == CMD_let)
3341 {
3342 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003343 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 {
3345 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003346 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 {
3348 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003349 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 if (vim_iswhite(*p))
3351 break;
3352 }
3353 return;
3354 }
3355 }
3356 else
3357 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3358 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 while ((xp->xp_pattern = vim_strpbrk(arg,
3360 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3361 {
3362 c = *xp->xp_pattern;
3363 if (c == '&')
3364 {
3365 c = xp->xp_pattern[1];
3366 if (c == '&')
3367 {
3368 ++xp->xp_pattern;
3369 xp->xp_context = cmdidx != CMD_let || got_eq
3370 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3371 }
3372 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3376 xp->xp_pattern += 2;
3377
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 else if (c == '$')
3381 {
3382 /* environment variable */
3383 xp->xp_context = EXPAND_ENV_VARS;
3384 }
3385 else if (c == '=')
3386 {
3387 got_eq = TRUE;
3388 xp->xp_context = EXPAND_EXPRESSION;
3389 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003390 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 && xp->xp_context == EXPAND_FUNCTIONS
3392 && vim_strchr(xp->xp_pattern, '(') == NULL)
3393 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 break;
3396 }
3397 else if (cmdidx != CMD_let || got_eq)
3398 {
3399 if (c == '"') /* string */
3400 {
3401 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3402 if (c == '\\' && xp->xp_pattern[1] != NUL)
3403 ++xp->xp_pattern;
3404 xp->xp_context = EXPAND_NOTHING;
3405 }
3406 else if (c == '\'') /* literal string */
3407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003408 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3410 /* skip */ ;
3411 xp->xp_context = EXPAND_NOTHING;
3412 }
3413 else if (c == '|')
3414 {
3415 if (xp->xp_pattern[1] == '|')
3416 {
3417 ++xp->xp_pattern;
3418 xp->xp_context = EXPAND_EXPRESSION;
3419 }
3420 else
3421 xp->xp_context = EXPAND_COMMANDS;
3422 }
3423 else
3424 xp->xp_context = EXPAND_EXPRESSION;
3425 }
3426 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003427 /* Doesn't look like something valid, expand as an expression
3428 * anyway. */
3429 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 arg = xp->xp_pattern;
3431 if (*arg != NUL)
3432 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3433 /* skip */ ;
3434 }
3435 xp->xp_pattern = arg;
3436}
3437
3438#endif /* FEAT_CMDL_COMPL */
3439
3440/*
3441 * ":1,25call func(arg1, arg2)" function call.
3442 */
3443 void
3444ex_call(eap)
3445 exarg_T *eap;
3446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003458 if (eap->skip)
3459 {
3460 /* trans_function_name() doesn't work well when skipping, use eval0()
3461 * instead to skip to any following command, e.g. for:
3462 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003463 ++emsg_skip;
3464 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3465 clear_tv(&rettv);
3466 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 return;
3468 }
3469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003470 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003471 if (fudi.fd_newkey != NULL)
3472 {
3473 /* Still need to give an error message for missing key. */
3474 EMSG2(_(e_dictkey), fudi.fd_newkey);
3475 vim_free(fudi.fd_newkey);
3476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 if (tofree == NULL)
3478 return;
3479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 /* Increase refcount on dictionary, it could get deleted when evaluating
3481 * the arguments. */
3482 if (fudi.fd_dict != NULL)
3483 ++fudi.fd_dict->dv_refcount;
3484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003487 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 /* Skip white space to allow ":call func ()". Not good, but required for
3490 * backward compatibility. */
3491 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 if (*startarg != '(')
3495 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003496 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 goto end;
3498 }
3499
3500 /*
3501 * When skipping, evaluate the function once, to find the end of the
3502 * arguments.
3503 * When the function takes a range, this is discovered after the first
3504 * call, and the loop is broken.
3505 */
3506 if (eap->skip)
3507 {
3508 ++emsg_skip;
3509 lnum = eap->line2; /* do it once, also with an invalid range */
3510 }
3511 else
3512 lnum = eap->line1;
3513 for ( ; lnum <= eap->line2; ++lnum)
3514 {
3515 if (!eap->skip && eap->addr_count > 0)
3516 {
3517 curwin->w_cursor.lnum = lnum;
3518 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003519#ifdef FEAT_VIRTUALEDIT
3520 curwin->w_cursor.coladd = 0;
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003525 eap->line1, eap->line2, &doesrange,
3526 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 failed = TRUE;
3529 break;
3530 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
3532 /* Handle a function returning a Funcref, Dictionary or List. */
3533 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3534 {
3535 failed = TRUE;
3536 break;
3537 }
3538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (doesrange || eap->skip)
3541 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (aborting())
3548 break;
3549 }
3550 if (eap->skip)
3551 --emsg_skip;
3552
3553 if (!failed)
3554 {
3555 /* Check for trailing illegal characters and a following command. */
3556 if (!ends_excmd(*arg))
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 else
3562 eap->nextcmd = check_nextcmd(arg);
3563 }
3564
3565end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003566 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570/*
3571 * ":unlet[!] var1 ... " command.
3572 */
3573 void
3574ex_unlet(eap)
3575 exarg_T *eap;
3576{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 ex_unletlock(eap, eap->arg, 0);
3578}
3579
3580/*
3581 * ":lockvar" and ":unlockvar" commands
3582 */
3583 void
3584ex_lockvar(eap)
3585 exarg_T *eap;
3586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588 int deep = 2;
3589
3590 if (eap->forceit)
3591 deep = -1;
3592 else if (vim_isdigit(*arg))
3593 {
3594 deep = getdigits(&arg);
3595 arg = skipwhite(arg);
3596 }
3597
3598 ex_unletlock(eap, arg, deep);
3599}
3600
3601/*
3602 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3603 */
3604 static void
3605ex_unletlock(eap, argstart, deep)
3606 exarg_T *eap;
3607 char_u *argstart;
3608 int deep;
3609{
3610 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 do
3616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003618 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003619 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 if (lv.ll_name == NULL)
3621 error = TRUE; /* error but continue parsing */
3622 if (name_end == NULL || (!vim_iswhite(*name_end)
3623 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 if (name_end != NULL)
3626 {
3627 emsg_severe = TRUE;
3628 EMSG(_(e_trailing));
3629 }
3630 if (!(eap->skip || error))
3631 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 break;
3633 }
3634
3635 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 {
3637 if (eap->cmdidx == CMD_unlet)
3638 {
3639 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3640 error = TRUE;
3641 }
3642 else
3643 {
3644 if (do_lock_var(&lv, name_end, deep,
3645 eap->cmdidx == CMD_lockvar) == FAIL)
3646 error = TRUE;
3647 }
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (!eap->skip)
3651 clear_lval(&lv);
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 arg = skipwhite(name_end);
3654 } while (!ends_excmd(*arg));
3655
3656 eap->nextcmd = check_nextcmd(arg);
3657}
3658
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 int forceit;
3664{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 int ret = OK;
3666 int cc;
3667
3668 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 cc = *name_end;
3671 *name_end = NUL;
3672
3673 /* Normal name or expanded name. */
3674 if (check_changedtick(lp->ll_name))
3675 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else if (lp->ll_range)
3686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688 listitem_T *ll_li = lp->ll_li;
3689 int ll_n1 = lp->ll_n1;
3690
3691 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3692 {
3693 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003694 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 return FAIL;
3696 ll_li = li;
3697 ++ll_n1;
3698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699
3700 /* Delete a range of List items. */
3701 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3702 {
3703 li = lp->ll_li->li_next;
3704 listitem_remove(lp->ll_list, lp->ll_li);
3705 lp->ll_li = li;
3706 ++lp->ll_n1;
3707 }
3708 }
3709 else
3710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 /* unlet a List item. */
3713 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003716 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 }
3718
3719 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722/*
3723 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003724 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003740 if (ht == &globvarht)
3741 d = &globvardict;
3742 else if (current_funccal != NULL
3743 && ht == &current_funccal->l_vars.dv_hashtab)
3744 d = &current_funccal->l_vars;
3745 else
3746 {
3747 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3748 d = di->di_tv.vval.v_dict;
3749 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003750 hi = hash_find(ht, varname);
3751 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003753 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003754 if (var_check_fixed(di->di_flags, name, FALSE)
3755 || var_check_ro(di->di_flags, name, FALSE)
3756 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757 return FAIL;
3758 delete_var(ht, hi);
3759 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 if (forceit)
3763 return OK;
3764 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766}
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768/*
3769 * Lock or unlock variable indicated by "lp".
3770 * "deep" is the levels to go (-1 for unlimited);
3771 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3772 */
3773 static int
3774do_lock_var(lp, name_end, deep, lock)
3775 lval_T *lp;
3776 char_u *name_end;
3777 int deep;
3778 int lock;
3779{
3780 int ret = OK;
3781 int cc;
3782 dictitem_T *di;
3783
3784 if (deep == 0) /* nothing to do */
3785 return OK;
3786
3787 if (lp->ll_tv == NULL)
3788 {
3789 cc = *name_end;
3790 *name_end = NUL;
3791
3792 /* Normal name or expanded name. */
3793 if (check_changedtick(lp->ll_name))
3794 ret = FAIL;
3795 else
3796 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003797 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 if (di == NULL)
3799 ret = FAIL;
3800 else
3801 {
3802 if (lock)
3803 di->di_flags |= DI_FLAGS_LOCK;
3804 else
3805 di->di_flags &= ~DI_FLAGS_LOCK;
3806 item_lock(&di->di_tv, deep, lock);
3807 }
3808 }
3809 *name_end = cc;
3810 }
3811 else if (lp->ll_range)
3812 {
3813 listitem_T *li = lp->ll_li;
3814
3815 /* (un)lock a range of List items. */
3816 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3817 {
3818 item_lock(&li->li_tv, deep, lock);
3819 li = li->li_next;
3820 ++lp->ll_n1;
3821 }
3822 }
3823 else if (lp->ll_list != NULL)
3824 /* (un)lock a List item. */
3825 item_lock(&lp->ll_li->li_tv, deep, lock);
3826 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003827 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 item_lock(&lp->ll_di->di_tv, deep, lock);
3829
3830 return ret;
3831}
3832
3833/*
3834 * Lock or unlock an item. "deep" is nr of levels to go.
3835 */
3836 static void
3837item_lock(tv, deep, lock)
3838 typval_T *tv;
3839 int deep;
3840 int lock;
3841{
3842 static int recurse = 0;
3843 list_T *l;
3844 listitem_T *li;
3845 dict_T *d;
3846 hashitem_T *hi;
3847 int todo;
3848
3849 if (recurse >= DICT_MAXNEST)
3850 {
3851 EMSG(_("E743: variable nested too deep for (un)lock"));
3852 return;
3853 }
3854 if (deep == 0)
3855 return;
3856 ++recurse;
3857
3858 /* lock/unlock the item itself */
3859 if (lock)
3860 tv->v_lock |= VAR_LOCKED;
3861 else
3862 tv->v_lock &= ~VAR_LOCKED;
3863
3864 switch (tv->v_type)
3865 {
3866 case VAR_LIST:
3867 if ((l = tv->vval.v_list) != NULL)
3868 {
3869 if (lock)
3870 l->lv_lock |= VAR_LOCKED;
3871 else
3872 l->lv_lock &= ~VAR_LOCKED;
3873 if (deep < 0 || deep > 1)
3874 /* recursive: lock/unlock the items the List contains */
3875 for (li = l->lv_first; li != NULL; li = li->li_next)
3876 item_lock(&li->li_tv, deep - 1, lock);
3877 }
3878 break;
3879 case VAR_DICT:
3880 if ((d = tv->vval.v_dict) != NULL)
3881 {
3882 if (lock)
3883 d->dv_lock |= VAR_LOCKED;
3884 else
3885 d->dv_lock &= ~VAR_LOCKED;
3886 if (deep < 0 || deep > 1)
3887 {
3888 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003889 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003890 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3891 {
3892 if (!HASHITEM_EMPTY(hi))
3893 {
3894 --todo;
3895 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3896 }
3897 }
3898 }
3899 }
3900 }
3901 --recurse;
3902}
3903
Bram Moolenaara40058a2005-07-11 22:42:07 +00003904/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003905 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3906 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003907 */
3908 static int
3909tv_islocked(tv)
3910 typval_T *tv;
3911{
3912 return (tv->v_lock & VAR_LOCKED)
3913 || (tv->v_type == VAR_LIST
3914 && tv->vval.v_list != NULL
3915 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3916 || (tv->v_type == VAR_DICT
3917 && tv->vval.v_dict != NULL
3918 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3919}
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3922/*
3923 * Delete all "menutrans_" variables.
3924 */
3925 void
3926del_menutrans_vars()
3927{
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003932 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003934 {
3935 if (!HASHITEM_EMPTY(hi))
3936 {
3937 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3939 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 }
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943}
3944#endif
3945
3946#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3947
3948/*
3949 * Local string buffer for the next two functions to store a variable name
3950 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3951 * get_user_var_name().
3952 */
3953
3954static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3955
3956static char_u *varnamebuf = NULL;
3957static int varnamebuflen = 0;
3958
3959/*
3960 * Function to concatenate a prefix and a variable name.
3961 */
3962 static char_u *
3963cat_prefix_varname(prefix, name)
3964 int prefix;
3965 char_u *name;
3966{
3967 int len;
3968
3969 len = (int)STRLEN(name) + 3;
3970 if (len > varnamebuflen)
3971 {
3972 vim_free(varnamebuf);
3973 len += 10; /* some additional space */
3974 varnamebuf = alloc(len);
3975 if (varnamebuf == NULL)
3976 {
3977 varnamebuflen = 0;
3978 return NULL;
3979 }
3980 varnamebuflen = len;
3981 }
3982 *varnamebuf = prefix;
3983 varnamebuf[1] = ':';
3984 STRCPY(varnamebuf + 2, name);
3985 return varnamebuf;
3986}
3987
3988/*
3989 * Function given to ExpandGeneric() to obtain the list of user defined
3990 * (global/buffer/window/built-in) variable names.
3991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u *
3993get_user_var_name(xp, idx)
3994 expand_T *xp;
3995 int idx;
3996{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003997 static long_u gdone;
3998 static long_u bdone;
3999 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000#ifdef FEAT_WINDOWS
4001 static long_u tdone;
4002#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static int vidx;
4004 static hashitem_T *hi;
4005 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004008 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 tdone = 0;
4012#endif
4013 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004014
4015 /* Global variables */
4016 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004020 else
4021 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 while (HASHITEM_EMPTY(hi))
4023 ++hi;
4024 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4025 return cat_prefix_varname('g', hi->hi_key);
4026 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004028
4029 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004030 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004035 else
4036 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 while (HASHITEM_EMPTY(hi))
4038 ++hi;
4039 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
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 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return (char_u *)"b:changedtick";
4045 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004046
4047 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004051 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 else
4054 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004062 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063 if (tdone < ht->ht_used)
4064 {
4065 if (tdone++ == 0)
4066 hi = ht->ht_array;
4067 else
4068 ++hi;
4069 while (HASHITEM_EMPTY(hi))
4070 ++hi;
4071 return cat_prefix_varname('t', hi->hi_key);
4072 }
4073#endif
4074
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 /* v: variables */
4076 if (vidx < VV_LEN)
4077 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 vim_free(varnamebuf);
4080 varnamebuf = NULL;
4081 varnamebuflen = 0;
4082 return NULL;
4083}
4084
4085#endif /* FEAT_CMDL_COMPL */
4086
4087/*
4088 * types for expressions.
4089 */
4090typedef enum
4091{
4092 TYPE_UNKNOWN = 0
4093 , TYPE_EQUAL /* == */
4094 , TYPE_NEQUAL /* != */
4095 , TYPE_GREATER /* > */
4096 , TYPE_GEQUAL /* >= */
4097 , TYPE_SMALLER /* < */
4098 , TYPE_SEQUAL /* <= */
4099 , TYPE_MATCH /* =~ */
4100 , TYPE_NOMATCH /* !~ */
4101} exptype_T;
4102
4103/*
4104 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4107 */
4108
4109/*
4110 * Handle zero level expression.
4111 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004113 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * Return OK or FAIL.
4115 */
4116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 char_u **nextcmd;
4121 int evaluate;
4122{
4123 int ret;
4124 char_u *p;
4125
4126 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (ret == FAIL || !ends_excmd(*p))
4129 {
4130 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 /*
4133 * Report the invalid expression unless the expression evaluation has
4134 * been cancelled due to an aborting error, an interrupt, or an
4135 * exception.
4136 */
4137 if (!aborting())
4138 EMSG2(_(e_invexpr2), arg);
4139 ret = FAIL;
4140 }
4141 if (nextcmd != NULL)
4142 *nextcmd = check_nextcmd(p);
4143
4144 return ret;
4145}
4146
4147/*
4148 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004149 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 *
4151 * "arg" must point to the first non-white of the expression.
4152 * "arg" is advanced to the next non-white after the recognized expression.
4153 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004154 * Note: "rettv.v_lock" is not set.
4155 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 * Return OK or FAIL.
4157 */
4158 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int evaluate;
4163{
4164 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
4167 /*
4168 * Get the first variable.
4169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 return FAIL;
4172
4173 if ((*arg)[0] == '?')
4174 {
4175 result = FALSE;
4176 if (evaluate)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 int error = FALSE;
4179
4180 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (error)
4184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193
4194 /*
4195 * Check for the ":".
4196 */
4197 if ((*arg)[0] != ':')
4198 {
4199 EMSG(_("E109: Missing ':' after '?'"));
4200 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203 }
4204
4205 /*
4206 * Get the third variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
4209 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4210 {
4211 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214 }
4215 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 return OK;
4220}
4221
4222/*
4223 * Handle first level expression:
4224 * expr2 || expr2 || expr2 logical OR
4225 *
4226 * "arg" must point to the first non-white of the expression.
4227 * "arg" is advanced to the next non-white after the recognized expression.
4228 *
4229 * Return OK or FAIL.
4230 */
4231 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int evaluate;
4236{
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 long result;
4239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 /*
4249 * Repeat until there is no following "||".
4250 */
4251 first = TRUE;
4252 result = FALSE;
4253 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4254 {
4255 if (evaluate && first)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 first = FALSE;
4263 }
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 2);
4269 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4270 return FAIL;
4271
4272 /*
4273 * Compute the result.
4274 */
4275 if (evaluate && !result)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (evaluate)
4284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * Handle second level expression:
4295 * expr3 && expr3 && expr3 logical AND
4296 *
4297 * "arg" must point to the first non-white of the expression.
4298 * "arg" is advanced to the next non-white after the recognized expression.
4299 *
4300 * Return OK or FAIL.
4301 */
4302 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 int evaluate;
4307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 long result;
4310 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * Get the first variable.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
4318
4319 /*
4320 * Repeat until there is no following "&&".
4321 */
4322 first = TRUE;
4323 result = TRUE;
4324 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4325 {
4326 if (evaluate && first)
4327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 first = FALSE;
4334 }
4335
4336 /*
4337 * Get the second variable.
4338 */
4339 *arg = skipwhite(*arg + 2);
4340 if (eval4(arg, &var2, evaluate && result) == FAIL)
4341 return FAIL;
4342
4343 /*
4344 * Compute the result.
4345 */
4346 if (evaluate && result)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (evaluate)
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle third level expression:
4366 * var1 == var2
4367 * var1 =~ var2
4368 * var1 != var2
4369 * var1 !~ var2
4370 * var1 > var2
4371 * var1 >= var2
4372 * var1 < var2
4373 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 * var1 is var2
4375 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 int evaluate;
4387{
Bram Moolenaar33570922005-01-25 22:26:29 +00004388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 char_u *p;
4390 int i;
4391 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 int len = 2;
4394 long n1, n2;
4395 char_u *s1, *s2;
4396 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4397 regmatch_T regmatch;
4398 int ic;
4399 char_u *save_cpo;
4400
4401 /*
4402 * Get the first variable.
4403 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
4406
4407 p = *arg;
4408 switch (p[0])
4409 {
4410 case '=': if (p[1] == '=')
4411 type = TYPE_EQUAL;
4412 else if (p[1] == '~')
4413 type = TYPE_MATCH;
4414 break;
4415 case '!': if (p[1] == '=')
4416 type = TYPE_NEQUAL;
4417 else if (p[1] == '~')
4418 type = TYPE_NOMATCH;
4419 break;
4420 case '>': if (p[1] != '=')
4421 {
4422 type = TYPE_GREATER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_GEQUAL;
4427 break;
4428 case '<': if (p[1] != '=')
4429 {
4430 type = TYPE_SMALLER;
4431 len = 1;
4432 }
4433 else
4434 type = TYPE_SEQUAL;
4435 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 case 'i': if (p[1] == 's')
4437 {
4438 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4439 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004440 i = p[len];
4441 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 {
4443 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4444 type_is = TRUE;
4445 }
4446 }
4447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449
4450 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004451 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 */
4453 if (type != TYPE_UNKNOWN)
4454 {
4455 /* extra question mark appended: ignore case */
4456 if (p[len] == '?')
4457 {
4458 ic = TRUE;
4459 ++len;
4460 }
4461 /* extra '#' appended: match case */
4462 else if (p[len] == '#')
4463 {
4464 ic = FALSE;
4465 ++len;
4466 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004467 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 else
4469 ic = p_ic;
4470
4471 /*
4472 * Get the second variable.
4473 */
4474 *arg = skipwhite(p + len);
4475 if (eval5(arg, &var2, evaluate) == FAIL)
4476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return FAIL;
4479 }
4480
4481 if (evaluate)
4482 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 if (type_is && rettv->v_type != var2.v_type)
4484 {
4485 /* For "is" a different type always means FALSE, for "notis"
4486 * it means TRUE. */
4487 n1 = (type == TYPE_NEQUAL);
4488 }
4489 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4490 {
4491 if (type_is)
4492 {
4493 n1 = (rettv->v_type == var2.v_type
4494 && rettv->vval.v_list == var2.vval.v_list);
4495 if (type == TYPE_NEQUAL)
4496 n1 = !n1;
4497 }
4498 else if (rettv->v_type != var2.v_type
4499 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4500 {
4501 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004502 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004504 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 clear_tv(rettv);
4506 clear_tv(&var2);
4507 return FAIL;
4508 }
4509 else
4510 {
4511 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004512 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4513 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 if (type == TYPE_NEQUAL)
4515 n1 = !n1;
4516 }
4517 }
4518
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004519 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4520 {
4521 if (type_is)
4522 {
4523 n1 = (rettv->v_type == var2.v_type
4524 && rettv->vval.v_dict == var2.vval.v_dict);
4525 if (type == TYPE_NEQUAL)
4526 n1 = !n1;
4527 }
4528 else if (rettv->v_type != var2.v_type
4529 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4530 {
4531 if (rettv->v_type != var2.v_type)
4532 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4533 else
4534 EMSG(_("E736: Invalid operation for Dictionary"));
4535 clear_tv(rettv);
4536 clear_tv(&var2);
4537 return FAIL;
4538 }
4539 else
4540 {
4541 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004542 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4543 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004544 if (type == TYPE_NEQUAL)
4545 n1 = !n1;
4546 }
4547 }
4548
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4550 {
4551 if (rettv->v_type != var2.v_type
4552 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4553 {
4554 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004557 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 clear_tv(rettv);
4559 clear_tv(&var2);
4560 return FAIL;
4561 }
4562 else
4563 {
4564 /* Compare two Funcrefs for being equal or unequal. */
4565 if (rettv->vval.v_string == NULL
4566 || var2.vval.v_string == NULL)
4567 n1 = FALSE;
4568 else
4569 n1 = STRCMP(rettv->vval.v_string,
4570 var2.vval.v_string) == 0;
4571 if (type == TYPE_NEQUAL)
4572 n1 = !n1;
4573 }
4574 }
4575
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004576#ifdef FEAT_FLOAT
4577 /*
4578 * If one of the two variables is a float, compare as a float.
4579 * When using "=~" or "!~", always compare as string.
4580 */
4581 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4582 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4583 {
4584 float_T f1, f2;
4585
4586 if (rettv->v_type == VAR_FLOAT)
4587 f1 = rettv->vval.v_float;
4588 else
4589 f1 = get_tv_number(rettv);
4590 if (var2.v_type == VAR_FLOAT)
4591 f2 = var2.vval.v_float;
4592 else
4593 f2 = get_tv_number(&var2);
4594 n1 = FALSE;
4595 switch (type)
4596 {
4597 case TYPE_EQUAL: n1 = (f1 == f2); break;
4598 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4599 case TYPE_GREATER: n1 = (f1 > f2); break;
4600 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4601 case TYPE_SMALLER: n1 = (f1 < f2); break;
4602 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4603 case TYPE_UNKNOWN:
4604 case TYPE_MATCH:
4605 case TYPE_NOMATCH: break; /* avoid gcc warning */
4606 }
4607 }
4608#endif
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 /*
4611 * If one of the two variables is a number, compare as a number.
4612 * When using "=~" or "!~", always compare as string.
4613 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 n1 = get_tv_number(rettv);
4618 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 switch (type)
4620 {
4621 case TYPE_EQUAL: n1 = (n1 == n2); break;
4622 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4623 case TYPE_GREATER: n1 = (n1 > n2); break;
4624 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4625 case TYPE_SMALLER: n1 = (n1 < n2); break;
4626 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4627 case TYPE_UNKNOWN:
4628 case TYPE_MATCH:
4629 case TYPE_NOMATCH: break; /* avoid gcc warning */
4630 }
4631 }
4632 else
4633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004634 s1 = get_tv_string_buf(rettv, buf1);
4635 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4637 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4638 else
4639 i = 0;
4640 n1 = FALSE;
4641 switch (type)
4642 {
4643 case TYPE_EQUAL: n1 = (i == 0); break;
4644 case TYPE_NEQUAL: n1 = (i != 0); break;
4645 case TYPE_GREATER: n1 = (i > 0); break;
4646 case TYPE_GEQUAL: n1 = (i >= 0); break;
4647 case TYPE_SMALLER: n1 = (i < 0); break;
4648 case TYPE_SEQUAL: n1 = (i <= 0); break;
4649
4650 case TYPE_MATCH:
4651 case TYPE_NOMATCH:
4652 /* avoid 'l' flag in 'cpoptions' */
4653 save_cpo = p_cpo;
4654 p_cpo = (char_u *)"";
4655 regmatch.regprog = vim_regcomp(s2,
4656 RE_MAGIC + RE_STRING);
4657 regmatch.rm_ic = ic;
4658 if (regmatch.regprog != NULL)
4659 {
4660 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004661 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (type == TYPE_NOMATCH)
4663 n1 = !n1;
4664 }
4665 p_cpo = save_cpo;
4666 break;
4667
4668 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4669 }
4670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
4672 clear_tv(&var2);
4673 rettv->v_type = VAR_NUMBER;
4674 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
4676 }
4677
4678 return OK;
4679}
4680
4681/*
4682 * Handle fourth level expression:
4683 * + number addition
4684 * - number subtraction
4685 * . string concatenation
4686 *
4687 * "arg" must point to the first non-white of the expression.
4688 * "arg" is advanced to the next non-white after the recognized expression.
4689 *
4690 * Return OK or FAIL.
4691 */
4692 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 int evaluate;
4697{
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T var2;
4699 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 int op;
4701 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 float_T f1 = 0, f2 = 0;
4704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 char_u *s1, *s2;
4706 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4707 char_u *p;
4708
4709 /*
4710 * Get the first variable.
4711 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 return FAIL;
4714
4715 /*
4716 * Repeat computing, until no '+', '-' or '.' is following.
4717 */
4718 for (;;)
4719 {
4720 op = **arg;
4721 if (op != '+' && op != '-' && op != '.')
4722 break;
4723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 if ((op != '+' || rettv->v_type != VAR_LIST)
4725#ifdef FEAT_FLOAT
4726 && (op == '.' || rettv->v_type != VAR_FLOAT)
4727#endif
4728 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 {
4730 /* For "list + ...", an illegal use of the first operand as
4731 * a number cannot be determined before evaluating the 2nd
4732 * operand: if this is also a list, all is ok.
4733 * For "something . ...", "something - ..." or "non-list + ...",
4734 * we know that the first operand needs to be a string or number
4735 * without evaluating the 2nd operand. So check before to avoid
4736 * side effects after an error. */
4737 if (evaluate && get_tv_string_chk(rettv) == NULL)
4738 {
4739 clear_tv(rettv);
4740 return FAIL;
4741 }
4742 }
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /*
4745 * Get the second variable.
4746 */
4747 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004748 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return FAIL;
4752 }
4753
4754 if (evaluate)
4755 {
4756 /*
4757 * Compute the result.
4758 */
4759 if (op == '.')
4760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4762 s2 = get_tv_string_buf_chk(&var2, buf2);
4763 if (s2 == NULL) /* type error ? */
4764 {
4765 clear_tv(rettv);
4766 clear_tv(&var2);
4767 return FAIL;
4768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004769 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_STRING;
4772 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004774 else if (op == '+' && rettv->v_type == VAR_LIST
4775 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004776 {
4777 /* concatenate Lists */
4778 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4779 &var3) == FAIL)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785 clear_tv(rettv);
4786 *rettv = var3;
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
4789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 int error = FALSE;
4791
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792#ifdef FEAT_FLOAT
4793 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 f1 = rettv->vval.v_float;
4796 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 else
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 n1 = get_tv_number_chk(rettv, &error);
4802 if (error)
4803 {
4804 /* This can only happen for "list + non-list". For
4805 * "non-list + ..." or "something - ...", we returned
4806 * before evaluating the 2nd operand. */
4807 clear_tv(rettv);
4808 return FAIL;
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 f1 = n1;
4813#endif
4814 }
4815#ifdef FEAT_FLOAT
4816 if (var2.v_type == VAR_FLOAT)
4817 {
4818 f2 = var2.vval.v_float;
4819 n2 = 0;
4820 }
4821 else
4822#endif
4823 {
4824 n2 = get_tv_number_chk(&var2, &error);
4825 if (error)
4826 {
4827 clear_tv(rettv);
4828 clear_tv(&var2);
4829 return FAIL;
4830 }
4831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 f2 = n2;
4834#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837
4838#ifdef FEAT_FLOAT
4839 /* If there is a float on either side the result is a float. */
4840 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4841 {
4842 if (op == '+')
4843 f1 = f1 + f2;
4844 else
4845 f1 = f1 - f2;
4846 rettv->v_type = VAR_FLOAT;
4847 rettv->vval.v_float = f1;
4848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#endif
4851 {
4852 if (op == '+')
4853 n1 = n1 + n2;
4854 else
4855 n1 = n1 - n2;
4856 rettv->v_type = VAR_NUMBER;
4857 rettv->vval.v_number = n1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 }
4863 return OK;
4864}
4865
4866/*
4867 * Handle fifth level expression:
4868 * * number multiplication
4869 * / number division
4870 * % number modulo
4871 *
4872 * "arg" must point to the first non-white of the expression.
4873 * "arg" is advanced to the next non-white after the recognized expression.
4874 *
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004878eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004882 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 int op;
4886 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887#ifdef FEAT_FLOAT
4888 int use_float = FALSE;
4889 float_T f1 = 0, f2;
4890#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Get the first variable.
4895 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004896 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
4898
4899 /*
4900 * Repeat computing, until no '*', '/' or '%' is following.
4901 */
4902 for (;;)
4903 {
4904 op = **arg;
4905 if (op != '*' && op != '/' && op != '%')
4906 break;
4907
4908 if (evaluate)
4909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910#ifdef FEAT_FLOAT
4911 if (rettv->v_type == VAR_FLOAT)
4912 {
4913 f1 = rettv->vval.v_float;
4914 use_float = TRUE;
4915 n1 = 0;
4916 }
4917 else
4918#endif
4919 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 if (error)
4922 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 else
4925 n1 = 0;
4926
4927 /*
4928 * Get the second variable.
4929 */
4930 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004931 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
4933
4934 if (evaluate)
4935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#ifdef FEAT_FLOAT
4937 if (var2.v_type == VAR_FLOAT)
4938 {
4939 if (!use_float)
4940 {
4941 f1 = n1;
4942 use_float = TRUE;
4943 }
4944 f2 = var2.vval.v_float;
4945 n2 = 0;
4946 }
4947 else
4948#endif
4949 {
4950 n2 = get_tv_number_chk(&var2, &error);
4951 clear_tv(&var2);
4952 if (error)
4953 return FAIL;
4954#ifdef FEAT_FLOAT
4955 if (use_float)
4956 f2 = n2;
4957#endif
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 /*
4961 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#ifdef FEAT_FLOAT
4965 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 if (op == '*')
4968 f1 = f1 * f2;
4969 else if (op == '/')
4970 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971# ifdef VMS
4972 /* VMS crashes on divide by zero, work around it */
4973 if (f2 == 0.0)
4974 {
4975 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004976 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004978 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 }
4982 else
4983 f1 = f1 / f2;
4984# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 /* We rely on the floating point library to handle divide
4986 * by zero to result in "inf" and not a crash. */
4987 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004992 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 return FAIL;
4994 }
4995 rettv->v_type = VAR_FLOAT;
4996 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 if (op == '*')
5002 n1 = n1 * n2;
5003 else if (op == '/')
5004 {
5005 if (n2 == 0) /* give an error message? */
5006 {
5007 if (n1 == 0)
5008 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5009 else if (n1 < 0)
5010 n1 = -0x7fffffffL;
5011 else
5012 n1 = 0x7fffffffL;
5013 }
5014 else
5015 n1 = n1 / n2;
5016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
5019 if (n2 == 0) /* give an error message? */
5020 n1 = 0;
5021 else
5022 n1 = n1 % n2;
5023 }
5024 rettv->v_type = VAR_NUMBER;
5025 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 }
5029
5030 return OK;
5031}
5032
5033/*
5034 * Handle sixth level expression:
5035 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005036 * "string" string constant
5037 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * &option-name option value
5039 * @r register contents
5040 * identifier variable value
5041 * function() function call
5042 * $VAR environment variable
5043 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005044 * [expr, expr] List
5045 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * Also handle:
5048 * ! in front logical NOT
5049 * - in front unary minus
5050 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * trailing [] subscript in String or List
5052 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 *
5054 * "arg" must point to the first non-white of the expression.
5055 * "arg" is advanced to the next non-white after the recognized expression.
5056 *
5057 * Return OK or FAIL.
5058 */
5059 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005060eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005064 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 long n;
5067 int len;
5068 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 char_u *start_leader, *end_leader;
5070 int ret = OK;
5071 char_u *alias;
5072
5073 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 /*
5080 * Skip '!' and '-' characters. They are handled later.
5081 */
5082 start_leader = *arg;
5083 while (**arg == '!' || **arg == '-' || **arg == '+')
5084 *arg = skipwhite(*arg + 1);
5085 end_leader = *arg;
5086
5087 switch (**arg)
5088 {
5089 /*
5090 * Number constant.
5091 */
5092 case '0':
5093 case '1':
5094 case '2':
5095 case '3':
5096 case '4':
5097 case '5':
5098 case '6':
5099 case '7':
5100 case '8':
5101 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 {
5103#ifdef FEAT_FLOAT
5104 char_u *p = skipdigits(*arg + 1);
5105 int get_float = FALSE;
5106
5107 /* We accept a float when the format matches
5108 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005109 * strict to avoid backwards compatibility problems.
5110 * Don't look for a float after the "." operator, so that
5111 * ":let vers = 1.2.3" doesn't fail. */
5112 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 get_float = TRUE;
5115 p = skipdigits(p + 2);
5116 if (*p == 'e' || *p == 'E')
5117 {
5118 ++p;
5119 if (*p == '-' || *p == '+')
5120 ++p;
5121 if (!vim_isdigit(*p))
5122 get_float = FALSE;
5123 else
5124 p = skipdigits(p + 1);
5125 }
5126 if (ASCII_ISALPHA(*p) || *p == '.')
5127 get_float = FALSE;
5128 }
5129 if (get_float)
5130 {
5131 float_T f;
5132
5133 *arg += string2float(*arg, &f);
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_FLOAT;
5137 rettv->vval.v_float = f;
5138 }
5139 }
5140 else
5141#endif
5142 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005143 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 *arg += len;
5145 if (evaluate)
5146 {
5147 rettv->v_type = VAR_NUMBER;
5148 rettv->vval.v_number = n;
5149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 /*
5155 * String constant: "string".
5156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159
5160 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 break;
5165
5166 /*
5167 * List: [expr, expr]
5168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 * Dictionary: {key: val, key: val}
5174 */
5175 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5176 break;
5177
5178 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005181 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Environment variable: $VAR.
5186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
5191 * Register contents: @r.
5192 */
5193 case '@': ++*arg;
5194 if (evaluate)
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005197 rettv->vval.v_string = get_reg_contents(**arg,
5198 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 if (**arg != NUL)
5201 ++*arg;
5202 break;
5203
5204 /*
5205 * nested expression: (expression).
5206 */
5207 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (**arg == ')')
5210 ++*arg;
5211 else if (ret == OK)
5212 {
5213 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ret = FAIL;
5216 }
5217 break;
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222
5223 if (ret == NOTDONE)
5224 {
5225 /*
5226 * Must be a variable or function name.
5227 * Can also be a curly-braces kind of name: {expr}.
5228 */
5229 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 if (alias != NULL)
5232 s = alias;
5233
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 ret = FAIL;
5236 else
5237 {
5238 if (**arg == '(') /* recursive! */
5239 {
5240 /* If "s" is the name of a variable of type VAR_FUNC
5241 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005242 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243
5244 /* Invoke the function. */
5245 ret = get_func_tv(s, len, rettv, arg,
5246 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248
5249 /* If evaluate is FALSE rettv->v_type was not set in
5250 * get_func_tv, but it's needed in handle_subscript() to parse
5251 * what follows. So set it here. */
5252 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5253 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005254 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005255 rettv->v_type = VAR_FUNC;
5256 }
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /* Stop the expression evaluation when immediately
5259 * aborting on error, or when an interrupt occurred or
5260 * an exception was thrown but not caught. */
5261 if (aborting())
5262 {
5263 if (ret == OK)
5264 clear_tv(rettv);
5265 ret = FAIL;
5266 }
5267 }
5268 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005269 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005270 else
5271 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005273 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 }
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 *arg = skipwhite(*arg);
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5279 * expr(expr). */
5280 if (ret == OK)
5281 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /*
5284 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5285 */
5286 if (ret == OK && evaluate && end_leader > start_leader)
5287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 int val = 0;
5290#ifdef FEAT_FLOAT
5291 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005293 if (rettv->v_type == VAR_FLOAT)
5294 f = rettv->vval.v_float;
5295 else
5296#endif
5297 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 clear_tv(rettv);
5301 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 else
5304 {
5305 while (end_leader > start_leader)
5306 {
5307 --end_leader;
5308 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = !f;
5313 else
5314#endif
5315 val = !val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 {
5319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 f = -f;
5322 else
5323#endif
5324 val = -val;
5325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327#ifdef FEAT_FLOAT
5328 if (rettv->v_type == VAR_FLOAT)
5329 {
5330 clear_tv(rettv);
5331 rettv->vval.v_float = f;
5332 }
5333 else
5334#endif
5335 {
5336 clear_tv(rettv);
5337 rettv->v_type = VAR_NUMBER;
5338 rettv->vval.v_number = val;
5339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342
5343 return ret;
5344}
5345
5346/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005347 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5348 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5350 */
5351 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005352eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005356 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357{
5358 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 long len = -1;
5362 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005368 if (verbose)
5369 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 return FAIL;
5371 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005372#ifdef FEAT_FLOAT
5373 else if (rettv->v_type == VAR_FLOAT)
5374 {
5375 if (verbose)
5376 EMSG(_(e_float_as_string));
5377 return FAIL;
5378 }
5379#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005381 init_tv(&var1);
5382 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 /*
5386 * dict.name
5387 */
5388 key = *arg + 1;
5389 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5390 ;
5391 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 }
5395 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 /*
5398 * something[idx]
5399 *
5400 * Get the (first) variable from inside the [].
5401 */
5402 *arg = skipwhite(*arg + 1);
5403 if (**arg == ':')
5404 empty1 = TRUE;
5405 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5406 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005407 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5408 {
5409 /* not a number or string */
5410 clear_tv(&var1);
5411 return FAIL;
5412 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413
5414 /*
5415 * Get the second variable from inside the [:].
5416 */
5417 if (**arg == ':')
5418 {
5419 range = TRUE;
5420 *arg = skipwhite(*arg + 1);
5421 if (**arg == ']')
5422 empty2 = TRUE;
5423 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005425 if (!empty1)
5426 clear_tv(&var1);
5427 return FAIL;
5428 }
5429 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5430 {
5431 /* not a number or string */
5432 if (!empty1)
5433 clear_tv(&var1);
5434 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 return FAIL;
5436 }
5437 }
5438
5439 /* Check for the ']'. */
5440 if (**arg != ']')
5441 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005442 if (verbose)
5443 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 clear_tv(&var1);
5445 if (range)
5446 clear_tv(&var2);
5447 return FAIL;
5448 }
5449 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451
5452 if (evaluate)
5453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 n1 = 0;
5455 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 n1 = get_tv_number(&var1);
5458 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 if (range)
5461 {
5462 if (empty2)
5463 n2 = -1;
5464 else
5465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n2 = get_tv_number(&var2);
5467 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 }
5470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 {
5473 case VAR_NUMBER:
5474 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 len = (long)STRLEN(s);
5477 if (range)
5478 {
5479 /* The resulting variable is a substring. If the indexes
5480 * are out of range the result is empty. */
5481 if (n1 < 0)
5482 {
5483 n1 = len + n1;
5484 if (n1 < 0)
5485 n1 = 0;
5486 }
5487 if (n2 < 0)
5488 n2 = len + n2;
5489 else if (n2 >= len)
5490 n2 = len;
5491 if (n1 >= len || n2 < 0 || n1 > n2)
5492 s = NULL;
5493 else
5494 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5495 }
5496 else
5497 {
5498 /* The resulting variable is a string of a single
5499 * character. If the index is too big or negative the
5500 * result is empty. */
5501 if (n1 >= len || n1 < 0)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, 1);
5505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 clear_tv(rettv);
5507 rettv->v_type = VAR_STRING;
5508 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 break;
5510
5511 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 if (n1 < 0)
5514 n1 = len + n1;
5515 if (!empty1 && (n1 < 0 || n1 >= len))
5516 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005517 /* For a range we allow invalid values and return an empty
5518 * list. A list index out of range is an error. */
5519 if (!range)
5520 {
5521 if (verbose)
5522 EMSGN(_(e_listidx), n1);
5523 return FAIL;
5524 }
5525 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 }
5527 if (range)
5528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005529 list_T *l;
5530 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531
5532 if (n2 < 0)
5533 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005534 else if (n2 >= len)
5535 n2 = len - 1;
5536 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005537 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 l = list_alloc();
5539 if (l == NULL)
5540 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 n1 <= n2; ++n1)
5543 {
5544 if (list_append_tv(l, &item->li_tv) == FAIL)
5545 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005546 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 return FAIL;
5548 }
5549 item = item->li_next;
5550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 clear_tv(rettv);
5552 rettv->v_type = VAR_LIST;
5553 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005554 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556 else
5557 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005558 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 clear_tv(rettv);
5560 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 }
5562 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563
5564 case VAR_DICT:
5565 if (range)
5566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005567 if (verbose)
5568 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 if (len == -1)
5570 clear_tv(&var1);
5571 return FAIL;
5572 }
5573 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005574 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
5576 if (len == -1)
5577 {
5578 key = get_tv_string(&var1);
5579 if (*key == NUL)
5580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (verbose)
5582 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 clear_tv(&var1);
5584 return FAIL;
5585 }
5586 }
5587
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005588 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005590 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005591 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 if (len == -1)
5593 clear_tv(&var1);
5594 if (item == NULL)
5595 return FAIL;
5596
5597 copy_tv(&item->di_tv, &var1);
5598 clear_tv(rettv);
5599 *rettv = var1;
5600 }
5601 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 }
5603 }
5604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 return OK;
5606}
5607
5608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 * Get an option value.
5610 * "arg" points to the '&' or '+' before the option name.
5611 * "arg" is advanced to character after the option name.
5612 * Return OK or FAIL.
5613 */
5614 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 int evaluate;
5619{
5620 char_u *option_end;
5621 long numval;
5622 char_u *stringval;
5623 int opt_type;
5624 int c;
5625 int working = (**arg == '+'); /* has("+option") */
5626 int ret = OK;
5627 int opt_flags;
5628
5629 /*
5630 * Isolate the option name and find its value.
5631 */
5632 option_end = find_option_end(arg, &opt_flags);
5633 if (option_end == NULL)
5634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 EMSG2(_("E112: Option name missing: %s"), *arg);
5637 return FAIL;
5638 }
5639
5640 if (!evaluate)
5641 {
5642 *arg = option_end;
5643 return OK;
5644 }
5645
5646 c = *option_end;
5647 *option_end = NUL;
5648 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 if (opt_type == -3) /* invalid name */
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E113: Unknown option: %s"), *arg);
5655 ret = FAIL;
5656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 if (opt_type == -2) /* hidden string option */
5660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv->v_type = VAR_STRING;
5662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664 else if (opt_type == -1) /* hidden number option */
5665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv->v_type = VAR_NUMBER;
5667 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669 else if (opt_type == 1) /* number option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_NUMBER;
5672 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else /* string option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 }
5680 else if (working && (opt_type == -2 || opt_type == -1))
5681 ret = FAIL;
5682
5683 *option_end = c; /* put back for error messages */
5684 *arg = option_end;
5685
5686 return ret;
5687}
5688
5689/*
5690 * Allocate a variable for a string constant.
5691 * Return OK or FAIL.
5692 */
5693 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 int evaluate;
5698{
5699 char_u *p;
5700 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 int extra = 0;
5702
5703 /*
5704 * Find the end of the string, skipping backslashed characters.
5705 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005706 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 if (*p == '\\' && p[1] != NUL)
5709 {
5710 ++p;
5711 /* A "\<x>" form occupies at least 4 characters, and produces up
5712 * to 6 characters: reserve space for 2 extra */
5713 if (*p == '<')
5714 extra += 2;
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
5718 if (*p != '"')
5719 {
5720 EMSG2(_("E114: Missing quote: %s"), *arg);
5721 return FAIL;
5722 }
5723
5724 /* If only parsing, set *arg and return here */
5725 if (!evaluate)
5726 {
5727 *arg = p + 1;
5728 return OK;
5729 }
5730
5731 /*
5732 * Copy the string into allocated memory, handling backslashed
5733 * characters.
5734 */
5735 name = alloc((unsigned)(p - *arg + extra));
5736 if (name == NULL)
5737 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 rettv->v_type = VAR_STRING;
5739 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 if (*p == '\\')
5744 {
5745 switch (*++p)
5746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 case 'b': *name++ = BS; ++p; break;
5748 case 'e': *name++ = ESC; ++p; break;
5749 case 'f': *name++ = FF; ++p; break;
5750 case 'n': *name++ = NL; ++p; break;
5751 case 'r': *name++ = CAR; ++p; break;
5752 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 case 'X': /* hex: "\x1", "\x12" */
5755 case 'x':
5756 case 'u': /* Unicode: "\u0023" */
5757 case 'U':
5758 if (vim_isxdigit(p[1]))
5759 {
5760 int n, nr;
5761 int c = toupper(*p);
5762
5763 if (c == 'X')
5764 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005765 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005767 else
5768 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 nr = 0;
5770 while (--n >= 0 && vim_isxdigit(p[1]))
5771 {
5772 ++p;
5773 nr = (nr << 4) + hex2nr(*p);
5774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#ifdef FEAT_MBYTE
5777 /* For "\u" store the number according to
5778 * 'encoding'. */
5779 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else
5782#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 break;
5786
5787 /* octal: "\1", "\12", "\123" */
5788 case '0':
5789 case '1':
5790 case '2':
5791 case '3':
5792 case '4':
5793 case '5':
5794 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 case '7': *name = *p++ - '0';
5796 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name = (*name << 3) + *p++ - '0';
5799 if (*p >= '0' && *p <= '7')
5800 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 break;
5804
5805 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if (extra != 0)
5808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811 }
5812 /* FALLTHROUGH */
5813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 }
5817 }
5818 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 *arg = p + 1;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 return OK;
5826}
5827
5828/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 * Return OK or FAIL.
5831 */
5832 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 int evaluate;
5837{
5838 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 int reduce = 0;
5841
5842 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 */
5845 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 break;
5851 ++reduce;
5852 ++p;
5853 }
5854 }
5855
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 return FAIL;
5860 }
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 if (!evaluate)
5864 {
5865 *arg = p + 1;
5866 return OK;
5867 }
5868
5869 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 */
5872 str = alloc((unsigned)((p - *arg) - reduce));
5873 if (str == NULL)
5874 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 rettv->v_type = VAR_STRING;
5876 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 break;
5884 ++p;
5885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 *arg = p + 1;
5890
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 * Allocate a variable for a List and fill it from "*arg".
5896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005899get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 int evaluate;
5903{
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 list_T *l = NULL;
5905 typval_T tv;
5906 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907
5908 if (evaluate)
5909 {
5910 l = list_alloc();
5911 if (l == NULL)
5912 return FAIL;
5913 }
5914
5915 *arg = skipwhite(*arg + 1);
5916 while (**arg != ']' && **arg != NUL)
5917 {
5918 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5919 goto failret;
5920 if (evaluate)
5921 {
5922 item = listitem_alloc();
5923 if (item != NULL)
5924 {
5925 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005926 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 list_append(l, item);
5928 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005929 else
5930 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 }
5932
5933 if (**arg == ']')
5934 break;
5935 if (**arg != ',')
5936 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 goto failret;
5939 }
5940 *arg = skipwhite(*arg + 1);
5941 }
5942
5943 if (**arg != ']')
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946failret:
5947 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005948 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 return FAIL;
5950 }
5951
5952 *arg = skipwhite(*arg + 1);
5953 if (evaluate)
5954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005955 rettv->v_type = VAR_LIST;
5956 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 ++l->lv_refcount;
5958 }
5959
5960 return OK;
5961}
5962
5963/*
5964 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005965 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005967 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968list_alloc()
5969{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005970 list_T *l;
5971
5972 l = (list_T *)alloc_clear(sizeof(list_T));
5973 if (l != NULL)
5974 {
5975 /* Prepend the list to the list of lists for garbage collection. */
5976 if (first_list != NULL)
5977 first_list->lv_used_prev = l;
5978 l->lv_used_prev = NULL;
5979 l->lv_used_next = first_list;
5980 first_list = l;
5981 }
5982 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983}
5984
5985/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005986 * Allocate an empty list for a return value.
5987 * Returns OK or FAIL.
5988 */
5989 static int
5990rettv_list_alloc(rettv)
5991 typval_T *rettv;
5992{
5993 list_T *l = list_alloc();
5994
5995 if (l == NULL)
5996 return FAIL;
5997
5998 rettv->vval.v_list = l;
5999 rettv->v_type = VAR_LIST;
6000 ++l->lv_refcount;
6001 return OK;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Unreference a list: decrement the reference count and free it when it
6006 * becomes zero.
6007 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006008 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006012 if (l != NULL && --l->lv_refcount <= 0)
6013 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014}
6015
6016/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006017 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Ignores the reference count.
6019 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006020 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006021list_free(l, recurse)
6022 list_T *l;
6023 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027 /* Remove the list from the list of lists for garbage collection. */
6028 if (l->lv_used_prev == NULL)
6029 first_list = l->lv_used_next;
6030 else
6031 l->lv_used_prev->lv_used_next = l->lv_used_next;
6032 if (l->lv_used_next != NULL)
6033 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6034
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006037 /* Remove the item before deleting it. */
6038 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006039 if (recurse || (item->li_tv.v_type != VAR_LIST
6040 && item->li_tv.v_type != VAR_DICT))
6041 clear_tv(&item->li_tv);
6042 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 }
6044 vim_free(l);
6045}
6046
6047/*
6048 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006049 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006051 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052listitem_alloc()
6053{
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055}
6056
6057/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006058 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069 * Remove a list item from a List and free it. Also clears the value.
6070 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006071 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 list_T *l;
6074 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006076 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077 listitem_free(item);
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Get the number of items in a list.
6082 */
6083 static long
6084list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 if (l == NULL)
6088 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090}
6091
6092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 * Return TRUE when two lists have exactly the same values.
6094 */
6095 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 list_T *l1;
6098 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006104 if (l1 == NULL || l2 == NULL)
6105 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 if (l1 == l2)
6107 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108 if (list_len(l1) != list_len(l2))
6109 return FALSE;
6110
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 for (item1 = l1->lv_first, item2 = l2->lv_first;
6112 item1 != NULL && item2 != NULL;
6113 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 return FALSE;
6116 return item1 == NULL && item2 == NULL;
6117}
6118
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006119#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6120 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121/*
6122 * Return the dictitem that an entry in a hashtable points to.
6123 */
6124 dictitem_T *
6125dict_lookup(hi)
6126 hashitem_T *hi;
6127{
6128 return HI2DI(hi);
6129}
6130#endif
6131
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 * Return TRUE when two dictionaries have exactly the same key/values.
6134 */
6135 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006136dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 dict_T *d1;
6138 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141{
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hashitem_T *hi;
6143 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006146 if (d1 == NULL || d2 == NULL)
6147 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006148 if (d1 == d2)
6149 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 if (dict_len(d1) != dict_len(d2))
6151 return FALSE;
6152
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006153 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006156 if (!HASHITEM_EMPTY(hi))
6157 {
6158 item2 = dict_find(d2, hi->hi_key, -1);
6159 if (item2 == NULL)
6160 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 return FALSE;
6163 --todo;
6164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 }
6166 return TRUE;
6167}
6168
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169static int tv_equal_recurse_limit;
6170
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006174 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 */
6176 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006177tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 typval_T *tv1;
6179 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 int ic; /* ignore case */
6181 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182{
6183 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006184 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006186 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006189 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006192 * recursiveness to a limit. We guess they are equal then.
6193 * A fixed limit has the problem of still taking an awful long time.
6194 * Reduce the limit every time running into it. That should work fine for
6195 * deeply linked structures that are not recursively linked and catch
6196 * recursiveness quickly. */
6197 if (!recursive)
6198 tv_equal_recurse_limit = 1000;
6199 if (recursive_cnt >= tv_equal_recurse_limit)
6200 {
6201 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006202 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204
6205 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006208 ++recursive_cnt;
6209 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6210 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006211 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006212
6213 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214 ++recursive_cnt;
6215 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6216 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006217 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218
6219 case VAR_FUNC:
6220 return (tv1->vval.v_string != NULL
6221 && tv2->vval.v_string != NULL
6222 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6223
6224 case VAR_NUMBER:
6225 return tv1->vval.v_number == tv2->vval.v_number;
6226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006227#ifdef FEAT_FLOAT
6228 case VAR_FLOAT:
6229 return tv1->vval.v_float == tv2->vval.v_float;
6230#endif
6231
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232 case VAR_STRING:
6233 s1 = get_tv_string_buf(tv1, buf1);
6234 s2 = get_tv_string_buf(tv2, buf2);
6235 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006236 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006237
6238 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239 return TRUE;
6240}
6241
6242/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 * Locate item with index "n" in list "l" and return it.
6244 * A negative index is counted from the end; -1 is the last item.
6245 * Returns NULL when "n" is out of range.
6246 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006247 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 long n;
6251{
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 long idx;
6254
6255 if (l == NULL)
6256 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006257
6258 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260 n = l->lv_len + n;
6261
6262 /* Check for index out of range. */
6263 if (n < 0 || n >= l->lv_len)
6264 return NULL;
6265
6266 /* When there is a cached index may start search from there. */
6267 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006269 if (n < l->lv_idx / 2)
6270 {
6271 /* closest to the start of the list */
6272 item = l->lv_first;
6273 idx = 0;
6274 }
6275 else if (n > (l->lv_idx + l->lv_len) / 2)
6276 {
6277 /* closest to the end of the list */
6278 item = l->lv_last;
6279 idx = l->lv_len - 1;
6280 }
6281 else
6282 {
6283 /* closest to the cached index */
6284 item = l->lv_idx_item;
6285 idx = l->lv_idx;
6286 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 }
6288 else
6289 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006290 if (n < l->lv_len / 2)
6291 {
6292 /* closest to the start of the list */
6293 item = l->lv_first;
6294 idx = 0;
6295 }
6296 else
6297 {
6298 /* closest to the end of the list */
6299 item = l->lv_last;
6300 idx = l->lv_len - 1;
6301 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303
6304 while (n > idx)
6305 {
6306 /* search forward */
6307 item = item->li_next;
6308 ++idx;
6309 }
6310 while (n < idx)
6311 {
6312 /* search backward */
6313 item = item->li_prev;
6314 --idx;
6315 }
6316
6317 /* cache the used index */
6318 l->lv_idx = idx;
6319 l->lv_idx_item = item;
6320
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 return item;
6322}
6323
6324/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006325 * Get list item "l[idx]" as a number.
6326 */
6327 static long
6328list_find_nr(l, idx, errorp)
6329 list_T *l;
6330 long idx;
6331 int *errorp; /* set to TRUE when something wrong */
6332{
6333 listitem_T *li;
6334
6335 li = list_find(l, idx);
6336 if (li == NULL)
6337 {
6338 if (errorp != NULL)
6339 *errorp = TRUE;
6340 return -1L;
6341 }
6342 return get_tv_number_chk(&li->li_tv, errorp);
6343}
6344
6345/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006346 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6347 */
6348 char_u *
6349list_find_str(l, idx)
6350 list_T *l;
6351 long idx;
6352{
6353 listitem_T *li;
6354
6355 li = list_find(l, idx - 1);
6356 if (li == NULL)
6357 {
6358 EMSGN(_(e_listidx), idx);
6359 return NULL;
6360 }
6361 return get_tv_string(&li->li_tv);
6362}
6363
6364/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006365 * Locate "item" list "l" and return its index.
6366 * Returns -1 when "item" is not in the list.
6367 */
6368 static long
6369list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *l;
6371 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006372{
6373 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375
6376 if (l == NULL)
6377 return -1;
6378 idx = 0;
6379 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6380 ++idx;
6381 if (li == NULL)
6382 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006383 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384}
6385
6386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 * Append item "item" to the end of list "l".
6388 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006389 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 list_T *l;
6392 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393{
6394 if (l->lv_last == NULL)
6395 {
6396 /* empty list */
6397 l->lv_first = item;
6398 l->lv_last = item;
6399 item->li_prev = NULL;
6400 }
6401 else
6402 {
6403 l->lv_last->li_next = item;
6404 item->li_prev = l->lv_last;
6405 l->lv_last = item;
6406 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006407 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 item->li_next = NULL;
6409}
6410
6411/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006415 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 list_T *l;
6418 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 copy_tv(tv, &li->li_tv);
6425 list_append(l, li);
6426 return OK;
6427}
6428
6429/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006430 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431 * Return FAIL when out of memory.
6432 */
6433 int
6434list_append_dict(list, dict)
6435 list_T *list;
6436 dict_T *dict;
6437{
6438 listitem_T *li = listitem_alloc();
6439
6440 if (li == NULL)
6441 return FAIL;
6442 li->li_tv.v_type = VAR_DICT;
6443 li->li_tv.v_lock = 0;
6444 li->li_tv.vval.v_dict = dict;
6445 list_append(list, li);
6446 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return OK;
6448}
6449
6450/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 * Returns FAIL when out of memory.
6454 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006456list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457 list_T *l;
6458 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006459 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006460{
6461 listitem_T *li = listitem_alloc();
6462
6463 if (li == NULL)
6464 return FAIL;
6465 list_append(l, li);
6466 li->li_tv.v_type = VAR_STRING;
6467 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006468 if (str == NULL)
6469 li->li_tv.vval.v_string = NULL;
6470 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006471 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006472 return FAIL;
6473 return OK;
6474}
6475
6476/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006477 * Append "n" to list "l".
6478 * Returns FAIL when out of memory.
6479 */
6480 static int
6481list_append_number(l, n)
6482 list_T *l;
6483 varnumber_T n;
6484{
6485 listitem_T *li;
6486
6487 li = listitem_alloc();
6488 if (li == NULL)
6489 return FAIL;
6490 li->li_tv.v_type = VAR_NUMBER;
6491 li->li_tv.v_lock = 0;
6492 li->li_tv.vval.v_number = n;
6493 list_append(l, li);
6494 return OK;
6495}
6496
6497/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 * If "item" is NULL append at the end.
6500 * Return FAIL when out of memory.
6501 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006502 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 list_T *l;
6505 typval_T *tv;
6506 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507{
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509
6510 if (ni == NULL)
6511 return FAIL;
6512 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006513 list_insert(l, ni, item);
6514 return OK;
6515}
6516
6517 void
6518list_insert(l, ni, item)
6519 list_T *l;
6520 listitem_T *ni;
6521 listitem_T *item;
6522{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 if (item == NULL)
6524 /* Append new item at end of list. */
6525 list_append(l, ni);
6526 else
6527 {
6528 /* Insert new item before existing item. */
6529 ni->li_prev = item->li_prev;
6530 ni->li_next = item;
6531 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 ++l->lv_idx;
6535 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 l->lv_idx_item = NULL;
6540 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544}
6545
6546/*
6547 * Extend "l1" with "l2".
6548 * If "bef" is NULL append at the end, otherwise insert before this item.
6549 * Returns FAIL when out of memory.
6550 */
6551 static int
6552list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 list_T *l1;
6554 list_T *l2;
6555 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556{
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006558 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006560 /* We also quit the loop when we have inserted the original item count of
6561 * the list, avoid a hang when we extend a list with itself. */
6562 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6564 return FAIL;
6565 return OK;
6566}
6567
6568/*
6569 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6570 * Return FAIL when out of memory.
6571 */
6572 static int
6573list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 list_T *l1;
6575 list_T *l2;
6576 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577{
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006580 if (l1 == NULL || l2 == NULL)
6581 return FAIL;
6582
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006584 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 if (l == NULL)
6586 return FAIL;
6587 tv->v_type = VAR_LIST;
6588 tv->vval.v_list = l;
6589
6590 /* append all items from the second list */
6591 return list_extend(l, l2, NULL);
6592}
6593
6594/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 * Returns NULL when out of memory.
6599 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605{
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 list_T *copy;
6607 listitem_T *item;
6608 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 if (orig == NULL)
6611 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612
6613 copy = list_alloc();
6614 if (copy != NULL)
6615 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 if (copyID != 0)
6617 {
6618 /* Do this before adding the items, because one of the items may
6619 * refer back to this list. */
6620 orig->lv_copyID = copyID;
6621 orig->lv_copylist = copy;
6622 }
6623 for (item = orig->lv_first; item != NULL && !got_int;
6624 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 {
6626 ni = listitem_alloc();
6627 if (ni == NULL)
6628 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006629 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 {
6631 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6632 {
6633 vim_free(ni);
6634 break;
6635 }
6636 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 list_append(copy, ni);
6640 }
6641 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006642 if (item != NULL)
6643 {
6644 list_unref(copy);
6645 copy = NULL;
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 }
6648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 return copy;
6650}
6651
6652/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006654 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006655 * This used to be called list_remove, but that conflicts with a Sun header
6656 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006658 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006659vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 list_T *l;
6661 listitem_T *item;
6662 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663{
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666 /* notify watchers */
6667 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006669 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670 list_fix_watch(l, ip);
6671 if (ip == item2)
6672 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674
6675 if (item2->li_next == NULL)
6676 l->lv_last = item->li_prev;
6677 else
6678 item2->li_next->li_prev = item->li_prev;
6679 if (item->li_prev == NULL)
6680 l->lv_first = item2->li_next;
6681 else
6682 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006683 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684}
6685
6686/*
6687 * Return an allocated string with the string representation of a list.
6688 * May return NULL.
6689 */
6690 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006691list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694{
6695 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006696
6697 if (tv->vval.v_list == NULL)
6698 return NULL;
6699 ga_init2(&ga, (int)sizeof(char), 80);
6700 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006702 {
6703 vim_free(ga.ga_data);
6704 return NULL;
6705 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 ga_append(&ga, ']');
6707 ga_append(&ga, NUL);
6708 return (char_u *)ga.ga_data;
6709}
6710
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711typedef struct join_S {
6712 char_u *s;
6713 char_u *tofree;
6714} join_T;
6715
6716 static int
6717list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6718 garray_T *gap; /* to store the result in */
6719 list_T *l;
6720 char_u *sep;
6721 int echo_style;
6722 int copyID;
6723 garray_T *join_gap; /* to keep each list item string */
6724{
6725 int i;
6726 join_T *p;
6727 int len;
6728 int sumlen = 0;
6729 int first = TRUE;
6730 char_u *tofree;
6731 char_u numbuf[NUMBUFLEN];
6732 listitem_T *item;
6733 char_u *s;
6734
6735 /* Stringify each item in the list. */
6736 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6737 {
6738 if (echo_style)
6739 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6740 else
6741 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6742 if (s == NULL)
6743 return FAIL;
6744
6745 len = (int)STRLEN(s);
6746 sumlen += len;
6747
Bram Moolenaarcde88542015-08-11 19:14:00 +02006748 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006749 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6750 if (tofree != NULL || s != numbuf)
6751 {
6752 p->s = s;
6753 p->tofree = tofree;
6754 }
6755 else
6756 {
6757 p->s = vim_strnsave(s, len);
6758 p->tofree = p->s;
6759 }
6760
6761 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006762 if (did_echo_string_emsg) /* recursion error, bail out */
6763 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006764 }
6765
6766 /* Allocate result buffer with its total size, avoid re-allocation and
6767 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6768 if (join_gap->ga_len >= 2)
6769 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6770 if (ga_grow(gap, sumlen + 2) == FAIL)
6771 return FAIL;
6772
6773 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6774 {
6775 if (first)
6776 first = FALSE;
6777 else
6778 ga_concat(gap, sep);
6779 p = ((join_T *)join_gap->ga_data) + i;
6780
6781 if (p->s != NULL)
6782 ga_concat(gap, p->s);
6783 line_breakcheck();
6784 }
6785
6786 return OK;
6787}
6788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006791 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006794 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006795list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006797 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006799 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006800 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006802 garray_T join_ga;
6803 int retval;
6804 join_T *p;
6805 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806
Bram Moolenaard39a7512015-04-16 22:51:22 +02006807 if (l->lv_len < 1)
6808 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6810 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6811
6812 /* Dispose each item in join_ga. */
6813 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815 p = (join_T *)join_ga.ga_data;
6816 for (i = 0; i < join_ga.ga_len; ++i)
6817 {
6818 vim_free(p->tofree);
6819 ++p;
6820 }
6821 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006823
6824 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825}
6826
6827/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 * Garbage collection for lists and dictionaries.
6829 *
6830 * We use reference counts to be able to free most items right away when they
6831 * are no longer used. But for composite items it's possible that it becomes
6832 * unused while the reference count is > 0: When there is a recursive
6833 * reference. Example:
6834 * :let l = [1, 2, 3]
6835 * :let d = {9: l}
6836 * :let l[1] = d
6837 *
6838 * Since this is quite unusual we handle this with garbage collection: every
6839 * once in a while find out which lists and dicts are not referenced from any
6840 * variable.
6841 *
6842 * Here is a good reference text about garbage collection (refers to Python
6843 * but it applies to all reference-counting mechanisms):
6844 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846
6847/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 * Do garbage collection for lists and dicts.
6849 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 int
6852garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 buf_T *buf;
6857 win_T *wp;
6858 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006859 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006860 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006862#ifdef FEAT_WINDOWS
6863 tabpage_T *tp;
6864#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006866 /* Only do this once. */
6867 want_garbage_collect = FALSE;
6868 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006869 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006870
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 /* We advance by two because we add one for items referenced through
6872 * previous_funccal. */
6873 current_copyID += COPYID_INC;
6874 copyID = current_copyID;
6875
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 /*
6877 * 1. Go through all accessible variables and mark all lists and dicts
6878 * with copyID.
6879 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880
6881 /* Don't free variables in the previous_funccal list unless they are only
6882 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006883 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6885 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6887 NULL);
6888 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6889 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890 }
6891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 /* script-local variables */
6893 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895
6896 /* buffer-local variables */
6897 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900
6901 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6904 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006905#ifdef FEAT_AUTOCMD
6906 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6908 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006909#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006911#ifdef FEAT_WINDOWS
6912 /* tabpage-local variables */
6913 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6915 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006916#endif
6917
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
6921 /* function-local variables */
6922 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6923 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6925 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 }
6927
Bram Moolenaard812df62008-11-09 12:46:09 +00006928 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006930
Bram Moolenaar1dced572012-04-05 16:54:08 +02006931#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006933#endif
6934
Bram Moolenaardb913952012-06-29 12:54:53 +02006935#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006937#endif
6938
6939#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006941#endif
6942
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 /*
6946 * 2. Free lists and dictionaries that are not referenced.
6947 */
6948 did_free = free_unref_items(copyID);
6949
6950 /*
6951 * 3. Check if any funccal can be freed now.
6952 */
6953 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (can_free_funccal(*pfc, copyID))
6956 {
6957 fc = *pfc;
6958 *pfc = fc->caller;
6959 free_funccal(fc, TRUE);
6960 did_free = TRUE;
6961 did_free_funccal = TRUE;
6962 }
6963 else
6964 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 if (did_free_funccal)
6967 /* When a funccal was freed some more items might be garbage
6968 * collected, so run again. */
6969 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 else if (p_verbose > 0)
6972 {
6973 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6974 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975
6976 return did_free;
6977}
6978
6979/*
6980 * Free lists and dictionaries that are no longer referenced.
6981 */
6982 static int
6983free_unref_items(copyID)
6984 int copyID;
6985{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006986 dict_T *dd, *dd_next;
6987 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988 int did_free = FALSE;
6989
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006998 /* Free the Dictionary and ordinary items it contains, but don't
6999 * recurse into Lists and Dictionaries, they will be in the list
7000 * of dicts or list of lists. */
7001 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 dd = dd_next;
7005 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
7007 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007008 * Go through the list of lists and free items without the copyID.
7009 * But don't free a list that has a watcher (used in a for loop), these
7010 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 */
7012 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 {
7014 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007015 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7016 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007018 /* Free the List and ordinary items it contains, but don't recurse
7019 * into Lists and Dictionaries, they will be in the list of dicts
7020 * or list of lists. */
7021 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007024 ll = ll_next;
7025 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 return did_free;
7027}
7028
7029/*
7030 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 * "list_stack" is used to add lists to be marked. Can be NULL.
7032 *
7033 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 int
7036set_ref_in_ht(ht, copyID, list_stack)
7037 hashtab_T *ht;
7038 int copyID;
7039 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040{
7041 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 hashtab_T *cur_ht;
7045 ht_stack_T *ht_stack = NULL;
7046 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 cur_ht = ht;
7049 for (;;)
7050 {
7051 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 /* Mark each item in the hashtab. If the item contains a hashtab
7054 * it is added to ht_stack, if it contains a list it is added to
7055 * list_stack. */
7056 todo = (int)cur_ht->ht_used;
7057 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7058 if (!HASHITEM_EMPTY(hi))
7059 {
7060 --todo;
7061 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7062 &ht_stack, list_stack);
7063 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065
7066 if (ht_stack == NULL)
7067 break;
7068
7069 /* take an item from the stack */
7070 cur_ht = ht_stack->ht;
7071 tempitem = ht_stack;
7072 ht_stack = ht_stack->prev;
7073 free(tempitem);
7074 }
7075
7076 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077}
7078
7079/*
7080 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7082 *
7083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int
7086set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 list_T *l;
7088 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 listitem_T *li;
7092 int abort = FALSE;
7093 list_T *cur_l;
7094 list_stack_T *list_stack = NULL;
7095 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 cur_l = l;
7098 for (;;)
7099 {
7100 if (!abort)
7101 /* Mark each item in the list. If the item contains a hashtab
7102 * it is added to ht_stack, if it contains a list it is added to
7103 * list_stack. */
7104 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7105 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7106 ht_stack, &list_stack);
7107 if (list_stack == NULL)
7108 break;
7109
7110 /* take an item from the stack */
7111 cur_l = list_stack->list;
7112 tempitem = list_stack;
7113 list_stack = list_stack->prev;
7114 free(tempitem);
7115 }
7116
7117 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118}
7119
7120/*
7121 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 * "list_stack" is used to add lists to be marked. Can be NULL.
7123 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7124 *
7125 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007126 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 int
7128set_ref_in_item(tv, copyID, ht_stack, list_stack)
7129 typval_T *tv;
7130 int copyID;
7131 ht_stack_T **ht_stack;
7132 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133{
7134 dict_T *dd;
7135 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137
7138 switch (tv->v_type)
7139 {
7140 case VAR_DICT:
7141 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007142 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144 /* Didn't see this dict yet. */
7145 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 if (ht_stack == NULL)
7147 {
7148 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7149 }
7150 else
7151 {
7152 ht_stack_T *newitem = (ht_stack_T*)malloc(
7153 sizeof(ht_stack_T));
7154 if (newitem == NULL)
7155 abort = TRUE;
7156 else
7157 {
7158 newitem->ht = &dd->dv_hashtab;
7159 newitem->prev = *ht_stack;
7160 *ht_stack = newitem;
7161 }
7162 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165
7166 case VAR_LIST:
7167 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007168 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170 /* Didn't see this list yet. */
7171 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 if (list_stack == NULL)
7173 {
7174 abort = set_ref_in_list(ll, copyID, ht_stack);
7175 }
7176 else
7177 {
7178 list_stack_T *newitem = (list_stack_T*)malloc(
7179 sizeof(list_stack_T));
7180 if (newitem == NULL)
7181 abort = TRUE;
7182 else
7183 {
7184 newitem->list = ll;
7185 newitem->prev = *list_stack;
7186 *list_stack = newitem;
7187 }
7188 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007189 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193}
7194
7195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 * Allocate an empty header for a dictionary.
7197 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007198 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199dict_alloc()
7200{
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007206 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007207 if (first_dict != NULL)
7208 first_dict->dv_used_prev = d;
7209 d->dv_used_next = first_dict;
7210 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007211 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007214 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007215 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220}
7221
7222/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007223 * Allocate an empty dict for a return value.
7224 * Returns OK or FAIL.
7225 */
7226 static int
7227rettv_dict_alloc(rettv)
7228 typval_T *rettv;
7229{
7230 dict_T *d = dict_alloc();
7231
7232 if (d == NULL)
7233 return FAIL;
7234
7235 rettv->vval.v_dict = d;
7236 rettv->v_type = VAR_DICT;
7237 ++d->dv_refcount;
7238 return OK;
7239}
7240
7241
7242/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 * Unreference a Dictionary: decrement the reference count and free it when it
7244 * becomes zero.
7245 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007246 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007250 if (d != NULL && --d->dv_refcount <= 0)
7251 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252}
7253
7254/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007255 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 * Ignores the reference count.
7257 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007258 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259dict_free(d, recurse)
7260 dict_T *d;
7261 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007267 /* Remove the dict from the list of dicts for garbage collection. */
7268 if (d->dv_used_prev == NULL)
7269 first_dict = d->dv_used_next;
7270 else
7271 d->dv_used_prev->dv_used_next = d->dv_used_next;
7272 if (d->dv_used_next != NULL)
7273 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7274
7275 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007276 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007277 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (!HASHITEM_EMPTY(hi))
7281 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 /* Remove the item before deleting it, just in case there is
7283 * something recursive causing trouble. */
7284 di = HI2DI(hi);
7285 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007286 if (recurse || (di->di_tv.v_type != VAR_LIST
7287 && di->di_tv.v_type != VAR_DICT))
7288 clear_tv(&di->di_tv);
7289 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 --todo;
7291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 vim_free(d);
7295}
7296
7297/*
7298 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 * The "key" is copied to the new item.
7300 * Note that the value of the item "di_tv" still needs to be initialized!
7301 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007303 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304dictitem_alloc(key)
7305 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007309 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007313 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316}
7317
7318/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 * Make a copy of a Dictionary item.
7320 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324{
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007327 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7328 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (di != NULL)
7330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007332 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 copy_tv(&org->di_tv, &di->di_tv);
7334 }
7335 return di;
7336}
7337
7338/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 * Remove item "item" from Dictionary "dict" and free it.
7340 */
7341 static void
7342dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *dict;
7344 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345{
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 if (HASHITEM_EMPTY(hi))
7350 EMSG2(_(e_intern2), "dictitem_remove()");
7351 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 dictitem_free(item);
7354}
7355
7356/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 * Free a dict item. Also clears the value.
7358 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007359 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007364 if (item->di_flags & DI_FLAGS_ALLOC)
7365 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366}
7367
7368/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7370 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007371 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 * Returns NULL when out of memory.
7373 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *copy;
7381 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384
7385 if (orig == NULL)
7386 return NULL;
7387
7388 copy = dict_alloc();
7389 if (copy != NULL)
7390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (copyID != 0)
7392 {
7393 orig->dv_copyID = copyID;
7394 orig->dv_copydict = copy;
7395 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007396 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 --todo;
7402
7403 di = dictitem_alloc(hi->hi_key);
7404 if (di == NULL)
7405 break;
7406 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 {
7408 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7409 copyID) == FAIL)
7410 {
7411 vim_free(di);
7412 break;
7413 }
7414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415 else
7416 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7417 if (dict_add(copy, di) == FAIL)
7418 {
7419 dictitem_free(di);
7420 break;
7421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007426 if (todo > 0)
7427 {
7428 dict_unref(copy);
7429 copy = NULL;
7430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 }
7432
7433 return copy;
7434}
7435
7436/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007438 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007440 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dict_T *d;
7443 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444{
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446}
7447
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007449 * Add a number or string entry to dictionary "d".
7450 * When "str" is NULL use number "nr", otherwise use "str".
7451 * Returns FAIL when out of memory and when key already exists.
7452 */
7453 int
7454dict_add_nr_str(d, key, nr, str)
7455 dict_T *d;
7456 char *key;
7457 long nr;
7458 char_u *str;
7459{
7460 dictitem_T *item;
7461
7462 item = dictitem_alloc((char_u *)key);
7463 if (item == NULL)
7464 return FAIL;
7465 item->di_tv.v_lock = 0;
7466 if (str == NULL)
7467 {
7468 item->di_tv.v_type = VAR_NUMBER;
7469 item->di_tv.vval.v_number = nr;
7470 }
7471 else
7472 {
7473 item->di_tv.v_type = VAR_STRING;
7474 item->di_tv.vval.v_string = vim_strsave(str);
7475 }
7476 if (dict_add(d, item) == FAIL)
7477 {
7478 dictitem_free(item);
7479 return FAIL;
7480 }
7481 return OK;
7482}
7483
7484/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007485 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007486 * Returns FAIL when out of memory and when key already exists.
7487 */
7488 int
7489dict_add_list(d, key, list)
7490 dict_T *d;
7491 char *key;
7492 list_T *list;
7493{
7494 dictitem_T *item;
7495
7496 item = dictitem_alloc((char_u *)key);
7497 if (item == NULL)
7498 return FAIL;
7499 item->di_tv.v_lock = 0;
7500 item->di_tv.v_type = VAR_LIST;
7501 item->di_tv.vval.v_list = list;
7502 if (dict_add(d, item) == FAIL)
7503 {
7504 dictitem_free(item);
7505 return FAIL;
7506 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007507 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007508 return OK;
7509}
7510
7511/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 * Get the number of items in a Dictionary.
7513 */
7514 static long
7515dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007518 if (d == NULL)
7519 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007520 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521}
7522
7523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 * Find item "key[len]" in Dictionary "d".
7525 * If "len" is negative use strlen(key).
7526 * Returns NULL when not found.
7527 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007528 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 char_u *key;
7532 int len;
7533{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534#define AKEYLEN 200
7535 char_u buf[AKEYLEN];
7536 char_u *akey;
7537 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007540 if (len < 0)
7541 akey = key;
7542 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007543 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 tofree = akey = vim_strnsave(key, len);
7545 if (akey == NULL)
7546 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007547 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 else
7549 {
7550 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007551 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 akey = buf;
7553 }
7554
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 vim_free(tofree);
7557 if (HASHITEM_EMPTY(hi))
7558 return NULL;
7559 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560}
7561
7562/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007563 * Get a string item from a dictionary.
7564 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007565 * Returns NULL if the entry doesn't exist or out of memory.
7566 */
7567 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007569 dict_T *d;
7570 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007571 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572{
7573 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007574 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575
7576 di = dict_find(d, key, -1);
7577 if (di == NULL)
7578 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007579 s = get_tv_string(&di->di_tv);
7580 if (save && s != NULL)
7581 s = vim_strsave(s);
7582 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007583}
7584
7585/*
7586 * Get a number item from a dictionary.
7587 * Returns 0 if the entry doesn't exist or out of memory.
7588 */
7589 long
7590get_dict_number(d, key)
7591 dict_T *d;
7592 char_u *key;
7593{
7594 dictitem_T *di;
7595
7596 di = dict_find(d, key, -1);
7597 if (di == NULL)
7598 return 0;
7599 return get_tv_number(&di->di_tv);
7600}
7601
7602/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 * Return an allocated string with the string representation of a Dictionary.
7604 * May return NULL.
7605 */
7606 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610{
7611 garray_T ga;
7612 int first = TRUE;
7613 char_u *tofree;
7614 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007620 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 return NULL;
7622 ga_init2(&ga, (int)sizeof(char), 80);
7623 ga_append(&ga, '{');
7624
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007625 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 --todo;
7631
7632 if (first)
7633 first = FALSE;
7634 else
7635 ga_concat(&ga, (char_u *)", ");
7636
7637 tofree = string_quote(hi->hi_key, FALSE);
7638 if (tofree != NULL)
7639 {
7640 ga_concat(&ga, tofree);
7641 vim_free(tofree);
7642 }
7643 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 if (s != NULL)
7646 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007648 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007649 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007650 line_breakcheck();
7651
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007654 if (todo > 0)
7655 {
7656 vim_free(ga.ga_data);
7657 return NULL;
7658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659
7660 ga_append(&ga, '}');
7661 ga_append(&ga, NUL);
7662 return (char_u *)ga.ga_data;
7663}
7664
7665/*
7666 * Allocate a variable for a Dictionary and fill it from "*arg".
7667 * Return OK or FAIL. Returns NOTDONE for {expr}.
7668 */
7669 static int
7670get_dict_tv(arg, rettv, evaluate)
7671 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 int evaluate;
7674{
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 dict_T *d = NULL;
7676 typval_T tvkey;
7677 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007678 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007681 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 /*
7684 * First check if it's not a curly-braces thing: {expr}.
7685 * Must do this without evaluating, otherwise a function may be called
7686 * twice. Unfortunately this means we need to call eval1() twice for the
7687 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007688 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 if (*start != '}')
7691 {
7692 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7693 return FAIL;
7694 if (*start == '}')
7695 return NOTDONE;
7696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697
7698 if (evaluate)
7699 {
7700 d = dict_alloc();
7701 if (d == NULL)
7702 return FAIL;
7703 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 tvkey.v_type = VAR_UNKNOWN;
7705 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706
7707 *arg = skipwhite(*arg + 1);
7708 while (**arg != '}' && **arg != NUL)
7709 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007710 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 goto failret;
7712 if (**arg != ':')
7713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007714 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 goto failret;
7717 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007718 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007720 key = get_tv_string_buf_chk(&tvkey, buf);
7721 if (key == NULL || *key == NUL)
7722 {
7723 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7724 if (key != NULL)
7725 EMSG(_(e_emptykey));
7726 clear_tv(&tvkey);
7727 goto failret;
7728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730
7731 *arg = skipwhite(*arg + 1);
7732 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7733 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007734 if (evaluate)
7735 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 goto failret;
7737 }
7738 if (evaluate)
7739 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 if (item != NULL)
7742 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007743 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007744 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 clear_tv(&tv);
7746 goto failret;
7747 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 item = dictitem_alloc(key);
7749 clear_tv(&tvkey);
7750 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007753 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 if (dict_add(d, item) == FAIL)
7755 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 }
7757 }
7758
7759 if (**arg == '}')
7760 break;
7761 if (**arg != ',')
7762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007763 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 goto failret;
7765 }
7766 *arg = skipwhite(*arg + 1);
7767 }
7768
7769 if (**arg != '}')
7770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007771 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772failret:
7773 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007774 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 return FAIL;
7776 }
7777
7778 *arg = skipwhite(*arg + 1);
7779 if (evaluate)
7780 {
7781 rettv->v_type = VAR_DICT;
7782 rettv->vval.v_dict = d;
7783 ++d->dv_refcount;
7784 }
7785
7786 return OK;
7787}
7788
Bram Moolenaar8c711452005-01-14 21:53:12 +00007789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007790 * Return a string with the string representation of a variable.
7791 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007792 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007793 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007794 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007795 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 */
7797 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007798echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007801 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007802 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007804 static int recurse = 0;
7805 char_u *r = NULL;
7806
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007809 if (!did_echo_string_emsg)
7810 {
7811 /* Only give this message once for a recursive call to avoid
7812 * flooding the user with errors. And stop iterating over lists
7813 * and dicts. */
7814 did_echo_string_emsg = TRUE;
7815 EMSG(_("E724: variable nested too deep for displaying"));
7816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007818 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007819 }
7820 ++recurse;
7821
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822 switch (tv->v_type)
7823 {
7824 case VAR_FUNC:
7825 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 r = tv->vval.v_string;
7827 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007830 if (tv->vval.v_list == NULL)
7831 {
7832 *tofree = NULL;
7833 r = NULL;
7834 }
7835 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7836 {
7837 *tofree = NULL;
7838 r = (char_u *)"[...]";
7839 }
7840 else
7841 {
7842 tv->vval.v_list->lv_copyID = copyID;
7843 *tofree = list2string(tv, copyID);
7844 r = *tofree;
7845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007846 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007849 if (tv->vval.v_dict == NULL)
7850 {
7851 *tofree = NULL;
7852 r = NULL;
7853 }
7854 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7855 {
7856 *tofree = NULL;
7857 r = (char_u *)"{...}";
7858 }
7859 else
7860 {
7861 tv->vval.v_dict->dv_copyID = copyID;
7862 *tofree = dict2string(tv, copyID);
7863 r = *tofree;
7864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 case VAR_STRING:
7868 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 *tofree = NULL;
7870 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#ifdef FEAT_FLOAT
7874 case VAR_FLOAT:
7875 *tofree = NULL;
7876 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7877 r = numbuf;
7878 break;
7879#endif
7880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885
Bram Moolenaar8502c702014-06-17 12:51:16 +02007886 if (--recurse == 0)
7887 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007889}
7890
7891/*
7892 * Return a string with the string representation of a variable.
7893 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7894 * "numbuf" is used for a number.
7895 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007896 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 */
7898 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007899tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 char_u **tofree;
7902 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007903 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904{
7905 switch (tv->v_type)
7906 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 case VAR_FUNC:
7908 *tofree = string_quote(tv->vval.v_string, TRUE);
7909 return *tofree;
7910 case VAR_STRING:
7911 *tofree = string_quote(tv->vval.v_string, FALSE);
7912 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#ifdef FEAT_FLOAT
7914 case VAR_FLOAT:
7915 *tofree = NULL;
7916 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7917 return numbuf;
7918#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007919 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007926 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927}
7928
7929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 * Return string "str" in ' quotes, doubling ' characters.
7931 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 */
7934 static char_u *
7935string_quote(str, function)
7936 char_u *str;
7937 int function;
7938{
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 char_u *p, *r, *s;
7941
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 len = (function ? 13 : 3);
7943 if (str != NULL)
7944 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007945 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 for (p = str; *p != NUL; mb_ptr_adv(p))
7947 if (*p == '\'')
7948 ++len;
7949 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 s = r = alloc(len);
7951 if (r != NULL)
7952 {
7953 if (function)
7954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 r += 10;
7957 }
7958 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 if (str != NULL)
7961 for (p = str; *p != NUL; )
7962 {
7963 if (*p == '\'')
7964 *r++ = '\'';
7965 MB_COPY_CHAR(p, r);
7966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 if (function)
7969 *r++ = ')';
7970 *r++ = NUL;
7971 }
7972 return s;
7973}
7974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975#ifdef FEAT_FLOAT
7976/*
7977 * Convert the string "text" to a floating point number.
7978 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7979 * this always uses a decimal point.
7980 * Returns the length of the text that was consumed.
7981 */
7982 static int
7983string2float(text, value)
7984 char_u *text;
7985 float_T *value; /* result stored here */
7986{
7987 char *s = (char *)text;
7988 float_T f;
7989
7990 f = strtod(s, &s);
7991 *value = f;
7992 return (int)((char_u *)s - text);
7993}
7994#endif
7995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 * Get the value of an environment variable.
7998 * "arg" is pointing to the '$'. It is advanced to after the name.
7999 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008000 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 */
8002 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 int evaluate;
8007{
8008 char_u *string = NULL;
8009 int len;
8010 int cc;
8011 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008012 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 ++*arg;
8015 name = *arg;
8016 len = get_env_len(arg);
8017 if (evaluate)
8018 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008019 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008020 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008021
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 cc = name[len];
8023 name[len] = NUL;
8024 /* first try vim_getenv(), fast for normal environment vars */
8025 string = vim_getenv(name, &mustfree);
8026 if (string != NULL && *string != NUL)
8027 {
8028 if (!mustfree)
8029 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008031 else
8032 {
8033 if (mustfree)
8034 vim_free(string);
8035
8036 /* next try expanding things like $VIM and ${HOME} */
8037 string = expand_env_save(name - 1);
8038 if (string != NULL && *string == '$')
8039 {
8040 vim_free(string);
8041 string = NULL;
8042 }
8043 }
8044 name[len] = cc;
8045
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008046 rettv->v_type = VAR_STRING;
8047 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049
8050 return OK;
8051}
8052
8053/*
8054 * Array with names and number of arguments of all internal functions
8055 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8056 */
8057static struct fst
8058{
8059 char *f_name; /* function name */
8060 char f_min_argc; /* minimal number of arguments */
8061 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008063 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064} functions[] =
8065{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
8067 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008068 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008070 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008071 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"append", 2, 2, f_append},
8073 {"argc", 0, 0, f_argc},
8074 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008075 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008076 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008077#ifdef FEAT_FLOAT
8078 {"asin", 1, 1, f_asin}, /* WJMc */
8079#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008080 {"assert_equal", 2, 3, f_assert_equal},
8081 {"assert_false", 1, 2, f_assert_false},
8082 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008085 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008088 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"bufexists", 1, 1, f_bufexists},
8090 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8091 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8092 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8093 {"buflisted", 1, 1, f_buflisted},
8094 {"bufloaded", 1, 1, f_bufloaded},
8095 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008096 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"bufwinnr", 1, 1, f_bufwinnr},
8098 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008099 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008100 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008101 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#ifdef FEAT_FLOAT
8103 {"ceil", 1, 1, f_ceil},
8104#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008105 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008106 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008108 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008110#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008111 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008112 {"complete_add", 1, 1, f_complete_add},
8113 {"complete_check", 0, 0, f_complete_check},
8114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008117#ifdef FEAT_FLOAT
8118 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008119 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008121 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008123 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008124 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"delete", 1, 1, f_delete},
8126 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008127 {"diff_filler", 1, 1, f_diff_filler},
8128 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008129 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"eventhandler", 0, 0, f_eventhandler},
8133 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008134 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008136#ifdef FEAT_FLOAT
8137 {"exp", 1, 1, f_exp},
8138#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008139 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008140 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008141 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8143 {"filereadable", 1, 1, f_filereadable},
8144 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008145 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008146 {"finddir", 1, 3, f_finddir},
8147 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#ifdef FEAT_FLOAT
8149 {"float2nr", 1, 1, f_float2nr},
8150 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008151 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008153 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"fnamemodify", 2, 2, f_fnamemodify},
8155 {"foldclosed", 1, 1, f_foldclosed},
8156 {"foldclosedend", 1, 1, f_foldclosedend},
8157 {"foldlevel", 1, 1, f_foldlevel},
8158 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008159 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008161 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008162 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008164 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008165 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"getchar", 0, 1, f_getchar},
8167 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008168 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"getcmdline", 0, 0, f_getcmdline},
8170 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008171 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008172 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008173 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008175 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008176 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"getfsize", 1, 1, f_getfsize},
8178 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008179 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008180 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008181 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008182 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008183 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008184 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008185 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008186 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008188 {"gettabvar", 2, 3, f_gettabvar},
8189 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getwinposx", 0, 0, f_getwinposx},
8191 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008192 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008193 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008194 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008195 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008198 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008199 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8201 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8202 {"histadd", 2, 2, f_histadd},
8203 {"histdel", 1, 2, f_histdel},
8204 {"histget", 1, 2, f_histget},
8205 {"histnr", 1, 1, f_histnr},
8206 {"hlID", 1, 1, f_hlID},
8207 {"hlexists", 1, 1, f_hlexists},
8208 {"hostname", 0, 0, f_hostname},
8209 {"iconv", 3, 3, f_iconv},
8210 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008211 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008212 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008214 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"inputrestore", 0, 0, f_inputrestore},
8216 {"inputsave", 0, 0, f_inputsave},
8217 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008218 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008219 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008221 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008222 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008224 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008226 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"libcall", 3, 3, f_libcall},
8228 {"libcallnr", 3, 3, f_libcallnr},
8229 {"line", 1, 1, f_line},
8230 {"line2byte", 1, 1, f_line2byte},
8231 {"lispindent", 1, 1, f_lispindent},
8232 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008233#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008234 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008235 {"log10", 1, 1, f_log10},
8236#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008237#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008238 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008239#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008241 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008242 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008243 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008244 {"matchadd", 2, 5, f_matchadd},
8245 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008246 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008247 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008248 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008249 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008250 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008251 {"max", 1, 1, f_max},
8252 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008253#ifdef vim_mkdir
8254 {"mkdir", 1, 3, f_mkdir},
8255#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008257#ifdef FEAT_MZSCHEME
8258 {"mzeval", 1, 1, f_mzeval},
8259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008261 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008262 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008263 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008264#ifdef FEAT_FLOAT
8265 {"pow", 2, 2, f_pow},
8266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008268 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008269 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008270#ifdef FEAT_PYTHON3
8271 {"py3eval", 1, 1, f_py3eval},
8272#endif
8273#ifdef FEAT_PYTHON
8274 {"pyeval", 1, 1, f_pyeval},
8275#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008276 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008277 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008278 {"reltime", 0, 2, f_reltime},
8279 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"remote_expr", 2, 3, f_remote_expr},
8281 {"remote_foreground", 1, 1, f_remote_foreground},
8282 {"remote_peek", 1, 2, f_remote_peek},
8283 {"remote_read", 1, 1, f_remote_read},
8284 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008285 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008287 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008289 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008290#ifdef FEAT_FLOAT
8291 {"round", 1, 1, f_round},
8292#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008293 {"screenattr", 2, 2, f_screenattr},
8294 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008295 {"screencol", 0, 0, f_screencol},
8296 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008297 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008298 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008299 {"searchpair", 3, 7, f_searchpair},
8300 {"searchpairpos", 3, 7, f_searchpairpos},
8301 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"server2client", 2, 2, f_server2client},
8303 {"serverlist", 0, 0, f_serverlist},
8304 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008305 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"setcmdpos", 1, 1, f_setcmdpos},
8307 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008308 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008309 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008310 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008311 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008313 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008314 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008316#ifdef FEAT_CRYPT
8317 {"sha256", 1, 1, f_sha256},
8318#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008319 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008320 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#ifdef FEAT_FLOAT
8323 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008324 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008326 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008327 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008328 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008329 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008330 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008331#ifdef FEAT_FLOAT
8332 {"sqrt", 1, 1, f_sqrt},
8333 {"str2float", 1, 1, f_str2float},
8334#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008335 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008336 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008337 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338#ifdef HAVE_STRFTIME
8339 {"strftime", 1, 2, f_strftime},
8340#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"strlen", 1, 1, f_strlen},
8344 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008345 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008347 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008348 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"substitute", 4, 4, f_substitute},
8350 {"synID", 3, 3, f_synID},
8351 {"synIDattr", 2, 3, f_synIDattr},
8352 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008353 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008354 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008355 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008356 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008357 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008358 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008359 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008360 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008361 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008362#ifdef FEAT_FLOAT
8363 {"tan", 1, 1, f_tan},
8364 {"tanh", 1, 1, f_tanh},
8365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008367 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"tolower", 1, 1, f_tolower},
8369 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008370 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371#ifdef FEAT_FLOAT
8372 {"trunc", 1, 1, f_trunc},
8373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008375 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008376 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008377 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"virtcol", 1, 1, f_virtcol},
8380 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008381 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"winbufnr", 1, 1, f_winbufnr},
8383 {"wincol", 0, 0, f_wincol},
8384 {"winheight", 1, 1, f_winheight},
8385 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008386 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008388 {"winrestview", 1, 1, f_winrestview},
8389 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008391 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008392 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008393 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394};
8395
8396#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8397
8398/*
8399 * Function given to ExpandGeneric() to obtain the list of internal
8400 * or user defined function names.
8401 */
8402 char_u *
8403get_function_name(xp, idx)
8404 expand_T *xp;
8405 int idx;
8406{
8407 static int intidx = -1;
8408 char_u *name;
8409
8410 if (idx == 0)
8411 intidx = -1;
8412 if (intidx < 0)
8413 {
8414 name = get_user_func_name(xp, idx);
8415 if (name != NULL)
8416 return name;
8417 }
8418 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8419 {
8420 STRCPY(IObuff, functions[intidx].f_name);
8421 STRCAT(IObuff, "(");
8422 if (functions[intidx].f_max_argc == 0)
8423 STRCAT(IObuff, ")");
8424 return IObuff;
8425 }
8426
8427 return NULL;
8428}
8429
8430/*
8431 * Function given to ExpandGeneric() to obtain the list of internal or
8432 * user defined variable or function names.
8433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 char_u *
8435get_expr_name(xp, idx)
8436 expand_T *xp;
8437 int idx;
8438{
8439 static int intidx = -1;
8440 char_u *name;
8441
8442 if (idx == 0)
8443 intidx = -1;
8444 if (intidx < 0)
8445 {
8446 name = get_function_name(xp, idx);
8447 if (name != NULL)
8448 return name;
8449 }
8450 return get_user_var_name(xp, ++intidx);
8451}
8452
8453#endif /* FEAT_CMDL_COMPL */
8454
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008455#if defined(EBCDIC) || defined(PROTO)
8456/*
8457 * Compare struct fst by function name.
8458 */
8459 static int
8460compare_func_name(s1, s2)
8461 const void *s1;
8462 const void *s2;
8463{
8464 struct fst *p1 = (struct fst *)s1;
8465 struct fst *p2 = (struct fst *)s2;
8466
8467 return STRCMP(p1->f_name, p2->f_name);
8468}
8469
8470/*
8471 * Sort the function table by function name.
8472 * The sorting of the table above is ASCII dependant.
8473 * On machines using EBCDIC we have to sort it.
8474 */
8475 static void
8476sortFunctions()
8477{
8478 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8479
8480 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8481}
8482#endif
8483
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485/*
8486 * Find internal function in table above.
8487 * Return index, or -1 if not found
8488 */
8489 static int
8490find_internal_func(name)
8491 char_u *name; /* name of the function */
8492{
8493 int first = 0;
8494 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8495 int cmp;
8496 int x;
8497
8498 /*
8499 * Find the function name in the table. Binary search.
8500 */
8501 while (first <= last)
8502 {
8503 x = first + ((unsigned)(last - first) >> 1);
8504 cmp = STRCMP(name, functions[x].f_name);
8505 if (cmp < 0)
8506 last = x - 1;
8507 else if (cmp > 0)
8508 first = x + 1;
8509 else
8510 return x;
8511 }
8512 return -1;
8513}
8514
8515/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8517 * name it contains, otherwise return "name".
8518 */
8519 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008520deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 char_u *name;
8522 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008523 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524{
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 int cc;
8527
8528 cc = name[*lenp];
8529 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008530 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 {
8536 *lenp = 0;
8537 return (char_u *)""; /* just in case */
8538 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008539 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 }
8542
8543 return name;
8544}
8545
8546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 * Allocate a variable for the result of a function.
8548 * Return OK or FAIL.
8549 */
8550 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008551get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8552 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 char_u *name; /* name of the function */
8554 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 char_u **arg; /* argument, pointing to the '(' */
8557 linenr_T firstline; /* first line of range */
8558 linenr_T lastline; /* last line of range */
8559 int *doesrange; /* return: function handled range */
8560 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 char_u *argp;
8564 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008565 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 int argcount = 0; /* number of arguments found */
8567
8568 /*
8569 * Get the arguments.
8570 */
8571 argp = *arg;
8572 while (argcount < MAX_FUNC_ARGS)
8573 {
8574 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8575 if (*argp == ')' || *argp == ',' || *argp == NUL)
8576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8578 {
8579 ret = FAIL;
8580 break;
8581 }
8582 ++argcount;
8583 if (*argp != ',')
8584 break;
8585 }
8586 if (*argp == ')')
8587 ++argp;
8588 else
8589 ret = FAIL;
8590
8591 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008593 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 {
8596 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008597 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008599 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605 *arg = skipwhite(argp);
8606 return ret;
8607}
8608
8609
8610/*
8611 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008612 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008613 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 */
8615 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008616call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008618 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008620 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008622 typval_T *argvars; /* vars for arguments, must have "argcount"
8623 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 linenr_T firstline; /* first line of range */
8625 linenr_T lastline; /* last line of range */
8626 int *doesrange; /* return: function handled range */
8627 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008628 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#define ERROR_UNKNOWN 0
8632#define ERROR_TOOMANY 1
8633#define ERROR_TOOFEW 2
8634#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008635#define ERROR_DICT 4
8636#define ERROR_NONE 5
8637#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 int error = ERROR_NONE;
8639 int i;
8640 int llen;
8641 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#define FLEN_FIXED 40
8643 char_u fname_buf[FLEN_FIXED + 1];
8644 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008645 char_u *name;
8646
8647 /* Make a copy of the name, if it comes from a funcref variable it could
8648 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008649 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008650 if (name == NULL)
8651 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652
8653 /*
8654 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8655 * Change <SNR>123_name() to K_SNR 123_name().
8656 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 llen = eval_fname_script(name);
8659 if (llen > 0)
8660 {
8661 fname_buf[0] = K_SPECIAL;
8662 fname_buf[1] = KS_EXTRA;
8663 fname_buf[2] = (int)KE_SNR;
8664 i = 3;
8665 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8666 {
8667 if (current_SID <= 0)
8668 error = ERROR_SCRIPT;
8669 else
8670 {
8671 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8672 i = (int)STRLEN(fname_buf);
8673 }
8674 }
8675 if (i + STRLEN(name + llen) < FLEN_FIXED)
8676 {
8677 STRCPY(fname_buf + i, name + llen);
8678 fname = fname_buf;
8679 }
8680 else
8681 {
8682 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8683 if (fname == NULL)
8684 error = ERROR_OTHER;
8685 else
8686 {
8687 mch_memmove(fname, fname_buf, (size_t)i);
8688 STRCPY(fname + i, name + llen);
8689 }
8690 }
8691 }
8692 else
8693 fname = name;
8694
8695 *doesrange = FALSE;
8696
8697
8698 /* execute the function if no errors detected and executing */
8699 if (evaluate && error == ERROR_NONE)
8700 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 char_u *rfname = fname;
8702
8703 /* Ignore "g:" before a function name. */
8704 if (fname[0] == 'g' && fname[1] == ':')
8705 rfname = fname + 2;
8706
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008707 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8708 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 error = ERROR_UNKNOWN;
8710
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
8713 /*
8714 * User defined function.
8715 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008716 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 /* Trigger FuncUndefined event, may load the function. */
8720 if (fp == NULL
8721 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008722 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008723 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008725 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008726 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 }
8728#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008730 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731 {
8732 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008733 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008734 }
8735
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 if (fp != NULL)
8737 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008744 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008745 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 else
8747 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008748 int did_save_redo = FALSE;
8749
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 /*
8751 * Call the user function.
8752 * Save and restore search patterns, script variables and
8753 * redo buffer.
8754 */
8755 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008756#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008757 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008758#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008759 {
8760 saveRedobuff();
8761 did_save_redo = TRUE;
8762 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008763 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008765 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008766 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8767 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8768 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008769 /* Function was unreferenced while being used, free it
8770 * now. */
8771 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008772 if (did_save_redo)
8773 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 restore_search_patterns();
8775 error = ERROR_NONE;
8776 }
8777 }
8778 }
8779 else
8780 {
8781 /*
8782 * Find the function name in the table, call its implementation.
8783 */
8784 i = find_internal_func(fname);
8785 if (i >= 0)
8786 {
8787 if (argcount < functions[i].f_min_argc)
8788 error = ERROR_TOOFEW;
8789 else if (argcount > functions[i].f_max_argc)
8790 error = ERROR_TOOMANY;
8791 else
8792 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008793 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 error = ERROR_NONE;
8796 }
8797 }
8798 }
8799 /*
8800 * The function call (or "FuncUndefined" autocommand sequence) might
8801 * have been aborted by an error, an interrupt, or an explicitly thrown
8802 * exception that has not been caught so far. This situation can be
8803 * tested for by calling aborting(). For an error in an internal
8804 * function or for the "E132" error in call_user_func(), however, the
8805 * throw point at which the "force_abort" flag (temporarily reset by
8806 * emsg()) is normally updated has not been reached yet. We need to
8807 * update that flag first to make aborting() reliable.
8808 */
8809 update_force_abort();
8810 }
8811 if (error == ERROR_NONE)
8812 ret = OK;
8813
8814 /*
8815 * Report an error unless the argument evaluation or function call has been
8816 * cancelled due to an aborting error, an interrupt, or an exception.
8817 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008818 if (!aborting())
8819 {
8820 switch (error)
8821 {
8822 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008823 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008824 break;
8825 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008826 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 break;
8828 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008829 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 name);
8831 break;
8832 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008833 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008834 name);
8835 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008836 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008837 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008838 name);
8839 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008840 }
8841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (fname != name && fname != fname_buf)
8844 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008845 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
8847 return ret;
8848}
8849
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008850/*
8851 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008852 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008853 */
8854 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008855emsg_funcname(ermsg, name)
8856 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008857 char_u *name;
8858{
8859 char_u *p;
8860
8861 if (*name == K_SPECIAL)
8862 p = concat_str((char_u *)"<SNR>", name + 3);
8863 else
8864 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008865 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008866 if (p != name)
8867 vim_free(p);
8868}
8869
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008870/*
8871 * Return TRUE for a non-zero Number and a non-empty String.
8872 */
8873 static int
8874non_zero_arg(argvars)
8875 typval_T *argvars;
8876{
8877 return ((argvars[0].v_type == VAR_NUMBER
8878 && argvars[0].vval.v_number != 0)
8879 || (argvars[0].v_type == VAR_STRING
8880 && argvars[0].vval.v_string != NULL
8881 && *argvars[0].vval.v_string != NUL));
8882}
8883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884/*********************************************
8885 * Implementation of the built-in functions
8886 */
8887
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008888#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008889static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8890
8891/*
8892 * Get the float value of "argvars[0]" into "f".
8893 * Returns FAIL when the argument is not a Number or Float.
8894 */
8895 static int
8896get_float_arg(argvars, f)
8897 typval_T *argvars;
8898 float_T *f;
8899{
8900 if (argvars[0].v_type == VAR_FLOAT)
8901 {
8902 *f = argvars[0].vval.v_float;
8903 return OK;
8904 }
8905 if (argvars[0].v_type == VAR_NUMBER)
8906 {
8907 *f = (float_T)argvars[0].vval.v_number;
8908 return OK;
8909 }
8910 EMSG(_("E808: Number or Float required"));
8911 return FAIL;
8912}
8913
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008914/*
8915 * "abs(expr)" function
8916 */
8917 static void
8918f_abs(argvars, rettv)
8919 typval_T *argvars;
8920 typval_T *rettv;
8921{
8922 if (argvars[0].v_type == VAR_FLOAT)
8923 {
8924 rettv->v_type = VAR_FLOAT;
8925 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8926 }
8927 else
8928 {
8929 varnumber_T n;
8930 int error = FALSE;
8931
8932 n = get_tv_number_chk(&argvars[0], &error);
8933 if (error)
8934 rettv->vval.v_number = -1;
8935 else if (n > 0)
8936 rettv->vval.v_number = n;
8937 else
8938 rettv->vval.v_number = -n;
8939 }
8940}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008941
8942/*
8943 * "acos()" function
8944 */
8945 static void
8946f_acos(argvars, rettv)
8947 typval_T *argvars;
8948 typval_T *rettv;
8949{
8950 float_T f;
8951
8952 rettv->v_type = VAR_FLOAT;
8953 if (get_float_arg(argvars, &f) == OK)
8954 rettv->vval.v_float = acos(f);
8955 else
8956 rettv->vval.v_float = 0.0;
8957}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008958#endif
8959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008961 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 */
8963 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008964f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967{
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008973 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008974 && !tv_check_lock(l->lv_lock,
8975 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008976 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008978 }
8979 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980 EMSG(_(e_listreq));
8981}
8982
8983/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008984 * "and(expr, expr)" function
8985 */
8986 static void
8987f_and(argvars, rettv)
8988 typval_T *argvars;
8989 typval_T *rettv;
8990{
8991 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8992 & get_tv_number_chk(&argvars[1], NULL);
8993}
8994
8995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 * "append(lnum, string/list)" function
8997 */
8998 static void
8999f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *argvars;
9001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002{
9003 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 list_T *l = NULL;
9006 listitem_T *li = NULL;
9007 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 long added = 0;
9009
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009010 /* When coming here from Insert mode, sync undo, so that this can be
9011 * undone separately from what was previously inserted. */
9012 if (u_sync_once == 2)
9013 {
9014 u_sync_once = 1; /* notify that u_sync() was called */
9015 u_sync(TRUE);
9016 }
9017
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 lnum = get_tv_lnum(argvars);
9019 if (lnum >= 0
9020 && lnum <= curbuf->b_ml.ml_line_count
9021 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009024 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009025 l = argvars[1].vval.v_list;
9026 if (l == NULL)
9027 return;
9028 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009029 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030 for (;;)
9031 {
9032 if (l == NULL)
9033 tv = &argvars[1]; /* append a string */
9034 else if (li == NULL)
9035 break; /* end of list */
9036 else
9037 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 line = get_tv_string_chk(tv);
9039 if (line == NULL) /* type error */
9040 {
9041 rettv->vval.v_number = 1; /* Failed */
9042 break;
9043 }
9044 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045 ++added;
9046 if (l == NULL)
9047 break;
9048 li = li->li_next;
9049 }
9050
9051 appended_lines_mark(lnum, added);
9052 if (curwin->w_cursor.lnum > lnum)
9053 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 else
9056 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
9058
9059/*
9060 * "argc()" function
9061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068}
9069
9070/*
9071 * "argidx()" function
9072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009075 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079}
9080
9081/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009082 * "arglistid()" function
9083 */
9084 static void
9085f_arglistid(argvars, rettv)
9086 typval_T *argvars UNUSED;
9087 typval_T *rettv;
9088{
9089 win_T *wp;
9090 tabpage_T *tp = NULL;
9091 long n;
9092
9093 rettv->vval.v_number = -1;
9094 if (argvars[0].v_type != VAR_UNKNOWN)
9095 {
9096 if (argvars[1].v_type != VAR_UNKNOWN)
9097 {
9098 n = get_tv_number(&argvars[1]);
9099 if (n >= 0)
9100 tp = find_tabpage(n);
9101 }
9102 else
9103 tp = curtab;
9104
9105 if (tp != NULL)
9106 {
9107 wp = find_win_by_nr(&argvars[0], tp);
9108 if (wp != NULL)
9109 rettv->vval.v_number = wp->w_alist->id;
9110 }
9111 }
9112 else
9113 rettv->vval.v_number = curwin->w_alist->id;
9114}
9115
9116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 * "argv(nr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 int idx;
9125
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009126 if (argvars[0].v_type != VAR_UNKNOWN)
9127 {
9128 idx = get_tv_number_chk(&argvars[0], NULL);
9129 if (idx >= 0 && idx < ARGCOUNT)
9130 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9131 else
9132 rettv->vval.v_string = NULL;
9133 rettv->v_type = VAR_STRING;
9134 }
9135 else if (rettv_list_alloc(rettv) == OK)
9136 for (idx = 0; idx < ARGCOUNT; ++idx)
9137 list_append_string(rettv->vval.v_list,
9138 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139}
9140
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009141static void prepare_assert_error __ARGS((garray_T*gap));
9142static 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));
9143static void assert_error __ARGS((garray_T *gap));
9144static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9145
9146/*
9147 * Prepare "gap" for an assert error and add the sourcing position.
9148 */
9149 static void
9150prepare_assert_error(gap)
9151 garray_T *gap;
9152{
9153 char buf[NUMBUFLEN];
9154
9155 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009156 if (sourcing_name != NULL)
9157 {
9158 ga_concat(gap, sourcing_name);
9159 if (sourcing_lnum > 0)
9160 ga_concat(gap, (char_u *)" ");
9161 }
9162 if (sourcing_lnum > 0)
9163 {
9164 sprintf(buf, "line %ld", (long)sourcing_lnum);
9165 ga_concat(gap, (char_u *)buf);
9166 }
9167 if (sourcing_name != NULL || sourcing_lnum > 0)
9168 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009169}
9170
9171/*
9172 * Fill "gap" with information about an assert error.
9173 */
9174 static void
9175fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9176 garray_T *gap;
9177 typval_T *opt_msg_tv;
9178 char_u *exp_str;
9179 typval_T *exp_tv;
9180 typval_T *got_tv;
9181{
9182 char_u numbuf[NUMBUFLEN];
9183 char_u *tofree;
9184
9185 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9186 {
9187 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9188 vim_free(tofree);
9189 }
9190 else
9191 {
9192 ga_concat(gap, (char_u *)"Expected ");
9193 if (exp_str == NULL)
9194 {
9195 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9196 vim_free(tofree);
9197 }
9198 else
9199 ga_concat(gap, exp_str);
9200 ga_concat(gap, (char_u *)" but got ");
9201 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9202 vim_free(tofree);
9203 }
9204}
Bram Moolenaar43345542015-11-29 17:35:35 +01009205
9206/*
9207 * Add an assert error to v:errors.
9208 */
9209 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009210assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009211 garray_T *gap;
9212{
9213 struct vimvar *vp = &vimvars[VV_ERRORS];
9214
9215 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9216 /* Make sure v:errors is a list. */
9217 set_vim_var_list(VV_ERRORS, list_alloc());
9218 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9219}
9220
Bram Moolenaar43345542015-11-29 17:35:35 +01009221/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009222 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009223 */
9224 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009225f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009226 typval_T *argvars;
9227 typval_T *rettv UNUSED;
9228{
9229 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009230
9231 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9232 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233 prepare_assert_error(&ga);
9234 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9235 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009236 ga_clear(&ga);
9237 }
9238}
9239
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009240/*
9241 * Common for assert_true() and assert_false().
9242 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009243 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 typval_T *argvars;
9246 int isTrue;
9247{
9248 int error = FALSE;
9249 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009250
9251 if (argvars[0].v_type != VAR_NUMBER
9252 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9253 || error)
9254 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009255 prepare_assert_error(&ga);
9256 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009257 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009258 NULL, &argvars[0]);
9259 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009260 ga_clear(&ga);
9261 }
9262}
9263
9264/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009266 */
9267 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009269 typval_T *argvars;
9270 typval_T *rettv UNUSED;
9271{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009273}
9274
9275/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009276 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009277 */
9278 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009280 typval_T *argvars;
9281 typval_T *rettv UNUSED;
9282{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009283 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009284}
9285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009286#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009287/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009288 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009290 static void
9291f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009293 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009294{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009295 float_T f;
9296
9297 rettv->v_type = VAR_FLOAT;
9298 if (get_float_arg(argvars, &f) == OK)
9299 rettv->vval.v_float = asin(f);
9300 else
9301 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009302}
9303
9304/*
9305 * "atan()" function
9306 */
9307 static void
9308f_atan(argvars, rettv)
9309 typval_T *argvars;
9310 typval_T *rettv;
9311{
9312 float_T f;
9313
9314 rettv->v_type = VAR_FLOAT;
9315 if (get_float_arg(argvars, &f) == OK)
9316 rettv->vval.v_float = atan(f);
9317 else
9318 rettv->vval.v_float = 0.0;
9319}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320
9321/*
9322 * "atan2()" function
9323 */
9324 static void
9325f_atan2(argvars, rettv)
9326 typval_T *argvars;
9327 typval_T *rettv;
9328{
9329 float_T fx, fy;
9330
9331 rettv->v_type = VAR_FLOAT;
9332 if (get_float_arg(argvars, &fx) == OK
9333 && get_float_arg(&argvars[1], &fy) == OK)
9334 rettv->vval.v_float = atan2(fx, fy);
9335 else
9336 rettv->vval.v_float = 0.0;
9337}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#endif
9339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340/*
9341 * "browse(save, title, initdir, default)" function
9342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348#ifdef FEAT_BROWSE
9349 int save;
9350 char_u *title;
9351 char_u *initdir;
9352 char_u *defname;
9353 char_u buf[NUMBUFLEN];
9354 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009357 save = get_tv_number_chk(&argvars[0], &error);
9358 title = get_tv_string_chk(&argvars[1]);
9359 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9360 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009362 if (error || title == NULL || initdir == NULL || defname == NULL)
9363 rettv->vval.v_string = NULL;
9364 else
9365 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009366 do_browse(save ? BROWSE_SAVE : 0,
9367 title, defname, NULL, initdir, NULL, curbuf);
9368#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009370#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009372}
9373
9374/*
9375 * "browsedir(title, initdir)" function
9376 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009379 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009381{
9382#ifdef FEAT_BROWSE
9383 char_u *title;
9384 char_u *initdir;
9385 char_u buf[NUMBUFLEN];
9386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 title = get_tv_string_chk(&argvars[0]);
9388 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 if (title == NULL || initdir == NULL)
9391 rettv->vval.v_string = NULL;
9392 else
9393 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009394 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399}
9400
Bram Moolenaar33570922005-01-25 22:26:29 +00009401static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403/*
9404 * Find a buffer by number or exact name.
9405 */
9406 static buf_T *
9407find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009412 if (avar->v_type == VAR_NUMBER)
9413 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009414 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009416 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009417 if (buf == NULL)
9418 {
9419 /* No full path name match, try a match with a URL or a "nofile"
9420 * buffer, these don't use the full path. */
9421 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9422 if (buf->b_fname != NULL
9423 && (path_with_url(buf->b_fname)
9424#ifdef FEAT_QUICKFIX
9425 || bt_nofile(buf)
9426#endif
9427 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009428 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009429 break;
9430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
9432 return buf;
9433}
9434
9435/*
9436 * "bufexists(expr)" function
9437 */
9438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *argvars;
9441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444}
9445
9446/*
9447 * "buflisted(expr)" function
9448 */
9449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454 buf_T *buf;
9455
9456 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458}
9459
9460/*
9461 * "bufloaded(expr)" function
9462 */
9463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 typval_T *argvars;
9466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 buf_T *buf;
9469
9470 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009474static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476/*
9477 * Get buffer by number or pattern.
9478 */
9479 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009480get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009482 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 int save_magic;
9486 char_u *save_cpo;
9487 buf_T *buf;
9488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 if (tv->v_type == VAR_NUMBER)
9490 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009491 if (tv->v_type != VAR_STRING)
9492 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (name == NULL || *name == NUL)
9494 return curbuf;
9495 if (name[0] == '$' && name[1] == NUL)
9496 return lastbuf;
9497
9498 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9499 save_magic = p_magic;
9500 p_magic = TRUE;
9501 save_cpo = p_cpo;
9502 p_cpo = (char_u *)"";
9503
9504 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009505 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
9507 p_magic = save_magic;
9508 p_cpo = save_cpo;
9509
9510 /* If not found, try expanding the name, like done for bufexists(). */
9511 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513
9514 return buf;
9515}
9516
9517/*
9518 * "bufname(expr)" function
9519 */
9520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 typval_T *argvars;
9523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525 buf_T *buf;
9526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009529 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 --emsg_off;
9536}
9537
9538/*
9539 * "bufnr(expr)" function
9540 */
9541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009543 typval_T *argvars;
9544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009547 int error = FALSE;
9548 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009552 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009553 --emsg_off;
9554
9555 /* If the buffer isn't found and the second argument is not zero create a
9556 * new buffer. */
9557 if (buf == NULL
9558 && argvars[1].v_type != VAR_UNKNOWN
9559 && get_tv_number_chk(&argvars[1], &error) != 0
9560 && !error
9561 && (name = get_tv_string_chk(&argvars[0])) != NULL
9562 && !error)
9563 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9564
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
9572 * "bufwinnr(nr)" function
9573 */
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579#ifdef FEAT_WINDOWS
9580 win_T *wp;
9581 int winnr = 0;
9582#endif
9583 buf_T *buf;
9584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009587 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#ifdef FEAT_WINDOWS
9589 for (wp = firstwin; wp; wp = wp->w_next)
9590 {
9591 ++winnr;
9592 if (wp->w_buffer == buf)
9593 break;
9594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598#endif
9599 --emsg_off;
9600}
9601
9602/*
9603 * "byte2line(byte)" function
9604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612#else
9613 long boff = 0;
9614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 (linenr_T)0, &boff);
9621#endif
9622}
9623
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009624 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009625byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009628 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009629{
9630#ifdef FEAT_MBYTE
9631 char_u *t;
9632#endif
9633 char_u *str;
9634 long idx;
9635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 str = get_tv_string_chk(&argvars[0]);
9637 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009640 return;
9641
9642#ifdef FEAT_MBYTE
9643 t = str;
9644 for ( ; idx > 0; idx--)
9645 {
9646 if (*t == NUL) /* EOL reached */
9647 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009648 if (enc_utf8 && comp)
9649 t += utf_ptr2len(t);
9650 else
9651 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009652 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009653 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009654#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009655 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009657#endif
9658}
9659
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009660/*
9661 * "byteidx()" function
9662 */
9663 static void
9664f_byteidx(argvars, rettv)
9665 typval_T *argvars;
9666 typval_T *rettv;
9667{
9668 byteidx(argvars, rettv, FALSE);
9669}
9670
9671/*
9672 * "byteidxcomp()" function
9673 */
9674 static void
9675f_byteidxcomp(argvars, rettv)
9676 typval_T *argvars;
9677 typval_T *rettv;
9678{
9679 byteidx(argvars, rettv, TRUE);
9680}
9681
Bram Moolenaardb913952012-06-29 12:54:53 +02009682 int
9683func_call(name, args, selfdict, rettv)
9684 char_u *name;
9685 typval_T *args;
9686 dict_T *selfdict;
9687 typval_T *rettv;
9688{
9689 listitem_T *item;
9690 typval_T argv[MAX_FUNC_ARGS + 1];
9691 int argc = 0;
9692 int dummy;
9693 int r = 0;
9694
9695 for (item = args->vval.v_list->lv_first; item != NULL;
9696 item = item->li_next)
9697 {
9698 if (argc == MAX_FUNC_ARGS)
9699 {
9700 EMSG(_("E699: Too many arguments"));
9701 break;
9702 }
9703 /* Make a copy of each argument. This is needed to be able to set
9704 * v_lock to VAR_FIXED in the copy without changing the original list.
9705 */
9706 copy_tv(&item->li_tv, &argv[argc++]);
9707 }
9708
9709 if (item == NULL)
9710 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9711 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9712 &dummy, TRUE, selfdict);
9713
9714 /* Free the arguments. */
9715 while (argc > 0)
9716 clear_tv(&argv[--argc]);
9717
9718 return r;
9719}
9720
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009721/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009722 * "call(func, arglist)" function
9723 */
9724 static void
9725f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728{
9729 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009731
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009732 if (argvars[1].v_type != VAR_LIST)
9733 {
9734 EMSG(_(e_listreq));
9735 return;
9736 }
9737 if (argvars[1].vval.v_list == NULL)
9738 return;
9739
9740 if (argvars[0].v_type == VAR_FUNC)
9741 func = argvars[0].vval.v_string;
9742 else
9743 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 if (*func == NUL)
9745 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009746
Bram Moolenaare9a41262005-01-15 22:18:47 +00009747 if (argvars[2].v_type != VAR_UNKNOWN)
9748 {
9749 if (argvars[2].v_type != VAR_DICT)
9750 {
9751 EMSG(_(e_dictreq));
9752 return;
9753 }
9754 selfdict = argvars[2].vval.v_dict;
9755 }
9756
Bram Moolenaardb913952012-06-29 12:54:53 +02009757 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009758}
9759
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009760#ifdef FEAT_FLOAT
9761/*
9762 * "ceil({float})" function
9763 */
9764 static void
9765f_ceil(argvars, rettv)
9766 typval_T *argvars;
9767 typval_T *rettv;
9768{
9769 float_T f;
9770
9771 rettv->v_type = VAR_FLOAT;
9772 if (get_float_arg(argvars, &f) == OK)
9773 rettv->vval.v_float = ceil(f);
9774 else
9775 rettv->vval.v_float = 0.0;
9776}
9777#endif
9778
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009779/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009780 * "changenr()" function
9781 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009782 static void
9783f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009784 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009785 typval_T *rettv;
9786{
9787 rettv->vval.v_number = curbuf->b_u_seq_cur;
9788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "char2nr(string)" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798#ifdef FEAT_MBYTE
9799 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009800 {
9801 int utf8 = 0;
9802
9803 if (argvars[1].v_type != VAR_UNKNOWN)
9804 utf8 = get_tv_number_chk(&argvars[1], NULL);
9805
9806 if (utf8)
9807 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9808 else
9809 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 else
9812#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814}
9815
9816/*
9817 * "cindent(lnum)" function
9818 */
9819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009821 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824#ifdef FEAT_CINDENT
9825 pos_T pos;
9826 linenr_T lnum;
9827
9828 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9831 {
9832 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 curwin->w_cursor = pos;
9835 }
9836 else
9837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839}
9840
9841/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009842 * "clearmatches()" function
9843 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009844 static void
9845f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009846 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009847 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009848{
9849#ifdef FEAT_SEARCH_EXTRA
9850 clear_matches(curwin);
9851#endif
9852}
9853
9854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 * "col(string)" function
9856 */
9857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 typval_T *argvars;
9860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
9862 colnr_T col = 0;
9863 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009864 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009866 fp = var2fpos(&argvars[0], FALSE, &fnum);
9867 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 {
9869 if (fp->col == MAXCOL)
9870 {
9871 /* '> can be MAXCOL, get the length of the line then */
9872 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009873 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 else
9875 col = MAXCOL;
9876 }
9877 else
9878 {
9879 col = fp->col + 1;
9880#ifdef FEAT_VIRTUALEDIT
9881 /* col(".") when the cursor is on the NUL at the end of the line
9882 * because of "coladd" can be seen as an extra column. */
9883 if (virtual_active() && fp == &curwin->w_cursor)
9884 {
9885 char_u *p = ml_get_cursor();
9886
9887 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9888 curwin->w_virtcol - curwin->w_cursor.coladd))
9889 {
9890# ifdef FEAT_MBYTE
9891 int l;
9892
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009893 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 col += l;
9895# else
9896 if (*p != NUL && p[1] == NUL)
9897 ++col;
9898# endif
9899 }
9900 }
9901#endif
9902 }
9903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
Bram Moolenaar572cb562005-08-05 21:35:02 +00009907#if defined(FEAT_INS_EXPAND)
9908/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009909 * "complete()" function
9910 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009911 static void
9912f_complete(argvars, rettv)
9913 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009914 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009915{
9916 int startcol;
9917
9918 if ((State & INSERT) == 0)
9919 {
9920 EMSG(_("E785: complete() can only be used in Insert mode"));
9921 return;
9922 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009923
9924 /* Check for undo allowed here, because if something was already inserted
9925 * the line was already saved for undo and this check isn't done. */
9926 if (!undo_allowed())
9927 return;
9928
Bram Moolenaarade00832006-03-10 21:46:58 +00009929 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9930 {
9931 EMSG(_(e_invarg));
9932 return;
9933 }
9934
9935 startcol = get_tv_number_chk(&argvars[0], NULL);
9936 if (startcol <= 0)
9937 return;
9938
9939 set_completion(startcol - 1, argvars[1].vval.v_list);
9940}
9941
9942/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009943 * "complete_add()" function
9944 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009945 static void
9946f_complete_add(argvars, rettv)
9947 typval_T *argvars;
9948 typval_T *rettv;
9949{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009950 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009951}
9952
9953/*
9954 * "complete_check()" function
9955 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009956 static void
9957f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009958 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009959 typval_T *rettv;
9960{
9961 int saved = RedrawingDisabled;
9962
9963 RedrawingDisabled = 0;
9964 ins_compl_check_keys(0);
9965 rettv->vval.v_number = compl_interrupted;
9966 RedrawingDisabled = saved;
9967}
9968#endif
9969
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970/*
9971 * "confirm(message, buttons[, default [, type]])" function
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009975 typval_T *argvars UNUSED;
9976 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9979 char_u *message;
9980 char_u *buttons = NULL;
9981 char_u buf[NUMBUFLEN];
9982 char_u buf2[NUMBUFLEN];
9983 int def = 1;
9984 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 char_u *typestr;
9986 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 message = get_tv_string_chk(&argvars[0]);
9989 if (message == NULL)
9990 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009991 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9994 if (buttons == NULL)
9995 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009998 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009999 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10002 if (typestr == NULL)
10003 error = TRUE;
10004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010006 switch (TOUPPER_ASC(*typestr))
10007 {
10008 case 'E': type = VIM_ERROR; break;
10009 case 'Q': type = VIM_QUESTION; break;
10010 case 'I': type = VIM_INFO; break;
10011 case 'W': type = VIM_WARNING; break;
10012 case 'G': type = VIM_GENERIC; break;
10013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015 }
10016 }
10017 }
10018
10019 if (buttons == NULL || *buttons == NUL)
10020 buttons = (char_u *)_("&Ok");
10021
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010022 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010024 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025#endif
10026}
10027
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010028/*
10029 * "copy()" function
10030 */
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010035{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010036 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010037}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010039#ifdef FEAT_FLOAT
10040/*
10041 * "cos()" function
10042 */
10043 static void
10044f_cos(argvars, rettv)
10045 typval_T *argvars;
10046 typval_T *rettv;
10047{
10048 float_T f;
10049
10050 rettv->v_type = VAR_FLOAT;
10051 if (get_float_arg(argvars, &f) == OK)
10052 rettv->vval.v_float = cos(f);
10053 else
10054 rettv->vval.v_float = 0.0;
10055}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010056
10057/*
10058 * "cosh()" function
10059 */
10060 static void
10061f_cosh(argvars, rettv)
10062 typval_T *argvars;
10063 typval_T *rettv;
10064{
10065 float_T f;
10066
10067 rettv->v_type = VAR_FLOAT;
10068 if (get_float_arg(argvars, &f) == OK)
10069 rettv->vval.v_float = cosh(f);
10070 else
10071 rettv->vval.v_float = 0.0;
10072}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010073#endif
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010076 * "count()" function
10077 */
10078 static void
10079f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010082{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010083 long n = 0;
10084 int ic = FALSE;
10085
Bram Moolenaare9a41262005-01-15 22:18:47 +000010086 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010087 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 listitem_T *li;
10089 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010090 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010091
Bram Moolenaare9a41262005-01-15 22:18:47 +000010092 if ((l = argvars[0].vval.v_list) != NULL)
10093 {
10094 li = l->lv_first;
10095 if (argvars[2].v_type != VAR_UNKNOWN)
10096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 int error = FALSE;
10098
10099 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010100 if (argvars[3].v_type != VAR_UNKNOWN)
10101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 idx = get_tv_number_chk(&argvars[3], &error);
10103 if (!error)
10104 {
10105 li = list_find(l, idx);
10106 if (li == NULL)
10107 EMSGN(_(e_listidx), idx);
10108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010109 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 if (error)
10111 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010112 }
10113
10114 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010115 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010116 ++n;
10117 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 else if (argvars[0].v_type == VAR_DICT)
10120 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 int todo;
10122 dict_T *d;
10123 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010124
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010125 if ((d = argvars[0].vval.v_dict) != NULL)
10126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 int error = FALSE;
10128
Bram Moolenaare9a41262005-01-15 22:18:47 +000010129 if (argvars[2].v_type != VAR_UNKNOWN)
10130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010132 if (argvars[3].v_type != VAR_UNKNOWN)
10133 EMSG(_(e_invarg));
10134 }
10135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010136 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010137 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010138 {
10139 if (!HASHITEM_EMPTY(hi))
10140 {
10141 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010142 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010143 ++n;
10144 }
10145 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010146 }
10147 }
10148 else
10149 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010150 rettv->vval.v_number = n;
10151}
10152
10153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10155 *
10156 * Checks the existence of a cscope connection.
10157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010160 typval_T *argvars UNUSED;
10161 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
10163#ifdef FEAT_CSCOPE
10164 int num = 0;
10165 char_u *dbpath = NULL;
10166 char_u *prepend = NULL;
10167 char_u buf[NUMBUFLEN];
10168
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010169 if (argvars[0].v_type != VAR_UNKNOWN
10170 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 num = (int)get_tv_number(&argvars[0]);
10173 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010174 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 }
10177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179#endif
10180}
10181
10182/*
10183 * "cursor(lnum, col)" function
10184 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010185 * Moves the cursor to the specified line and column.
10186 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010190 typval_T *argvars;
10191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192{
10193 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010194#ifdef FEAT_VIRTUALEDIT
10195 long coladd = 0;
10196#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010197 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010199 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010200 if (argvars[1].v_type == VAR_UNKNOWN)
10201 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010202 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010203 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010204
Bram Moolenaar493c1782014-05-28 14:34:46 +020010205 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010206 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010207 line = pos.lnum;
10208 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010209#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010210 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010211#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010212 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010213 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010214 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010215 set_curswant = FALSE;
10216 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010217 }
10218 else
10219 {
10220 line = get_tv_lnum(argvars);
10221 col = get_tv_number_chk(&argvars[1], NULL);
10222#ifdef FEAT_VIRTUALEDIT
10223 if (argvars[2].v_type != VAR_UNKNOWN)
10224 coladd = get_tv_number_chk(&argvars[2], NULL);
10225#endif
10226 }
10227 if (line < 0 || col < 0
10228#ifdef FEAT_VIRTUALEDIT
10229 || coladd < 0
10230#endif
10231 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 if (line > 0)
10234 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 if (col > 0)
10236 curwin->w_cursor.col = col - 1;
10237#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010238 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239#endif
10240
10241 /* Make sure the cursor is in a valid position. */
10242 check_cursor();
10243#ifdef FEAT_MBYTE
10244 /* Correct cursor for multi-byte character. */
10245 if (has_mbyte)
10246 mb_adjust_cursor();
10247#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010248
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010249 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010250 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
10253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010254 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 */
10256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010261 int noref = 0;
10262
10263 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010265 if (noref < 0 || noref > 1)
10266 EMSG(_(e_invarg));
10267 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010268 {
10269 current_copyID += COPYID_INC;
10270 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272}
10273
10274/*
10275 * "delete()" function
10276 */
10277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 typval_T *argvars;
10280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288/*
10289 * "did_filetype()" function
10290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010293 typval_T *argvars UNUSED;
10294 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
10296#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#endif
10299}
10300
10301/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010302 * "diff_filler()" function
10303 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010306 typval_T *argvars UNUSED;
10307 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010308{
10309#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010311#endif
10312}
10313
10314/*
10315 * "diff_hlID()" function
10316 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010319 typval_T *argvars UNUSED;
10320 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010321{
10322#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010324 static linenr_T prev_lnum = 0;
10325 static int changedtick = 0;
10326 static int fnum = 0;
10327 static int change_start = 0;
10328 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010329 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010330 int filler_lines;
10331 int col;
10332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 if (lnum < 0) /* ignore type error in {lnum} arg */
10334 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010335 if (lnum != prev_lnum
10336 || changedtick != curbuf->b_changedtick
10337 || fnum != curbuf->b_fnum)
10338 {
10339 /* New line, buffer, change: need to get the values. */
10340 filler_lines = diff_check(curwin, lnum);
10341 if (filler_lines < 0)
10342 {
10343 if (filler_lines == -1)
10344 {
10345 change_start = MAXCOL;
10346 change_end = -1;
10347 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10348 hlID = HLF_ADD; /* added line */
10349 else
10350 hlID = HLF_CHD; /* changed line */
10351 }
10352 else
10353 hlID = HLF_ADD; /* added line */
10354 }
10355 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010356 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010357 prev_lnum = lnum;
10358 changedtick = curbuf->b_changedtick;
10359 fnum = curbuf->b_fnum;
10360 }
10361
10362 if (hlID == HLF_CHD || hlID == HLF_TXD)
10363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010365 if (col >= change_start && col <= change_end)
10366 hlID = HLF_TXD; /* changed text */
10367 else
10368 hlID = HLF_CHD; /* changed line */
10369 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010370 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010371#endif
10372}
10373
10374/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010375 * "empty({expr})" function
10376 */
10377 static void
10378f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010381{
10382 int n;
10383
10384 switch (argvars[0].v_type)
10385 {
10386 case VAR_STRING:
10387 case VAR_FUNC:
10388 n = argvars[0].vval.v_string == NULL
10389 || *argvars[0].vval.v_string == NUL;
10390 break;
10391 case VAR_NUMBER:
10392 n = argvars[0].vval.v_number == 0;
10393 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010394#ifdef FEAT_FLOAT
10395 case VAR_FLOAT:
10396 n = argvars[0].vval.v_float == 0.0;
10397 break;
10398#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010399 case VAR_LIST:
10400 n = argvars[0].vval.v_list == NULL
10401 || argvars[0].vval.v_list->lv_first == NULL;
10402 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 case VAR_DICT:
10404 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010407 default:
10408 EMSG2(_(e_intern2), "f_empty()");
10409 n = 0;
10410 }
10411
10412 rettv->vval.v_number = n;
10413}
10414
10415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 * "escape({string}, {chars})" function
10417 */
10418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010420 typval_T *argvars;
10421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 char_u buf[NUMBUFLEN];
10424
Bram Moolenaar758711c2005-02-02 23:11:38 +000010425 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10426 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428}
10429
10430/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010431 * "eval()" function
10432 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010433 static void
10434f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010437{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010438 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 s = get_tv_string_chk(&argvars[0]);
10441 if (s != NULL)
10442 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010443
Bram Moolenaar615b9972015-01-14 17:15:05 +010010444 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10446 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010447 if (p != NULL && !aborting())
10448 EMSG2(_(e_invexpr2), p);
10449 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010453 else if (*s != NUL)
10454 EMSG(_(e_trailing));
10455}
10456
10457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 * "eventhandler()" function
10459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010462 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
10467
10468/*
10469 * "executable()" function
10470 */
10471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010476 char_u *name = get_tv_string(&argvars[0]);
10477
10478 /* Check in $PATH and also check directly if there is a directory name. */
10479 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10480 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010481}
10482
10483/*
10484 * "exepath()" function
10485 */
10486 static void
10487f_exepath(argvars, rettv)
10488 typval_T *argvars;
10489 typval_T *rettv;
10490{
10491 char_u *p = NULL;
10492
Bram Moolenaarb5971142015-03-21 17:32:19 +010010493 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010494 rettv->v_type = VAR_STRING;
10495 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496}
10497
10498/*
10499 * "exists()" function
10500 */
10501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010503 typval_T *argvars;
10504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 char_u *p;
10507 char_u *name;
10508 int n = FALSE;
10509 int len = 0;
10510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 if (*p == '$') /* environment variable */
10513 {
10514 /* first try "normal" environment variables (fast) */
10515 if (mch_getenv(p + 1) != NULL)
10516 n = TRUE;
10517 else
10518 {
10519 /* try expanding things like $VIM and ${HOME} */
10520 p = expand_env_save(p);
10521 if (p != NULL && *p != '$')
10522 n = TRUE;
10523 vim_free(p);
10524 }
10525 }
10526 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010529 if (*skipwhite(p) != NUL)
10530 n = FALSE; /* trailing garbage */
10531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 else if (*p == '*') /* internal or user defined function */
10533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010534 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 }
10536 else if (*p == ':')
10537 {
10538 n = cmd_exists(p + 1);
10539 }
10540 else if (*p == '#')
10541 {
10542#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010543 if (p[1] == '#')
10544 n = autocmd_supported(p + 2);
10545 else
10546 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547#endif
10548 }
10549 else /* internal variable */
10550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010551 char_u *tofree;
10552 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010554 /* get_name_len() takes care of expanding curly braces */
10555 name = p;
10556 len = get_name_len(&p, &tofree, TRUE, FALSE);
10557 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010559 if (tofree != NULL)
10560 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010561 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010562 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010564 /* handle d.key, l[idx], f(expr) */
10565 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10566 if (n)
10567 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010570 if (*p != NUL)
10571 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010573 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577}
10578
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010579#ifdef FEAT_FLOAT
10580/*
10581 * "exp()" function
10582 */
10583 static void
10584f_exp(argvars, rettv)
10585 typval_T *argvars;
10586 typval_T *rettv;
10587{
10588 float_T f;
10589
10590 rettv->v_type = VAR_FLOAT;
10591 if (get_float_arg(argvars, &f) == OK)
10592 rettv->vval.v_float = exp(f);
10593 else
10594 rettv->vval.v_float = 0.0;
10595}
10596#endif
10597
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598/*
10599 * "expand()" function
10600 */
10601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 typval_T *argvars;
10604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606 char_u *s;
10607 int len;
10608 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010609 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010612 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010615 if (argvars[1].v_type != VAR_UNKNOWN
10616 && argvars[2].v_type != VAR_UNKNOWN
10617 && get_tv_number_chk(&argvars[2], &error)
10618 && !error)
10619 {
10620 rettv->v_type = VAR_LIST;
10621 rettv->vval.v_list = NULL;
10622 }
10623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 if (*s == '%' || *s == '#' || *s == '<')
10626 {
10627 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010628 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010630 if (rettv->v_type == VAR_LIST)
10631 {
10632 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10633 list_append_string(rettv->vval.v_list, result, -1);
10634 else
10635 vim_free(result);
10636 }
10637 else
10638 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 }
10640 else
10641 {
10642 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010643 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 if (argvars[1].v_type != VAR_UNKNOWN
10645 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010646 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 if (!error)
10648 {
10649 ExpandInit(&xpc);
10650 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010651 if (p_wic)
10652 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010653 if (rettv->v_type == VAR_STRING)
10654 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10655 options, WILD_ALL);
10656 else if (rettv_list_alloc(rettv) != FAIL)
10657 {
10658 int i;
10659
10660 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10661 for (i = 0; i < xpc.xp_numfiles; i++)
10662 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10663 ExpandCleanup(&xpc);
10664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 }
10666 else
10667 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 }
10669}
10670
10671/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010672 * Go over all entries in "d2" and add them to "d1".
10673 * When "action" is "error" then a duplicate key is an error.
10674 * When "action" is "force" then a duplicate key is overwritten.
10675 * Otherwise duplicate keys are ignored ("action" is "keep").
10676 */
10677 void
10678dict_extend(d1, d2, action)
10679 dict_T *d1;
10680 dict_T *d2;
10681 char_u *action;
10682{
10683 dictitem_T *di1;
10684 hashitem_T *hi2;
10685 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010686 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010687
10688 todo = (int)d2->dv_hashtab.ht_used;
10689 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10690 {
10691 if (!HASHITEM_EMPTY(hi2))
10692 {
10693 --todo;
10694 di1 = dict_find(d1, hi2->hi_key, -1);
10695 if (d1->dv_scope != 0)
10696 {
10697 /* Disallow replacing a builtin function in l: and g:.
10698 * Check the key to be valid when adding to any
10699 * scope. */
10700 if (d1->dv_scope == VAR_DEF_SCOPE
10701 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10702 && var_check_func_name(hi2->hi_key,
10703 di1 == NULL))
10704 break;
10705 if (!valid_varname(hi2->hi_key))
10706 break;
10707 }
10708 if (di1 == NULL)
10709 {
10710 di1 = dictitem_copy(HI2DI(hi2));
10711 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10712 dictitem_free(di1);
10713 }
10714 else if (*action == 'e')
10715 {
10716 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10717 break;
10718 }
10719 else if (*action == 'f' && HI2DI(hi2) != di1)
10720 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010721 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10722 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010723 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010724 clear_tv(&di1->di_tv);
10725 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10726 }
10727 }
10728 }
10729}
10730
10731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010734 */
10735 static void
10736f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010739{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010740 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010741
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 list_T *l1, *l2;
10745 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010747 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010748
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749 l1 = argvars[0].vval.v_list;
10750 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010751 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010752 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010753 {
10754 if (argvars[2].v_type != VAR_UNKNOWN)
10755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 before = get_tv_number_chk(&argvars[2], &error);
10757 if (error)
10758 return; /* type error; errmsg already given */
10759
Bram Moolenaar758711c2005-02-02 23:11:38 +000010760 if (before == l1->lv_len)
10761 item = NULL;
10762 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010764 item = list_find(l1, before);
10765 if (item == NULL)
10766 {
10767 EMSGN(_(e_listidx), before);
10768 return;
10769 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010770 }
10771 }
10772 else
10773 item = NULL;
10774 list_extend(l1, l2, item);
10775
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 copy_tv(&argvars[0], rettv);
10777 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10780 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010781 dict_T *d1, *d2;
10782 char_u *action;
10783 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784
10785 d1 = argvars[0].vval.v_dict;
10786 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010787 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010788 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 {
10790 /* Check the third argument. */
10791 if (argvars[2].v_type != VAR_UNKNOWN)
10792 {
10793 static char *(av[]) = {"keep", "force", "error"};
10794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 action = get_tv_string_chk(&argvars[2]);
10796 if (action == NULL)
10797 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010798 for (i = 0; i < 3; ++i)
10799 if (STRCMP(action, av[i]) == 0)
10800 break;
10801 if (i == 3)
10802 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010803 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804 return;
10805 }
10806 }
10807 else
10808 action = (char_u *)"force";
10809
Bram Moolenaara9922d62013-05-30 13:01:18 +020010810 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 copy_tv(&argvars[0], rettv);
10813 }
10814 }
10815 else
10816 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010817}
10818
10819/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010820 * "feedkeys()" function
10821 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010822 static void
10823f_feedkeys(argvars, rettv)
10824 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010825 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010826{
10827 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010828 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010829 char_u *keys, *flags;
10830 char_u nbuf[NUMBUFLEN];
10831 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010832 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010833
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010834 /* This is not allowed in the sandbox. If the commands would still be
10835 * executed in the sandbox it would be OK, but it probably happens later,
10836 * when "sandbox" is no longer set. */
10837 if (check_secure())
10838 return;
10839
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010840 keys = get_tv_string(&argvars[0]);
10841 if (*keys != NUL)
10842 {
10843 if (argvars[1].v_type != VAR_UNKNOWN)
10844 {
10845 flags = get_tv_string_buf(&argvars[1], nbuf);
10846 for ( ; *flags != NUL; ++flags)
10847 {
10848 switch (*flags)
10849 {
10850 case 'n': remap = FALSE; break;
10851 case 'm': remap = TRUE; break;
10852 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010853 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010854 }
10855 }
10856 }
10857
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010858 /* Need to escape K_SPECIAL and CSI before putting the string in the
10859 * typeahead buffer. */
10860 keys_esc = vim_strsave_escape_csi(keys);
10861 if (keys_esc != NULL)
10862 {
10863 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010864 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010865 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010866 if (vgetc_busy)
10867 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010868 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010869 }
10870}
10871
10872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 * "filereadable()" function
10874 */
10875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010880 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 char_u *p;
10882 int n;
10883
Bram Moolenaarc236c162008-07-13 17:41:49 +000010884#ifndef O_NONBLOCK
10885# define O_NONBLOCK 0
10886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010888 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10889 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 {
10891 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010892 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 }
10894 else
10895 n = FALSE;
10896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898}
10899
10900/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010901 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 * rights to write into.
10903 */
10904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 typval_T *argvars;
10907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010909 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910}
10911
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010912static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010913
10914 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010915findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010916 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010917 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010918 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010919{
10920#ifdef FEAT_SEARCHPATH
10921 char_u *fname;
10922 char_u *fresult = NULL;
10923 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10924 char_u *p;
10925 char_u pathbuf[NUMBUFLEN];
10926 int count = 1;
10927 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010928 int error = FALSE;
10929#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010930
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010931 rettv->vval.v_string = NULL;
10932 rettv->v_type = VAR_STRING;
10933
10934#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10940 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010941 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 else
10943 {
10944 if (*p != NUL)
10945 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010948 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010950 }
10951
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010952 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10953 error = TRUE;
10954
10955 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 do
10958 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010959 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010960 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 fresult = find_file_in_path_option(first ? fname : NULL,
10962 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010963 0, first, path,
10964 find_what,
10965 curbuf->b_ffname,
10966 find_what == FINDFILE_DIR
10967 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010969
10970 if (fresult != NULL && rettv->v_type == VAR_LIST)
10971 list_append_string(rettv->vval.v_list, fresult, -1);
10972
10973 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010975
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010976 if (rettv->v_type == VAR_STRING)
10977 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010978#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010979}
10980
Bram Moolenaar33570922005-01-25 22:26:29 +000010981static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10982static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010983
10984/*
10985 * Implementation of map() and filter().
10986 */
10987 static void
10988filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 typval_T *argvars;
10990 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010991 int map;
10992{
10993 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 listitem_T *li, *nli;
10996 list_T *l = NULL;
10997 dictitem_T *di;
10998 hashtab_T *ht;
10999 hashitem_T *hi;
11000 dict_T *d = NULL;
11001 typval_T save_val;
11002 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011004 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011005 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011006 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011007 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011008 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011009 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011010
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011012 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011013 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011014 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 return;
11016 }
11017 else if (argvars[0].v_type == VAR_DICT)
11018 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011019 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011020 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011021 return;
11022 }
11023 else
11024 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011025 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011026 return;
11027 }
11028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 expr = get_tv_string_buf_chk(&argvars[1], buf);
11030 /* On type errors, the preceding call has already displayed an error
11031 * message. Avoid a misleading error message for an empty string that
11032 * was not passed as argument. */
11033 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011035 prepare_vimvar(VV_VAL, &save_val);
11036 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011037
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011038 /* We reset "did_emsg" to be able to detect whether an error
11039 * occurred during evaluation of the expression. */
11040 save_did_emsg = did_emsg;
11041 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011042
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011043 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 vimvars[VV_KEY].vv_type = VAR_STRING;
11047
11048 ht = &d->dv_hashtab;
11049 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011050 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 if (!HASHITEM_EMPTY(hi))
11054 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011055 int r;
11056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057 --todo;
11058 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011059 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011060 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11061 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011062 break;
11063 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011064 r = filter_map_one(&di->di_tv, expr, map, &rem);
11065 clear_tv(&vimvars[VV_KEY].vv_tv);
11066 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067 break;
11068 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011069 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011070 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11071 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011072 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 }
11076 }
11077 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 }
11079 else
11080 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011081 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 for (li = l->lv_first; li != NULL; li = nli)
11084 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011085 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011086 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011088 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011089 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011090 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011091 break;
11092 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011094 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011095 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011096 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011097
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011098 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011100
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011101 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103
11104 copy_tv(&argvars[0], rettv);
11105}
11106
11107 static int
11108filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 char_u *expr;
11111 int map;
11112 int *remp;
11113{
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011116 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 s = expr;
11120 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011121 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 if (*s != NUL) /* check for trailing chars after expr */
11123 {
11124 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011125 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011126 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 }
11128 if (map)
11129 {
11130 /* map(): replace the list item value */
11131 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011132 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 *tv = rettv;
11134 }
11135 else
11136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 int error = FALSE;
11138
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011141 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 /* On type error, nothing has been removed; return FAIL to stop the
11143 * loop. The error message was given by get_tv_number_chk(). */
11144 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011145 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011147 retval = OK;
11148theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011149 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011150 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011151}
11152
11153/*
11154 * "filter()" function
11155 */
11156 static void
11157f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 typval_T *argvars;
11159 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011160{
11161 filter_map(argvars, rettv, FALSE);
11162}
11163
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 * "finddir({fname}[, {path}[, {count}]])" function
11166 */
11167 static void
11168f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 typval_T *argvars;
11170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011172 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173}
11174
11175/*
11176 * "findfile({fname}[, {path}[, {count}]])" function
11177 */
11178 static void
11179f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011180 typval_T *argvars;
11181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011183 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011184}
11185
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011186#ifdef FEAT_FLOAT
11187/*
11188 * "float2nr({float})" function
11189 */
11190 static void
11191f_float2nr(argvars, rettv)
11192 typval_T *argvars;
11193 typval_T *rettv;
11194{
11195 float_T f;
11196
11197 if (get_float_arg(argvars, &f) == OK)
11198 {
11199 if (f < -0x7fffffff)
11200 rettv->vval.v_number = -0x7fffffff;
11201 else if (f > 0x7fffffff)
11202 rettv->vval.v_number = 0x7fffffff;
11203 else
11204 rettv->vval.v_number = (varnumber_T)f;
11205 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011206}
11207
11208/*
11209 * "floor({float})" function
11210 */
11211 static void
11212f_floor(argvars, rettv)
11213 typval_T *argvars;
11214 typval_T *rettv;
11215{
11216 float_T f;
11217
11218 rettv->v_type = VAR_FLOAT;
11219 if (get_float_arg(argvars, &f) == OK)
11220 rettv->vval.v_float = floor(f);
11221 else
11222 rettv->vval.v_float = 0.0;
11223}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011224
11225/*
11226 * "fmod()" function
11227 */
11228 static void
11229f_fmod(argvars, rettv)
11230 typval_T *argvars;
11231 typval_T *rettv;
11232{
11233 float_T fx, fy;
11234
11235 rettv->v_type = VAR_FLOAT;
11236 if (get_float_arg(argvars, &fx) == OK
11237 && get_float_arg(&argvars[1], &fy) == OK)
11238 rettv->vval.v_float = fmod(fx, fy);
11239 else
11240 rettv->vval.v_float = 0.0;
11241}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011242#endif
11243
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011245 * "fnameescape({string})" function
11246 */
11247 static void
11248f_fnameescape(argvars, rettv)
11249 typval_T *argvars;
11250 typval_T *rettv;
11251{
11252 rettv->vval.v_string = vim_strsave_fnameescape(
11253 get_tv_string(&argvars[0]), FALSE);
11254 rettv->v_type = VAR_STRING;
11255}
11256
11257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 * "fnamemodify({fname}, {mods})" function
11259 */
11260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011262 typval_T *argvars;
11263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
11265 char_u *fname;
11266 char_u *mods;
11267 int usedlen = 0;
11268 int len;
11269 char_u *fbuf = NULL;
11270 char_u buf[NUMBUFLEN];
11271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 fname = get_tv_string_chk(&argvars[0]);
11273 mods = get_tv_string_buf_chk(&argvars[1], buf);
11274 if (fname == NULL || mods == NULL)
11275 fname = NULL;
11276 else
11277 {
11278 len = (int)STRLEN(fname);
11279 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 vim_free(fbuf);
11288}
11289
Bram Moolenaar33570922005-01-25 22:26:29 +000011290static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
11292/*
11293 * "foldclosed()" function
11294 */
11295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011299 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300{
11301#ifdef FEAT_FOLDING
11302 linenr_T lnum;
11303 linenr_T first, last;
11304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11307 {
11308 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11309 {
11310 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 return;
11315 }
11316 }
11317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
11321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011322 * "foldclosed()" function
11323 */
11324 static void
11325f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011326 typval_T *argvars;
11327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011328{
11329 foldclosed_both(argvars, rettv, FALSE);
11330}
11331
11332/*
11333 * "foldclosedend()" function
11334 */
11335 static void
11336f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011337 typval_T *argvars;
11338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339{
11340 foldclosed_both(argvars, rettv, TRUE);
11341}
11342
11343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 * "foldlevel()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011348 typval_T *argvars UNUSED;
11349 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351#ifdef FEAT_FOLDING
11352 linenr_T lnum;
11353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358}
11359
11360/*
11361 * "foldtext()" function
11362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011365 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368#ifdef FEAT_FOLDING
11369 linenr_T lnum;
11370 char_u *s;
11371 char_u *r;
11372 int len;
11373 char *txt;
11374#endif
11375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11380 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11381 <= curbuf->b_ml.ml_line_count
11382 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 {
11384 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011385 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11386 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 {
11388 if (!linewhite(lnum))
11389 break;
11390 ++lnum;
11391 }
11392
11393 /* Find interesting text in this line. */
11394 s = skipwhite(ml_get(lnum));
11395 /* skip C comment-start */
11396 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011399 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011400 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011401 {
11402 s = skipwhite(ml_get(lnum + 1));
11403 if (*s == '*')
11404 s = skipwhite(s + 1);
11405 }
11406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 txt = _("+-%s%3ld lines: ");
11408 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011409 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 + 20 /* for %3ld */
11411 + STRLEN(s))); /* concatenated */
11412 if (r != NULL)
11413 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011414 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11415 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11416 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 len = (int)STRLEN(r);
11418 STRCAT(r, s);
11419 /* remove 'foldmarker' and 'commentstring' */
11420 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 }
11423 }
11424#endif
11425}
11426
11427/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011428 * "foldtextresult(lnum)" function
11429 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431f_foldtextresult(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 Moolenaar7b0294c2004-10-11 10:16:09 +000011434{
11435#ifdef FEAT_FOLDING
11436 linenr_T lnum;
11437 char_u *text;
11438 char_u buf[51];
11439 foldinfo_T foldinfo;
11440 int fold_count;
11441#endif
11442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011445#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 /* treat illegal types and illegal string values for {lnum} the same */
11448 if (lnum < 0)
11449 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011450 fold_count = foldedCount(curwin, lnum, &foldinfo);
11451 if (fold_count > 0)
11452 {
11453 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11454 &foldinfo, buf);
11455 if (text == buf)
11456 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011458 }
11459#endif
11460}
11461
11462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 * "foreground()" function
11464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011467 typval_T *argvars UNUSED;
11468 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470#ifdef FEAT_GUI
11471 if (gui.in_use)
11472 gui_mch_set_foreground();
11473#else
11474# ifdef WIN32
11475 win32_set_foreground();
11476# endif
11477#endif
11478}
11479
11480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481 * "function()" function
11482 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *argvars;
11486 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011487{
11488 char_u *s;
11489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011491 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011493 /* Don't check an autoload name for existence here. */
11494 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011495 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011496 else
11497 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011498 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011499 {
11500 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011501 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011502
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011503 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11504 * also be called from another script. Using trans_function_name()
11505 * would also work, but some plugins depend on the name being
11506 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011507 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011508 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011509 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011510 if (rettv->vval.v_string != NULL)
11511 {
11512 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011513 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011514 }
11515 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011516 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011517 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011519 }
11520}
11521
11522/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011523 * "garbagecollect()" function
11524 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011525 static void
11526f_garbagecollect(argvars, rettv)
11527 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011528 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011529{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011530 /* This is postponed until we are back at the toplevel, because we may be
11531 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11532 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011533
11534 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11535 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011536}
11537
11538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 * "get()" function
11540 */
11541 static void
11542f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011545{
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 listitem_T *li;
11547 list_T *l;
11548 dictitem_T *di;
11549 dict_T *d;
11550 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551
Bram Moolenaare9a41262005-01-15 22:18:47 +000011552 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011553 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011554 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 int error = FALSE;
11557
11558 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11559 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011560 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011561 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 else if (argvars[0].v_type == VAR_DICT)
11564 {
11565 if ((d = argvars[0].vval.v_dict) != NULL)
11566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011567 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011568 if (di != NULL)
11569 tv = &di->di_tv;
11570 }
11571 }
11572 else
11573 EMSG2(_(e_listdictarg), "get()");
11574
11575 if (tv == NULL)
11576 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011578 copy_tv(&argvars[2], rettv);
11579 }
11580 else
11581 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582}
11583
Bram Moolenaar342337a2005-07-21 21:11:17 +000011584static 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 +000011585
11586/*
11587 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011588 * Return a range (from start to end) of lines in rettv from the specified
11589 * buffer.
11590 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011591 */
11592 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011593get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011594 buf_T *buf;
11595 linenr_T start;
11596 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011597 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011598 typval_T *rettv;
11599{
11600 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011601
Bram Moolenaar959a1432013-12-14 12:17:38 +010011602 rettv->v_type = VAR_STRING;
11603 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011604 if (retlist && rettv_list_alloc(rettv) == FAIL)
11605 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606
11607 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11608 return;
11609
11610 if (!retlist)
11611 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011612 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11613 p = ml_get_buf(buf, start, FALSE);
11614 else
11615 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011616 rettv->vval.v_string = vim_strsave(p);
11617 }
11618 else
11619 {
11620 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011621 return;
11622
11623 if (start < 1)
11624 start = 1;
11625 if (end > buf->b_ml.ml_line_count)
11626 end = buf->b_ml.ml_line_count;
11627 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011628 if (list_append_string(rettv->vval.v_list,
11629 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011630 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011631 }
11632}
11633
11634/*
11635 * "getbufline()" function
11636 */
11637 static void
11638f_getbufline(argvars, rettv)
11639 typval_T *argvars;
11640 typval_T *rettv;
11641{
11642 linenr_T lnum;
11643 linenr_T end;
11644 buf_T *buf;
11645
11646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11647 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011648 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011649 --emsg_off;
11650
Bram Moolenaar661b1822005-07-28 22:36:45 +000011651 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011652 if (argvars[2].v_type == VAR_UNKNOWN)
11653 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011654 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011655 end = get_tv_lnum_buf(&argvars[2], buf);
11656
Bram Moolenaar342337a2005-07-21 21:11:17 +000011657 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011658}
11659
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660/*
11661 * "getbufvar()" function
11662 */
11663 static void
11664f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667{
11668 buf_T *buf;
11669 buf_T *save_curbuf;
11670 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011672 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11675 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011677 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011679 rettv->v_type = VAR_STRING;
11680 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681
11682 if (buf != NULL && varname != NULL)
11683 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011684 /* set curbuf to be our buf, temporarily */
11685 save_curbuf = curbuf;
11686 curbuf = buf;
11687
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011689 {
11690 if (get_option_tv(&varname, rettv, TRUE) == OK)
11691 done = TRUE;
11692 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011693 else if (STRCMP(varname, "changedtick") == 0)
11694 {
11695 rettv->v_type = VAR_NUMBER;
11696 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011697 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699 else
11700 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011701 /* Look up the variable. */
11702 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11703 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11704 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011705 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011706 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011708 done = TRUE;
11709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011711
11712 /* restore previous notion of curbuf */
11713 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714 }
11715
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011716 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11717 /* use the default value */
11718 copy_tv(&argvars[2], rettv);
11719
Bram Moolenaar0d660222005-01-07 21:51:51 +000011720 --emsg_off;
11721}
11722
11723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 * "getchar()" function
11725 */
11726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *argvars;
11729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
11731 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011732 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011734 /* Position the cursor. Needed after a message that ends in a space. */
11735 windgoto(msg_row, msg_col);
11736
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 ++no_mapping;
11738 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011739 for (;;)
11740 {
11741 if (argvars[0].v_type == VAR_UNKNOWN)
11742 /* getchar(): blocking wait. */
11743 n = safe_vgetc();
11744 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11745 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011746 n = vpeekc_any();
11747 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011748 /* illegal argument or getchar(0) and no char avail: return zero */
11749 n = 0;
11750 else
11751 /* getchar(0) and char avail: return char */
11752 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011753
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011754 if (n == K_IGNORE)
11755 continue;
11756 break;
11757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 --no_mapping;
11759 --allow_keys;
11760
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11762 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11763 vimvars[VV_MOUSE_COL].vv_nr = 0;
11764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 if (IS_SPECIAL(n) || mod_mask != 0)
11767 {
11768 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11769 int i = 0;
11770
11771 /* Turn a special key into three bytes, plus modifier. */
11772 if (mod_mask != 0)
11773 {
11774 temp[i++] = K_SPECIAL;
11775 temp[i++] = KS_MODIFIER;
11776 temp[i++] = mod_mask;
11777 }
11778 if (IS_SPECIAL(n))
11779 {
11780 temp[i++] = K_SPECIAL;
11781 temp[i++] = K_SECOND(n);
11782 temp[i++] = K_THIRD(n);
11783 }
11784#ifdef FEAT_MBYTE
11785 else if (has_mbyte)
11786 i += (*mb_char2bytes)(n, temp + i);
11787#endif
11788 else
11789 temp[i++] = n;
11790 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011791 rettv->v_type = VAR_STRING;
11792 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011793
11794#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011795 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011796 {
11797 int row = mouse_row;
11798 int col = mouse_col;
11799 win_T *win;
11800 linenr_T lnum;
11801# ifdef FEAT_WINDOWS
11802 win_T *wp;
11803# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011804 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011805
11806 if (row >= 0 && col >= 0)
11807 {
11808 /* Find the window at the mouse coordinates and compute the
11809 * text position. */
11810 win = mouse_find_win(&row, &col);
11811 (void)mouse_comp_pos(win, &row, &col, &lnum);
11812# ifdef FEAT_WINDOWS
11813 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011814 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011815# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011816 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011817 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11818 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11819 }
11820 }
11821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 }
11823}
11824
11825/*
11826 * "getcharmod()" function
11827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834}
11835
11836/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011837 * "getcharsearch()" function
11838 */
11839 static void
11840f_getcharsearch(argvars, rettv)
11841 typval_T *argvars UNUSED;
11842 typval_T *rettv;
11843{
11844 if (rettv_dict_alloc(rettv) != FAIL)
11845 {
11846 dict_T *dict = rettv->vval.v_dict;
11847
11848 dict_add_nr_str(dict, "char", 0L, last_csearch());
11849 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11850 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11851 }
11852}
11853
11854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 * "getcmdline()" function
11856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011859 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->v_type = VAR_STRING;
11863 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864}
11865
11866/*
11867 * "getcmdpos()" function
11868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011871 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875}
11876
11877/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011878 * "getcmdtype()" function
11879 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011880 static void
11881f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011882 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011883 typval_T *rettv;
11884{
11885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = alloc(2);
11887 if (rettv->vval.v_string != NULL)
11888 {
11889 rettv->vval.v_string[0] = get_cmdline_type();
11890 rettv->vval.v_string[1] = NUL;
11891 }
11892}
11893
11894/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011895 * "getcmdwintype()" function
11896 */
11897 static void
11898f_getcmdwintype(argvars, rettv)
11899 typval_T *argvars UNUSED;
11900 typval_T *rettv;
11901{
11902 rettv->v_type = VAR_STRING;
11903 rettv->vval.v_string = NULL;
11904#ifdef FEAT_CMDWIN
11905 rettv->vval.v_string = alloc(2);
11906 if (rettv->vval.v_string != NULL)
11907 {
11908 rettv->vval.v_string[0] = cmdwin_type;
11909 rettv->vval.v_string[1] = NUL;
11910 }
11911#endif
11912}
11913
11914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 * "getcwd()" function
11916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011922 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011925 rettv->vval.v_string = NULL;
11926 cwd = alloc(MAXPATHL);
11927 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011929 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11930 {
11931 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011933 if (rettv->vval.v_string != NULL)
11934 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011936 }
11937 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 }
11939}
11940
11941/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011942 * "getfontname()" function
11943 */
11944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011946 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949 rettv->v_type = VAR_STRING;
11950 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011951#ifdef FEAT_GUI
11952 if (gui.in_use)
11953 {
11954 GuiFont font;
11955 char_u *name = NULL;
11956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011958 {
11959 /* Get the "Normal" font. Either the name saved by
11960 * hl_set_font_name() or from the font ID. */
11961 font = gui.norm_font;
11962 name = hl_get_font_name();
11963 }
11964 else
11965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011967 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11968 return;
11969 font = gui_mch_get_font(name, FALSE);
11970 if (font == NOFONT)
11971 return; /* Invalid font name, return empty string. */
11972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011974 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011975 gui_mch_free_font(font);
11976 }
11977#endif
11978}
11979
11980/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011981 * "getfperm({fname})" function
11982 */
11983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011987{
11988 char_u *fname;
11989 struct stat st;
11990 char_u *perm = NULL;
11991 char_u flags[] = "rwx";
11992 int i;
11993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011997 if (mch_stat((char *)fname, &st) >= 0)
11998 {
11999 perm = vim_strsave((char_u *)"---------");
12000 if (perm != NULL)
12001 {
12002 for (i = 0; i < 9; i++)
12003 {
12004 if (st.st_mode & (1 << (8 - i)))
12005 perm[i] = flags[i % 3];
12006 }
12007 }
12008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010}
12011
12012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 * "getfsize({fname})" function
12014 */
12015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
12020 char_u *fname;
12021 struct stat st;
12022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026
12027 if (mch_stat((char *)fname, &st) >= 0)
12028 {
12029 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012034
12035 /* non-perfect check for overflow */
12036 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12037 rettv->vval.v_number = -2;
12038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 }
12040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042}
12043
12044/*
12045 * "getftime({fname})" function
12046 */
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 char_u *fname;
12053 struct stat st;
12054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
12057 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061}
12062
12063/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012064 * "getftype({fname})" function
12065 */
12066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012067f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012068 typval_T *argvars;
12069 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012070{
12071 char_u *fname;
12072 struct stat st;
12073 char_u *type = NULL;
12074 char *t;
12075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012079 if (mch_lstat((char *)fname, &st) >= 0)
12080 {
12081#ifdef S_ISREG
12082 if (S_ISREG(st.st_mode))
12083 t = "file";
12084 else if (S_ISDIR(st.st_mode))
12085 t = "dir";
12086# ifdef S_ISLNK
12087 else if (S_ISLNK(st.st_mode))
12088 t = "link";
12089# endif
12090# ifdef S_ISBLK
12091 else if (S_ISBLK(st.st_mode))
12092 t = "bdev";
12093# endif
12094# ifdef S_ISCHR
12095 else if (S_ISCHR(st.st_mode))
12096 t = "cdev";
12097# endif
12098# ifdef S_ISFIFO
12099 else if (S_ISFIFO(st.st_mode))
12100 t = "fifo";
12101# endif
12102# ifdef S_ISSOCK
12103 else if (S_ISSOCK(st.st_mode))
12104 t = "fifo";
12105# endif
12106 else
12107 t = "other";
12108#else
12109# ifdef S_IFMT
12110 switch (st.st_mode & S_IFMT)
12111 {
12112 case S_IFREG: t = "file"; break;
12113 case S_IFDIR: t = "dir"; break;
12114# ifdef S_IFLNK
12115 case S_IFLNK: t = "link"; break;
12116# endif
12117# ifdef S_IFBLK
12118 case S_IFBLK: t = "bdev"; break;
12119# endif
12120# ifdef S_IFCHR
12121 case S_IFCHR: t = "cdev"; break;
12122# endif
12123# ifdef S_IFIFO
12124 case S_IFIFO: t = "fifo"; break;
12125# endif
12126# ifdef S_IFSOCK
12127 case S_IFSOCK: t = "socket"; break;
12128# endif
12129 default: t = "other";
12130 }
12131# else
12132 if (mch_isdir(fname))
12133 t = "dir";
12134 else
12135 t = "file";
12136# endif
12137#endif
12138 type = vim_strsave((char_u *)t);
12139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012141}
12142
12143/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012144 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145 */
12146 static void
12147f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 typval_T *argvars;
12149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012150{
12151 linenr_T lnum;
12152 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012153 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154
12155 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012156 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012157 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012158 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012159 retlist = FALSE;
12160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012163 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012164 retlist = TRUE;
12165 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012166
Bram Moolenaar342337a2005-07-21 21:11:17 +000012167 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168}
12169
12170/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012171 * "getmatches()" function
12172 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012173 static void
12174f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012175 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012176 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012177{
12178#ifdef FEAT_SEARCH_EXTRA
12179 dict_T *dict;
12180 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012181 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012182
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012183 if (rettv_list_alloc(rettv) == OK)
12184 {
12185 while (cur != NULL)
12186 {
12187 dict = dict_alloc();
12188 if (dict == NULL)
12189 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012190 if (cur->match.regprog == NULL)
12191 {
12192 /* match added with matchaddpos() */
12193 for (i = 0; i < MAXPOSMATCH; ++i)
12194 {
12195 llpos_T *llpos;
12196 char buf[6];
12197 list_T *l;
12198
12199 llpos = &cur->pos.pos[i];
12200 if (llpos->lnum == 0)
12201 break;
12202 l = list_alloc();
12203 if (l == NULL)
12204 break;
12205 list_append_number(l, (varnumber_T)llpos->lnum);
12206 if (llpos->col > 0)
12207 {
12208 list_append_number(l, (varnumber_T)llpos->col);
12209 list_append_number(l, (varnumber_T)llpos->len);
12210 }
12211 sprintf(buf, "pos%d", i + 1);
12212 dict_add_list(dict, buf, l);
12213 }
12214 }
12215 else
12216 {
12217 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12218 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012219 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012220 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12221 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012222# ifdef FEAT_CONCEAL
12223 if (cur->conceal_char)
12224 {
12225 char_u buf[MB_MAXBYTES + 1];
12226
12227 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12228 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12229 }
12230# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012231 list_append_dict(rettv->vval.v_list, dict);
12232 cur = cur->next;
12233 }
12234 }
12235#endif
12236}
12237
12238/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012239 * "getpid()" function
12240 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012241 static void
12242f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012243 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012244 typval_T *rettv;
12245{
12246 rettv->vval.v_number = mch_get_pid();
12247}
12248
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012249static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12250
12251/*
12252 * "getcurpos()" function
12253 */
12254 static void
12255f_getcurpos(argvars, rettv)
12256 typval_T *argvars;
12257 typval_T *rettv;
12258{
12259 getpos_both(argvars, rettv, TRUE);
12260}
12261
Bram Moolenaar18081e32008-02-20 19:11:07 +000012262/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012263 * "getpos(string)" function
12264 */
12265 static void
12266f_getpos(argvars, rettv)
12267 typval_T *argvars;
12268 typval_T *rettv;
12269{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012270 getpos_both(argvars, rettv, FALSE);
12271}
12272
12273 static void
12274getpos_both(argvars, rettv, getcurpos)
12275 typval_T *argvars;
12276 typval_T *rettv;
12277 int getcurpos;
12278{
Bram Moolenaara5525202006-03-02 22:52:09 +000012279 pos_T *fp;
12280 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012281 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012282
12283 if (rettv_list_alloc(rettv) == OK)
12284 {
12285 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012286 if (getcurpos)
12287 fp = &curwin->w_cursor;
12288 else
12289 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012290 if (fnum != -1)
12291 list_append_number(l, (varnumber_T)fnum);
12292 else
12293 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012294 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12295 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012296 list_append_number(l, (fp != NULL)
12297 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012298 : (varnumber_T)0);
12299 list_append_number(l,
12300#ifdef FEAT_VIRTUALEDIT
12301 (fp != NULL) ? (varnumber_T)fp->coladd :
12302#endif
12303 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012304 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012305 list_append_number(l, curwin->w_curswant == MAXCOL ?
12306 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012307 }
12308 else
12309 rettv->vval.v_number = FALSE;
12310}
12311
12312/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012313 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012314 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012315 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012316f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012317 typval_T *argvars UNUSED;
12318 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012319{
12320#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012321 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012322#endif
12323
Bram Moolenaar2641f772005-03-25 21:58:17 +000012324#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012325 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012326 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012327 wp = NULL;
12328 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12329 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012330 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012331 if (wp == NULL)
12332 return;
12333 }
12334
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012335 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012336 }
12337#endif
12338}
12339
12340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 * "getreg()" function
12342 */
12343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012345 typval_T *argvars;
12346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347{
12348 char_u *strregname;
12349 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012350 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012351 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012354 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356 strregname = get_tv_string_chk(&argvars[0]);
12357 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012358 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012361 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12362 return_list = get_tv_number_chk(&argvars[2], &error);
12363 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012366 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012367
12368 if (error)
12369 return;
12370
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 regname = (strregname == NULL ? '"' : *strregname);
12372 if (regname == 0)
12373 regname = '"';
12374
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012375 if (return_list)
12376 {
12377 rettv->v_type = VAR_LIST;
12378 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12379 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012380 if (rettv->vval.v_list != NULL)
12381 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012382 }
12383 else
12384 {
12385 rettv->v_type = VAR_STRING;
12386 rettv->vval.v_string = get_reg_contents(regname,
12387 arg2 ? GREG_EXPR_SRC : 0);
12388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389}
12390
12391/*
12392 * "getregtype()" function
12393 */
12394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012395f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012396 typval_T *argvars;
12397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398{
12399 char_u *strregname;
12400 int regname;
12401 char_u buf[NUMBUFLEN + 2];
12402 long reglen = 0;
12403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 {
12406 strregname = get_tv_string_chk(&argvars[0]);
12407 if (strregname == NULL) /* type error; errmsg already given */
12408 {
12409 rettv->v_type = VAR_STRING;
12410 rettv->vval.v_string = NULL;
12411 return;
12412 }
12413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 else
12415 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417
12418 regname = (strregname == NULL ? '"' : *strregname);
12419 if (regname == 0)
12420 regname = '"';
12421
12422 buf[0] = NUL;
12423 buf[1] = NUL;
12424 switch (get_reg_type(regname, &reglen))
12425 {
12426 case MLINE: buf[0] = 'V'; break;
12427 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 case MBLOCK:
12429 buf[0] = Ctrl_V;
12430 sprintf((char *)buf + 1, "%ld", reglen + 1);
12431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 rettv->v_type = VAR_STRING;
12434 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435}
12436
12437/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012438 * "gettabvar()" function
12439 */
12440 static void
12441f_gettabvar(argvars, rettv)
12442 typval_T *argvars;
12443 typval_T *rettv;
12444{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012445 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012446 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012447 dictitem_T *v;
12448 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012449 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012450
12451 rettv->v_type = VAR_STRING;
12452 rettv->vval.v_string = NULL;
12453
12454 varname = get_tv_string_chk(&argvars[1]);
12455 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12456 if (tp != NULL && varname != NULL)
12457 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012458 /* Set tp to be our tabpage, temporarily. Also set the window to the
12459 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012460 if (switch_win(&oldcurwin, &oldtabpage,
12461 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012462 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012463 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012464 /* look up the variable */
12465 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12466 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12467 if (v != NULL)
12468 {
12469 copy_tv(&v->di_tv, rettv);
12470 done = TRUE;
12471 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012472 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012473
12474 /* restore previous notion of curwin */
12475 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012476 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012477
12478 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012479 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012480}
12481
12482/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012483 * "gettabwinvar()" function
12484 */
12485 static void
12486f_gettabwinvar(argvars, rettv)
12487 typval_T *argvars;
12488 typval_T *rettv;
12489{
12490 getwinvar(argvars, rettv, 1);
12491}
12492
12493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 * "getwinposx()" function
12495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012498 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502#ifdef FEAT_GUI
12503 if (gui.in_use)
12504 {
12505 int x, y;
12506
12507 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 }
12510#endif
12511}
12512
12513/*
12514 * "getwinposy()" function
12515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012518 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522#ifdef FEAT_GUI
12523 if (gui.in_use)
12524 {
12525 int x, y;
12526
12527 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 }
12530#endif
12531}
12532
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012533/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012534 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012535 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012536 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012537find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012538 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012539 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012540{
12541#ifdef FEAT_WINDOWS
12542 win_T *wp;
12543#endif
12544 int nr;
12545
12546 nr = get_tv_number_chk(vp, NULL);
12547
12548#ifdef FEAT_WINDOWS
12549 if (nr < 0)
12550 return NULL;
12551 if (nr == 0)
12552 return curwin;
12553
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012554 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12555 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012556 if (--nr <= 0)
12557 break;
12558 return wp;
12559#else
12560 if (nr == 0 || nr == 1)
12561 return curwin;
12562 return NULL;
12563#endif
12564}
12565
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566/*
12567 * "getwinvar()" function
12568 */
12569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *argvars;
12572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012574 getwinvar(argvars, rettv, 0);
12575}
12576
12577/*
12578 * getwinvar() and gettabwinvar()
12579 */
12580 static void
12581getwinvar(argvars, rettv, off)
12582 typval_T *argvars;
12583 typval_T *rettv;
12584 int off; /* 1 for gettabwinvar() */
12585{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012586 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012588 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012589 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012590 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012591#ifdef FEAT_WINDOWS
12592 win_T *oldcurwin;
12593 tabpage_T *oldtabpage;
12594 int need_switch_win;
12595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012597#ifdef FEAT_WINDOWS
12598 if (off == 1)
12599 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12600 else
12601 tp = curtab;
12602#endif
12603 win = find_win_by_nr(&argvars[off], tp);
12604 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012607 rettv->v_type = VAR_STRING;
12608 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609
12610 if (win != NULL && varname != NULL)
12611 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012612#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012613 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012614 * otherwise the window is not valid. Only do this when needed,
12615 * autocommands get blocked. */
12616 need_switch_win = !(tp == curtab && win == curwin);
12617 if (!need_switch_win
12618 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12619#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012621 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012622 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012623 if (get_option_tv(&varname, rettv, 1) == OK)
12624 done = TRUE;
12625 }
12626 else
12627 {
12628 /* Look up the variable. */
12629 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12630 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12631 varname, FALSE);
12632 if (v != NULL)
12633 {
12634 copy_tv(&v->di_tv, rettv);
12635 done = TRUE;
12636 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012639
Bram Moolenaarba117c22015-09-29 16:53:22 +020012640#ifdef FEAT_WINDOWS
12641 if (need_switch_win)
12642 /* restore previous notion of curwin */
12643 restore_win(oldcurwin, oldtabpage, TRUE);
12644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 }
12646
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012647 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12648 /* use the default return value */
12649 copy_tv(&argvars[off + 2], rettv);
12650
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 --emsg_off;
12652}
12653
12654/*
12655 * "glob()" function
12656 */
12657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012659 typval_T *argvars;
12660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012662 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012664 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012666 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012667 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012669 if (argvars[1].v_type != VAR_UNKNOWN)
12670 {
12671 if (get_tv_number_chk(&argvars[1], &error))
12672 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012673 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012674 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012675 if (get_tv_number_chk(&argvars[2], &error))
12676 {
12677 rettv->v_type = VAR_LIST;
12678 rettv->vval.v_list = NULL;
12679 }
12680 if (argvars[3].v_type != VAR_UNKNOWN
12681 && get_tv_number_chk(&argvars[3], &error))
12682 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012683 }
12684 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012685 if (!error)
12686 {
12687 ExpandInit(&xpc);
12688 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012689 if (p_wic)
12690 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012691 if (rettv->v_type == VAR_STRING)
12692 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012693 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012694 else if (rettv_list_alloc(rettv) != FAIL)
12695 {
12696 int i;
12697
12698 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12699 NULL, options, WILD_ALL_KEEP);
12700 for (i = 0; i < xpc.xp_numfiles; i++)
12701 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12702
12703 ExpandCleanup(&xpc);
12704 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012705 }
12706 else
12707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708}
12709
12710/*
12711 * "globpath()" function
12712 */
12713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012718 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012721 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012722 garray_T ga;
12723 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012725 /* When the optional second argument is non-zero, don't remove matches
12726 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012728 if (argvars[2].v_type != VAR_UNKNOWN)
12729 {
12730 if (get_tv_number_chk(&argvars[2], &error))
12731 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012732 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012733 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012734 if (get_tv_number_chk(&argvars[3], &error))
12735 {
12736 rettv->v_type = VAR_LIST;
12737 rettv->vval.v_list = NULL;
12738 }
12739 if (argvars[4].v_type != VAR_UNKNOWN
12740 && get_tv_number_chk(&argvars[4], &error))
12741 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012742 }
12743 }
12744 if (file != NULL && !error)
12745 {
12746 ga_init2(&ga, (int)sizeof(char_u *), 10);
12747 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12748 if (rettv->v_type == VAR_STRING)
12749 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12750 else if (rettv_list_alloc(rettv) != FAIL)
12751 for (i = 0; i < ga.ga_len; ++i)
12752 list_append_string(rettv->vval.v_list,
12753 ((char_u **)(ga.ga_data))[i], -1);
12754 ga_clear_strings(&ga);
12755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758}
12759
12760/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012761 * "glob2regpat()" function
12762 */
12763 static void
12764f_glob2regpat(argvars, rettv)
12765 typval_T *argvars;
12766 typval_T *rettv;
12767{
12768 char_u *pat = get_tv_string_chk(&argvars[0]);
12769
12770 rettv->v_type = VAR_STRING;
12771 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12772}
12773
12774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 * "has()" function
12776 */
12777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *argvars;
12780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781{
12782 int i;
12783 char_u *name;
12784 int n = FALSE;
12785 static char *(has_list[]) =
12786 {
12787#ifdef AMIGA
12788 "amiga",
12789# ifdef FEAT_ARP
12790 "arp",
12791# endif
12792#endif
12793#ifdef __BEOS__
12794 "beos",
12795#endif
12796#ifdef MSDOS
12797# ifdef DJGPP
12798 "dos32",
12799# else
12800 "dos16",
12801# endif
12802#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012803#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 "mac",
12805#endif
12806#if defined(MACOS_X_UNIX)
12807 "macunix",
12808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809#ifdef __QNX__
12810 "qnx",
12811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812#ifdef UNIX
12813 "unix",
12814#endif
12815#ifdef VMS
12816 "vms",
12817#endif
12818#ifdef WIN16
12819 "win16",
12820#endif
12821#ifdef WIN32
12822 "win32",
12823#endif
12824#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12825 "win32unix",
12826#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012827#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 "win64",
12829#endif
12830#ifdef EBCDIC
12831 "ebcdic",
12832#endif
12833#ifndef CASE_INSENSITIVE_FILENAME
12834 "fname_case",
12835#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012836#ifdef HAVE_ACL
12837 "acl",
12838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839#ifdef FEAT_ARABIC
12840 "arabic",
12841#endif
12842#ifdef FEAT_AUTOCMD
12843 "autocmd",
12844#endif
12845#ifdef FEAT_BEVAL
12846 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012847# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12848 "balloon_multiline",
12849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850#endif
12851#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12852 "builtin_terms",
12853# ifdef ALL_BUILTIN_TCAPS
12854 "all_builtin_terms",
12855# endif
12856#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012857#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12858 || defined(FEAT_GUI_W32) \
12859 || defined(FEAT_GUI_MOTIF))
12860 "browsefilter",
12861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862#ifdef FEAT_BYTEOFF
12863 "byte_offset",
12864#endif
12865#ifdef FEAT_CINDENT
12866 "cindent",
12867#endif
12868#ifdef FEAT_CLIENTSERVER
12869 "clientserver",
12870#endif
12871#ifdef FEAT_CLIPBOARD
12872 "clipboard",
12873#endif
12874#ifdef FEAT_CMDL_COMPL
12875 "cmdline_compl",
12876#endif
12877#ifdef FEAT_CMDHIST
12878 "cmdline_hist",
12879#endif
12880#ifdef FEAT_COMMENTS
12881 "comments",
12882#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012883#ifdef FEAT_CONCEAL
12884 "conceal",
12885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886#ifdef FEAT_CRYPT
12887 "cryptv",
12888#endif
12889#ifdef FEAT_CSCOPE
12890 "cscope",
12891#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012892#ifdef FEAT_CURSORBIND
12893 "cursorbind",
12894#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012895#ifdef CURSOR_SHAPE
12896 "cursorshape",
12897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898#ifdef DEBUG
12899 "debug",
12900#endif
12901#ifdef FEAT_CON_DIALOG
12902 "dialog_con",
12903#endif
12904#ifdef FEAT_GUI_DIALOG
12905 "dialog_gui",
12906#endif
12907#ifdef FEAT_DIFF
12908 "diff",
12909#endif
12910#ifdef FEAT_DIGRAPHS
12911 "digraphs",
12912#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012913#ifdef FEAT_DIRECTX
12914 "directx",
12915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916#ifdef FEAT_DND
12917 "dnd",
12918#endif
12919#ifdef FEAT_EMACS_TAGS
12920 "emacs_tags",
12921#endif
12922 "eval", /* always present, of course! */
12923#ifdef FEAT_EX_EXTRA
12924 "ex_extra",
12925#endif
12926#ifdef FEAT_SEARCH_EXTRA
12927 "extra_search",
12928#endif
12929#ifdef FEAT_FKMAP
12930 "farsi",
12931#endif
12932#ifdef FEAT_SEARCHPATH
12933 "file_in_path",
12934#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012935#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012936 "filterpipe",
12937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938#ifdef FEAT_FIND_ID
12939 "find_in_path",
12940#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012941#ifdef FEAT_FLOAT
12942 "float",
12943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944#ifdef FEAT_FOLDING
12945 "folding",
12946#endif
12947#ifdef FEAT_FOOTER
12948 "footer",
12949#endif
12950#if !defined(USE_SYSTEM) && defined(UNIX)
12951 "fork",
12952#endif
12953#ifdef FEAT_GETTEXT
12954 "gettext",
12955#endif
12956#ifdef FEAT_GUI
12957 "gui",
12958#endif
12959#ifdef FEAT_GUI_ATHENA
12960# ifdef FEAT_GUI_NEXTAW
12961 "gui_neXtaw",
12962# else
12963 "gui_athena",
12964# endif
12965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966#ifdef FEAT_GUI_GTK
12967 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012970#ifdef FEAT_GUI_GNOME
12971 "gui_gnome",
12972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973#ifdef FEAT_GUI_MAC
12974 "gui_mac",
12975#endif
12976#ifdef FEAT_GUI_MOTIF
12977 "gui_motif",
12978#endif
12979#ifdef FEAT_GUI_PHOTON
12980 "gui_photon",
12981#endif
12982#ifdef FEAT_GUI_W16
12983 "gui_win16",
12984#endif
12985#ifdef FEAT_GUI_W32
12986 "gui_win32",
12987#endif
12988#ifdef FEAT_HANGULIN
12989 "hangul_input",
12990#endif
12991#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12992 "iconv",
12993#endif
12994#ifdef FEAT_INS_EXPAND
12995 "insert_expand",
12996#endif
12997#ifdef FEAT_JUMPLIST
12998 "jumplist",
12999#endif
13000#ifdef FEAT_KEYMAP
13001 "keymap",
13002#endif
13003#ifdef FEAT_LANGMAP
13004 "langmap",
13005#endif
13006#ifdef FEAT_LIBCALL
13007 "libcall",
13008#endif
13009#ifdef FEAT_LINEBREAK
13010 "linebreak",
13011#endif
13012#ifdef FEAT_LISP
13013 "lispindent",
13014#endif
13015#ifdef FEAT_LISTCMDS
13016 "listcmds",
13017#endif
13018#ifdef FEAT_LOCALMAP
13019 "localmap",
13020#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013021#ifdef FEAT_LUA
13022# ifndef DYNAMIC_LUA
13023 "lua",
13024# endif
13025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026#ifdef FEAT_MENU
13027 "menu",
13028#endif
13029#ifdef FEAT_SESSION
13030 "mksession",
13031#endif
13032#ifdef FEAT_MODIFY_FNAME
13033 "modify_fname",
13034#endif
13035#ifdef FEAT_MOUSE
13036 "mouse",
13037#endif
13038#ifdef FEAT_MOUSESHAPE
13039 "mouseshape",
13040#endif
13041#if defined(UNIX) || defined(VMS)
13042# ifdef FEAT_MOUSE_DEC
13043 "mouse_dec",
13044# endif
13045# ifdef FEAT_MOUSE_GPM
13046 "mouse_gpm",
13047# endif
13048# ifdef FEAT_MOUSE_JSB
13049 "mouse_jsbterm",
13050# endif
13051# ifdef FEAT_MOUSE_NET
13052 "mouse_netterm",
13053# endif
13054# ifdef FEAT_MOUSE_PTERM
13055 "mouse_pterm",
13056# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013057# ifdef FEAT_MOUSE_SGR
13058 "mouse_sgr",
13059# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013060# ifdef FEAT_SYSMOUSE
13061 "mouse_sysmouse",
13062# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013063# ifdef FEAT_MOUSE_URXVT
13064 "mouse_urxvt",
13065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066# ifdef FEAT_MOUSE_XTERM
13067 "mouse_xterm",
13068# endif
13069#endif
13070#ifdef FEAT_MBYTE
13071 "multi_byte",
13072#endif
13073#ifdef FEAT_MBYTE_IME
13074 "multi_byte_ime",
13075#endif
13076#ifdef FEAT_MULTI_LANG
13077 "multi_lang",
13078#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013079#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013080#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013081 "mzscheme",
13082#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_OLE
13085 "ole",
13086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087#ifdef FEAT_PATH_EXTRA
13088 "path_extra",
13089#endif
13090#ifdef FEAT_PERL
13091#ifndef DYNAMIC_PERL
13092 "perl",
13093#endif
13094#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013095#ifdef FEAT_PERSISTENT_UNDO
13096 "persistent_undo",
13097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098#ifdef FEAT_PYTHON
13099#ifndef DYNAMIC_PYTHON
13100 "python",
13101#endif
13102#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013103#ifdef FEAT_PYTHON3
13104#ifndef DYNAMIC_PYTHON3
13105 "python3",
13106#endif
13107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108#ifdef FEAT_POSTSCRIPT
13109 "postscript",
13110#endif
13111#ifdef FEAT_PRINTER
13112 "printer",
13113#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013114#ifdef FEAT_PROFILE
13115 "profile",
13116#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013117#ifdef FEAT_RELTIME
13118 "reltime",
13119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120#ifdef FEAT_QUICKFIX
13121 "quickfix",
13122#endif
13123#ifdef FEAT_RIGHTLEFT
13124 "rightleft",
13125#endif
13126#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13127 "ruby",
13128#endif
13129#ifdef FEAT_SCROLLBIND
13130 "scrollbind",
13131#endif
13132#ifdef FEAT_CMDL_INFO
13133 "showcmd",
13134 "cmdline_info",
13135#endif
13136#ifdef FEAT_SIGNS
13137 "signs",
13138#endif
13139#ifdef FEAT_SMARTINDENT
13140 "smartindent",
13141#endif
13142#ifdef FEAT_SNIFF
13143 "sniff",
13144#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013145#ifdef STARTUPTIME
13146 "startuptime",
13147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148#ifdef FEAT_STL_OPT
13149 "statusline",
13150#endif
13151#ifdef FEAT_SUN_WORKSHOP
13152 "sun_workshop",
13153#endif
13154#ifdef FEAT_NETBEANS_INTG
13155 "netbeans_intg",
13156#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013157#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013158 "spell",
13159#endif
13160#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 "syntax",
13162#endif
13163#if defined(USE_SYSTEM) || !defined(UNIX)
13164 "system",
13165#endif
13166#ifdef FEAT_TAG_BINS
13167 "tag_binary",
13168#endif
13169#ifdef FEAT_TAG_OLDSTATIC
13170 "tag_old_static",
13171#endif
13172#ifdef FEAT_TAG_ANYWHITE
13173 "tag_any_white",
13174#endif
13175#ifdef FEAT_TCL
13176# ifndef DYNAMIC_TCL
13177 "tcl",
13178# endif
13179#endif
13180#ifdef TERMINFO
13181 "terminfo",
13182#endif
13183#ifdef FEAT_TERMRESPONSE
13184 "termresponse",
13185#endif
13186#ifdef FEAT_TEXTOBJ
13187 "textobjects",
13188#endif
13189#ifdef HAVE_TGETENT
13190 "tgetent",
13191#endif
13192#ifdef FEAT_TITLE
13193 "title",
13194#endif
13195#ifdef FEAT_TOOLBAR
13196 "toolbar",
13197#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013198#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13199 "unnamedplus",
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_USR_CMDS
13202 "user-commands", /* was accidentally included in 5.4 */
13203 "user_commands",
13204#endif
13205#ifdef FEAT_VIMINFO
13206 "viminfo",
13207#endif
13208#ifdef FEAT_VERTSPLIT
13209 "vertsplit",
13210#endif
13211#ifdef FEAT_VIRTUALEDIT
13212 "virtualedit",
13213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215#ifdef FEAT_VISUALEXTRA
13216 "visualextra",
13217#endif
13218#ifdef FEAT_VREPLACE
13219 "vreplace",
13220#endif
13221#ifdef FEAT_WILDIGN
13222 "wildignore",
13223#endif
13224#ifdef FEAT_WILDMENU
13225 "wildmenu",
13226#endif
13227#ifdef FEAT_WINDOWS
13228 "windows",
13229#endif
13230#ifdef FEAT_WAK
13231 "winaltkeys",
13232#endif
13233#ifdef FEAT_WRITEBACKUP
13234 "writebackup",
13235#endif
13236#ifdef FEAT_XIM
13237 "xim",
13238#endif
13239#ifdef FEAT_XFONTSET
13240 "xfontset",
13241#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013242#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013243 "xpm",
13244 "xpm_w32", /* for backward compatibility */
13245#else
13246# if defined(HAVE_XPM)
13247 "xpm",
13248# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250#ifdef USE_XSMP
13251 "xsmp",
13252#endif
13253#ifdef USE_XSMP_INTERACT
13254 "xsmp_interact",
13255#endif
13256#ifdef FEAT_XCLIPBOARD
13257 "xterm_clipboard",
13258#endif
13259#ifdef FEAT_XTERM_SAVE
13260 "xterm_save",
13261#endif
13262#if defined(UNIX) && defined(FEAT_X11)
13263 "X11",
13264#endif
13265 NULL
13266 };
13267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 for (i = 0; has_list[i] != NULL; ++i)
13270 if (STRICMP(name, has_list[i]) == 0)
13271 {
13272 n = TRUE;
13273 break;
13274 }
13275
13276 if (n == FALSE)
13277 {
13278 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013279 {
13280 if (name[5] == '-'
13281 && STRLEN(name) > 11
13282 && vim_isdigit(name[6])
13283 && vim_isdigit(name[8])
13284 && vim_isdigit(name[10]))
13285 {
13286 int major = atoi((char *)name + 6);
13287 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013288
13289 /* Expect "patch-9.9.01234". */
13290 n = (major < VIM_VERSION_MAJOR
13291 || (major == VIM_VERSION_MAJOR
13292 && (minor < VIM_VERSION_MINOR
13293 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013294 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013295 }
13296 else
13297 n = has_patch(atoi((char *)name + 5));
13298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 else if (STRICMP(name, "vim_starting") == 0)
13300 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013301#ifdef FEAT_MBYTE
13302 else if (STRICMP(name, "multi_byte_encoding") == 0)
13303 n = has_mbyte;
13304#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013305#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13306 else if (STRICMP(name, "balloon_multiline") == 0)
13307 n = multiline_balloon_available();
13308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#ifdef DYNAMIC_TCL
13310 else if (STRICMP(name, "tcl") == 0)
13311 n = tcl_enabled(FALSE);
13312#endif
13313#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13314 else if (STRICMP(name, "iconv") == 0)
13315 n = iconv_enabled(FALSE);
13316#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013317#ifdef DYNAMIC_LUA
13318 else if (STRICMP(name, "lua") == 0)
13319 n = lua_enabled(FALSE);
13320#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013321#ifdef DYNAMIC_MZSCHEME
13322 else if (STRICMP(name, "mzscheme") == 0)
13323 n = mzscheme_enabled(FALSE);
13324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325#ifdef DYNAMIC_RUBY
13326 else if (STRICMP(name, "ruby") == 0)
13327 n = ruby_enabled(FALSE);
13328#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013329#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330#ifdef DYNAMIC_PYTHON
13331 else if (STRICMP(name, "python") == 0)
13332 n = python_enabled(FALSE);
13333#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013334#endif
13335#ifdef FEAT_PYTHON3
13336#ifdef DYNAMIC_PYTHON3
13337 else if (STRICMP(name, "python3") == 0)
13338 n = python3_enabled(FALSE);
13339#endif
13340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341#ifdef DYNAMIC_PERL
13342 else if (STRICMP(name, "perl") == 0)
13343 n = perl_enabled(FALSE);
13344#endif
13345#ifdef FEAT_GUI
13346 else if (STRICMP(name, "gui_running") == 0)
13347 n = (gui.in_use || gui.starting);
13348# ifdef FEAT_GUI_W32
13349 else if (STRICMP(name, "gui_win32s") == 0)
13350 n = gui_is_win32s();
13351# endif
13352# ifdef FEAT_BROWSE
13353 else if (STRICMP(name, "browse") == 0)
13354 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13355# endif
13356#endif
13357#ifdef FEAT_SYN_HL
13358 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013359 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#endif
13361#if defined(WIN3264)
13362 else if (STRICMP(name, "win95") == 0)
13363 n = mch_windows95();
13364#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013365#ifdef FEAT_NETBEANS_INTG
13366 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013367 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 }
13370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013375 * "has_key()" function
13376 */
13377 static void
13378f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013381{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013382 if (argvars[0].v_type != VAR_DICT)
13383 {
13384 EMSG(_(e_dictreq));
13385 return;
13386 }
13387 if (argvars[0].vval.v_dict == NULL)
13388 return;
13389
13390 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013391 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013392}
13393
13394/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013395 * "haslocaldir()" function
13396 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013397 static void
13398f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013399 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013400 typval_T *rettv;
13401{
13402 rettv->vval.v_number = (curwin->w_localdir != NULL);
13403}
13404
13405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 * "hasmapto()" function
13407 */
13408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013410 typval_T *argvars;
13411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412{
13413 char_u *name;
13414 char_u *mode;
13415 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013416 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013419 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 mode = (char_u *)"nvo";
13421 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013424 if (argvars[2].v_type != VAR_UNKNOWN)
13425 abbr = get_tv_number(&argvars[2]);
13426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427
Bram Moolenaar2c932302006-03-18 21:42:09 +000013428 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013431 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
13434/*
13435 * "histadd()" function
13436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013439 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
13442#ifdef FEAT_CMDHIST
13443 int histype;
13444 char_u *str;
13445 char_u buf[NUMBUFLEN];
13446#endif
13447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 if (check_restricted() || check_secure())
13450 return;
13451#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13453 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 if (histype >= 0)
13455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 if (*str != NUL)
13458 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013459 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 return;
13463 }
13464 }
13465#endif
13466}
13467
13468/*
13469 * "histdel()" function
13470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013473 typval_T *argvars UNUSED;
13474 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
13476#ifdef FEAT_CMDHIST
13477 int n;
13478 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13482 if (str == NULL)
13483 n = 0;
13484 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013486 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013487 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 else
13492 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 get_tv_string_buf(&argvars[1], buf));
13495 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496#endif
13497}
13498
13499/*
13500 * "histget()" function
13501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506{
13507#ifdef FEAT_CMDHIST
13508 int type;
13509 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013512 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13513 if (str == NULL)
13514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013516 {
13517 type = get_histtype(str);
13518 if (argvars[1].v_type == VAR_UNKNOWN)
13519 idx = get_history_idx(type);
13520 else
13521 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13522 /* -1 on type error */
13523 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529}
13530
13531/*
13532 * "histnr()" function
13533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013536 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
13539 int i;
13540
13541#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 char_u *history = get_tv_string_chk(&argvars[0]);
13543
13544 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 if (i >= HIST_CMD && i < HIST_COUNT)
13546 i = get_history_idx(i);
13547 else
13548#endif
13549 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551}
13552
13553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 * "highlightID(name)" function
13555 */
13556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 typval_T *argvars;
13559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562}
13563
13564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013565 * "highlight_exists()" function
13566 */
13567 static void
13568f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571{
13572 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13573}
13574
13575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 * "hostname()" function
13577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582{
13583 char_u hostname[256];
13584
13585 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->v_type = VAR_STRING;
13587 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588}
13589
13590/*
13591 * iconv() function
13592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597{
13598#ifdef FEAT_MBYTE
13599 char_u buf1[NUMBUFLEN];
13600 char_u buf2[NUMBUFLEN];
13601 char_u *from, *to, *str;
13602 vimconv_T vimconv;
13603#endif
13604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->v_type = VAR_STRING;
13606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607
13608#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 str = get_tv_string(&argvars[0]);
13610 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13611 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 vimconv.vc_type = CONV_NONE;
13613 convert_setup(&vimconv, from, to);
13614
13615 /* If the encodings are equal, no conversion needed. */
13616 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
13621 convert_setup(&vimconv, NULL, NULL);
13622 vim_free(from);
13623 vim_free(to);
13624#endif
13625}
13626
13627/*
13628 * "indent()" function
13629 */
13630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634{
13635 linenr_T lnum;
13636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642}
13643
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013644/*
13645 * "index()" function
13646 */
13647 static void
13648f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013651{
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 list_T *l;
13653 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013654 long idx = 0;
13655 int ic = FALSE;
13656
13657 rettv->vval.v_number = -1;
13658 if (argvars[0].v_type != VAR_LIST)
13659 {
13660 EMSG(_(e_listreq));
13661 return;
13662 }
13663 l = argvars[0].vval.v_list;
13664 if (l != NULL)
13665 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013666 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013667 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 int error = FALSE;
13670
Bram Moolenaar758711c2005-02-02 23:11:38 +000013671 /* Start at specified item. Use the cached index that list_find()
13672 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013673 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013674 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013675 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 ic = get_tv_number_chk(&argvars[3], &error);
13677 if (error)
13678 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013679 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013680
Bram Moolenaar758711c2005-02-02 23:11:38 +000013681 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013682 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013683 {
13684 rettv->vval.v_number = idx;
13685 break;
13686 }
13687 }
13688}
13689
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690static int inputsecret_flag = 0;
13691
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013692static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13693
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013695 * This function is used by f_input() and f_inputdialog() functions. The third
13696 * argument to f_input() specifies the type of completion to use at the
13697 * prompt. The third argument to f_inputdialog() specifies the value to return
13698 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 */
13700 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013701get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 typval_T *argvars;
13703 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013704 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 char_u *p = NULL;
13708 int c;
13709 char_u buf[NUMBUFLEN];
13710 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013711 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013712 int xp_type = EXPAND_NOTHING;
13713 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717
13718#ifdef NO_CONSOLE_INPUT
13719 /* While starting up, there is no place to enter text. */
13720 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722#endif
13723
13724 cmd_silent = FALSE; /* Want to see the prompt. */
13725 if (prompt != NULL)
13726 {
13727 /* Only the part of the message after the last NL is considered as
13728 * prompt for the command line */
13729 p = vim_strrchr(prompt, '\n');
13730 if (p == NULL)
13731 p = prompt;
13732 else
13733 {
13734 ++p;
13735 c = *p;
13736 *p = NUL;
13737 msg_start();
13738 msg_clr_eos();
13739 msg_puts_attr(prompt, echo_attr);
13740 msg_didout = FALSE;
13741 msg_starthere();
13742 *p = c;
13743 }
13744 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 if (argvars[1].v_type != VAR_UNKNOWN)
13747 {
13748 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13749 if (defstr != NULL)
13750 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013752 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013753 {
13754 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013755 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013756 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013757
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013758 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013759 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013760
Bram Moolenaar4463f292005-09-25 22:20:24 +000013761 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13762 if (xp_name == NULL)
13763 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013764
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013765 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013766
Bram Moolenaar4463f292005-09-25 22:20:24 +000013767 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13768 &xp_arg) == FAIL)
13769 return;
13770 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013771 }
13772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013774 {
13775# ifdef FEAT_EX_EXTRA
13776 int save_ex_normal_busy = ex_normal_busy;
13777 ex_normal_busy = 0;
13778# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013780 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13781 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013782# ifdef FEAT_EX_EXTRA
13783 ex_normal_busy = save_ex_normal_busy;
13784# endif
13785 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013786 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013787 && argvars[1].v_type != VAR_UNKNOWN
13788 && argvars[2].v_type != VAR_UNKNOWN)
13789 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13790 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013791
13792 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 /* since the user typed this, no need to wait for return */
13795 need_wait_return = FALSE;
13796 msg_didout = FALSE;
13797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 cmd_silent = cmd_silent_save;
13799}
13800
13801/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013802 * "input()" function
13803 * Also handles inputsecret() when inputsecret is set.
13804 */
13805 static void
13806f_input(argvars, rettv)
13807 typval_T *argvars;
13808 typval_T *rettv;
13809{
13810 get_user_input(argvars, rettv, FALSE);
13811}
13812
13813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 * "inputdialog()" function
13815 */
13816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013818 typval_T *argvars;
13819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820{
13821#if defined(FEAT_GUI_TEXTDIALOG)
13822 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13823 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13824 {
13825 char_u *message;
13826 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 message = get_tv_string_chk(&argvars[0]);
13830 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013831 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013832 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 else
13834 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 if (message != NULL && defstr != NULL
13836 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013837 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013838 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 else
13840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 if (message != NULL && defstr != NULL
13842 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013843 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 rettv->vval.v_string = vim_strsave(
13845 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 }
13851 else
13852#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013853 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854}
13855
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013856/*
13857 * "inputlist()" function
13858 */
13859 static void
13860f_inputlist(argvars, rettv)
13861 typval_T *argvars;
13862 typval_T *rettv;
13863{
13864 listitem_T *li;
13865 int selected;
13866 int mouse_used;
13867
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013868#ifdef NO_CONSOLE_INPUT
13869 /* While starting up, there is no place to enter text. */
13870 if (no_console_input())
13871 return;
13872#endif
13873 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13874 {
13875 EMSG2(_(e_listarg), "inputlist()");
13876 return;
13877 }
13878
13879 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013880 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013881 lines_left = Rows; /* avoid more prompt */
13882 msg_scroll = TRUE;
13883 msg_clr_eos();
13884
13885 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13886 {
13887 msg_puts(get_tv_string(&li->li_tv));
13888 msg_putchar('\n');
13889 }
13890
13891 /* Ask for choice. */
13892 selected = prompt_for_number(&mouse_used);
13893 if (mouse_used)
13894 selected -= lines_left;
13895
13896 rettv->vval.v_number = selected;
13897}
13898
13899
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13901
13902/*
13903 * "inputrestore()" function
13904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013907 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
13910 if (ga_userinput.ga_len > 0)
13911 {
13912 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13914 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013915 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 }
13917 else if (p_verbose > 1)
13918 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013919 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 }
13922}
13923
13924/*
13925 * "inputsave()" function
13926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013932 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 if (ga_grow(&ga_userinput, 1) == OK)
13934 {
13935 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13936 + ga_userinput.ga_len);
13937 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013938 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 }
13940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942}
13943
13944/*
13945 * "inputsecret()" function
13946 */
13947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013949 typval_T *argvars;
13950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951{
13952 ++cmdline_star;
13953 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013954 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 --cmdline_star;
13956 --inputsecret_flag;
13957}
13958
13959/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013960 * "insert()" function
13961 */
13962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013966{
13967 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013968 listitem_T *item;
13969 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013971
13972 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013973 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013974 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013975 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013976 {
13977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 before = get_tv_number_chk(&argvars[2], &error);
13979 if (error)
13980 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013981
Bram Moolenaar758711c2005-02-02 23:11:38 +000013982 if (before == l->lv_len)
13983 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984 else
13985 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013986 item = list_find(l, before);
13987 if (item == NULL)
13988 {
13989 EMSGN(_(e_listidx), before);
13990 l = NULL;
13991 }
13992 }
13993 if (l != NULL)
13994 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013995 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013996 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013997 }
13998 }
13999}
14000
14001/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014002 * "invert(expr)" function
14003 */
14004 static void
14005f_invert(argvars, rettv)
14006 typval_T *argvars;
14007 typval_T *rettv;
14008{
14009 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14010}
14011
14012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 * "isdirectory()" function
14014 */
14015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *argvars;
14018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021}
14022
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014023/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014024 * "islocked()" function
14025 */
14026 static void
14027f_islocked(argvars, rettv)
14028 typval_T *argvars;
14029 typval_T *rettv;
14030{
14031 lval_T lv;
14032 char_u *end;
14033 dictitem_T *di;
14034
14035 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014036 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14037 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014038 if (end != NULL && lv.ll_name != NULL)
14039 {
14040 if (*end != NUL)
14041 EMSG(_(e_trailing));
14042 else
14043 {
14044 if (lv.ll_tv == NULL)
14045 {
14046 if (check_changedtick(lv.ll_name))
14047 rettv->vval.v_number = 1; /* always locked */
14048 else
14049 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014050 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014051 if (di != NULL)
14052 {
14053 /* Consider a variable locked when:
14054 * 1. the variable itself is locked
14055 * 2. the value of the variable is locked.
14056 * 3. the List or Dict value is locked.
14057 */
14058 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14059 || tv_islocked(&di->di_tv));
14060 }
14061 }
14062 }
14063 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014064 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014065 else if (lv.ll_newkey != NULL)
14066 EMSG2(_(e_dictkey), lv.ll_newkey);
14067 else if (lv.ll_list != NULL)
14068 /* List item. */
14069 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14070 else
14071 /* Dictionary item. */
14072 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14073 }
14074 }
14075
14076 clear_lval(&lv);
14077}
14078
Bram Moolenaar33570922005-01-25 22:26:29 +000014079static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014080
14081/*
14082 * Turn a dict into a list:
14083 * "what" == 0: list of keys
14084 * "what" == 1: list of values
14085 * "what" == 2: list of items
14086 */
14087 static void
14088dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 typval_T *argvars;
14090 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014091 int what;
14092{
Bram Moolenaar33570922005-01-25 22:26:29 +000014093 list_T *l2;
14094 dictitem_T *di;
14095 hashitem_T *hi;
14096 listitem_T *li;
14097 listitem_T *li2;
14098 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014099 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014100
Bram Moolenaar8c711452005-01-14 21:53:12 +000014101 if (argvars[0].v_type != VAR_DICT)
14102 {
14103 EMSG(_(e_dictreq));
14104 return;
14105 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014106 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107 return;
14108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014110 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014111
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014112 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014115 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014117 --todo;
14118 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014120 li = listitem_alloc();
14121 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014122 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014123 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014124
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014125 if (what == 0)
14126 {
14127 /* keys() */
14128 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014129 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014130 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14131 }
14132 else if (what == 1)
14133 {
14134 /* values() */
14135 copy_tv(&di->di_tv, &li->li_tv);
14136 }
14137 else
14138 {
14139 /* items() */
14140 l2 = list_alloc();
14141 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014142 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014143 li->li_tv.vval.v_list = l2;
14144 if (l2 == NULL)
14145 break;
14146 ++l2->lv_refcount;
14147
14148 li2 = listitem_alloc();
14149 if (li2 == NULL)
14150 break;
14151 list_append(l2, li2);
14152 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014153 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014154 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14155
14156 li2 = listitem_alloc();
14157 if (li2 == NULL)
14158 break;
14159 list_append(l2, li2);
14160 copy_tv(&di->di_tv, &li2->li_tv);
14161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014162 }
14163 }
14164}
14165
14166/*
14167 * "items(dict)" function
14168 */
14169 static void
14170f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 typval_T *argvars;
14172 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014173{
14174 dict_list(argvars, rettv, 2);
14175}
14176
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014178 * "join()" function
14179 */
14180 static void
14181f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 typval_T *argvars;
14183 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014184{
14185 garray_T ga;
14186 char_u *sep;
14187
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014188 if (argvars[0].v_type != VAR_LIST)
14189 {
14190 EMSG(_(e_listreq));
14191 return;
14192 }
14193 if (argvars[0].vval.v_list == NULL)
14194 return;
14195 if (argvars[1].v_type == VAR_UNKNOWN)
14196 sep = (char_u *)" ";
14197 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014199
14200 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201
14202 if (sep != NULL)
14203 {
14204 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014205 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 ga_append(&ga, NUL);
14207 rettv->vval.v_string = (char_u *)ga.ga_data;
14208 }
14209 else
14210 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014211}
14212
14213/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014214 * "keys()" function
14215 */
14216 static void
14217f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *argvars;
14219 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014220{
14221 dict_list(argvars, rettv, 0);
14222}
14223
14224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 * "last_buffer_nr()" function.
14226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014229 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231{
14232 int n = 0;
14233 buf_T *buf;
14234
14235 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14236 if (n < buf->b_fnum)
14237 n = buf->b_fnum;
14238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014240}
14241
14242/*
14243 * "len()" function
14244 */
14245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014247 typval_T *argvars;
14248 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014249{
14250 switch (argvars[0].v_type)
14251 {
14252 case VAR_STRING:
14253 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = (varnumber_T)STRLEN(
14255 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014256 break;
14257 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014259 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260 case VAR_DICT:
14261 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14262 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014264 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014265 break;
14266 }
14267}
14268
Bram Moolenaar33570922005-01-25 22:26:29 +000014269static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014270
14271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014273 typval_T *argvars;
14274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014275 int type;
14276{
14277#ifdef FEAT_LIBCALL
14278 char_u *string_in;
14279 char_u **string_result;
14280 int nr_result;
14281#endif
14282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014284 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286
14287 if (check_restricted() || check_secure())
14288 return;
14289
14290#ifdef FEAT_LIBCALL
14291 /* The first two args must be strings, otherwise its meaningless */
14292 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014294 string_in = NULL;
14295 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296 string_in = argvars[2].vval.v_string;
14297 if (type == VAR_NUMBER)
14298 string_result = NULL;
14299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301 if (mch_libcall(argvars[0].vval.v_string,
14302 argvars[1].vval.v_string,
14303 string_in,
14304 argvars[2].vval.v_number,
14305 string_result,
14306 &nr_result) == OK
14307 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014309 }
14310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311}
14312
14313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014314 * "libcall()" function
14315 */
14316 static void
14317f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014318 typval_T *argvars;
14319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320{
14321 libcall_common(argvars, rettv, VAR_STRING);
14322}
14323
14324/*
14325 * "libcallnr()" function
14326 */
14327 static void
14328f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331{
14332 libcall_common(argvars, rettv, VAR_NUMBER);
14333}
14334
14335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 * "line(string)" function
14337 */
14338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 typval_T *argvars;
14341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
14343 linenr_T lnum = 0;
14344 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014345 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014347 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 if (fp != NULL)
14349 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352
14353/*
14354 * "line2byte(lnum)" function
14355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360{
14361#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363#else
14364 linenr_T lnum;
14365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14371 if (rettv->vval.v_number >= 0)
14372 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373#endif
14374}
14375
14376/*
14377 * "lispindent(lnum)" function
14378 */
14379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014381 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
14384#ifdef FEAT_LISP
14385 pos_T pos;
14386 linenr_T lnum;
14387
14388 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14391 {
14392 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 curwin->w_cursor = pos;
14395 }
14396 else
14397#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399}
14400
14401/*
14402 * "localtime()" function
14403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410}
14411
Bram Moolenaar33570922005-01-25 22:26:29 +000014412static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 int exact;
14419{
14420 char_u *keys;
14421 char_u *which;
14422 char_u buf[NUMBUFLEN];
14423 char_u *keys_buf = NULL;
14424 char_u *rhs;
14425 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014426 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014427 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014428 mapblock_T *mp;
14429 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
14431 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->v_type = VAR_STRING;
14433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 if (*keys == NUL)
14437 return;
14438
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014439 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014443 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014444 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014445 if (argvars[3].v_type != VAR_UNKNOWN)
14446 get_dict = get_tv_number(&argvars[3]);
14447 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 else
14450 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 if (which == NULL)
14452 return;
14453
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 mode = get_map_mode(&which, 0);
14455
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014456 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014457 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014459
14460 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014462 /* Return a string. */
14463 if (rhs != NULL)
14464 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465
Bram Moolenaarbd743252010-10-20 21:23:33 +020014466 }
14467 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14468 {
14469 /* Return a dictionary. */
14470 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14471 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14472 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473
Bram Moolenaarbd743252010-10-20 21:23:33 +020014474 dict_add_nr_str(dict, "lhs", 0L, lhs);
14475 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14476 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14477 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14478 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14479 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14480 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014481 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014482 dict_add_nr_str(dict, "mode", 0L, mapmode);
14483
14484 vim_free(lhs);
14485 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 }
14487}
14488
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014489#ifdef FEAT_FLOAT
14490/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014491 * "log()" function
14492 */
14493 static void
14494f_log(argvars, rettv)
14495 typval_T *argvars;
14496 typval_T *rettv;
14497{
14498 float_T f;
14499
14500 rettv->v_type = VAR_FLOAT;
14501 if (get_float_arg(argvars, &f) == OK)
14502 rettv->vval.v_float = log(f);
14503 else
14504 rettv->vval.v_float = 0.0;
14505}
14506
14507/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014508 * "log10()" function
14509 */
14510 static void
14511f_log10(argvars, rettv)
14512 typval_T *argvars;
14513 typval_T *rettv;
14514{
14515 float_T f;
14516
14517 rettv->v_type = VAR_FLOAT;
14518 if (get_float_arg(argvars, &f) == OK)
14519 rettv->vval.v_float = log10(f);
14520 else
14521 rettv->vval.v_float = 0.0;
14522}
14523#endif
14524
Bram Moolenaar1dced572012-04-05 16:54:08 +020014525#ifdef FEAT_LUA
14526/*
14527 * "luaeval()" function
14528 */
14529 static void
14530f_luaeval(argvars, rettv)
14531 typval_T *argvars;
14532 typval_T *rettv;
14533{
14534 char_u *str;
14535 char_u buf[NUMBUFLEN];
14536
14537 str = get_tv_string_buf(&argvars[0], buf);
14538 do_luaeval(str, argvars + 1, rettv);
14539}
14540#endif
14541
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014543 * "map()" function
14544 */
14545 static void
14546f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014547 typval_T *argvars;
14548 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014549{
14550 filter_map(argvars, rettv, TRUE);
14551}
14552
14553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 */
14556 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *argvars;
14559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562}
14563
14564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 */
14567 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014569 typval_T *argvars;
14570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573}
14574
Bram Moolenaar33570922005-01-25 22:26:29 +000014575static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576
14577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 int type;
14582{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014584 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014585 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 char_u *pat;
14587 regmatch_T regmatch;
14588 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014589 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 char_u *save_cpo;
14591 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014592 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014593 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014594 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 list_T *l = NULL;
14596 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014597 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014598 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
14600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14601 save_cpo = p_cpo;
14602 p_cpo = (char_u *)"";
14603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014604 rettv->vval.v_number = -1;
14605 if (type == 3)
14606 {
14607 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014608 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014609 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014610 }
14611 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014613 rettv->v_type = VAR_STRING;
14614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014617 if (argvars[0].v_type == VAR_LIST)
14618 {
14619 if ((l = argvars[0].vval.v_list) == NULL)
14620 goto theend;
14621 li = l->lv_first;
14622 }
14623 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014624 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014625 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014626 len = (long)STRLEN(str);
14627 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14630 if (pat == NULL)
14631 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014632
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014635 int error = FALSE;
14636
14637 start = get_tv_number_chk(&argvars[2], &error);
14638 if (error)
14639 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014640 if (l != NULL)
14641 {
14642 li = list_find(l, start);
14643 if (li == NULL)
14644 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014645 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014646 }
14647 else
14648 {
14649 if (start < 0)
14650 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014651 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014652 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014653 /* When "count" argument is there ignore matches before "start",
14654 * otherwise skip part of the string. Differs when pattern is "^"
14655 * or "\<". */
14656 if (argvars[3].v_type != VAR_UNKNOWN)
14657 startcol = start;
14658 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014659 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014660 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014661 len -= start;
14662 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014663 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014664
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 nth = get_tv_number_chk(&argvars[3], &error);
14667 if (error)
14668 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 }
14670
14671 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14672 if (regmatch.regprog != NULL)
14673 {
14674 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014676 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014677 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 if (l != NULL)
14679 {
14680 if (li == NULL)
14681 {
14682 match = FALSE;
14683 break;
14684 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014685 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014686 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014687 if (str == NULL)
14688 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014689 }
14690
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014691 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014693 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014694 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014695 if (l == NULL && !match)
14696 break;
14697
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014698 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014699 if (l != NULL)
14700 {
14701 li = li->li_next;
14702 ++idx;
14703 }
14704 else
14705 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014706#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014707 startcol = (colnr_T)(regmatch.startp[0]
14708 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014710 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014711#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014712 if (startcol > (colnr_T)len
14713 || str + startcol <= regmatch.startp[0])
14714 {
14715 match = FALSE;
14716 break;
14717 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014718 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014719 }
14720
14721 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725 int i;
14726
14727 /* return list with matched string and submatches */
14728 for (i = 0; i < NSUBEXP; ++i)
14729 {
14730 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014731 {
14732 if (list_append_string(rettv->vval.v_list,
14733 (char_u *)"", 0) == FAIL)
14734 break;
14735 }
14736 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014737 regmatch.startp[i],
14738 (int)(regmatch.endp[i] - regmatch.startp[i]))
14739 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741 }
14742 }
14743 else if (type == 2)
14744 {
14745 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014746 if (l != NULL)
14747 copy_tv(&li->li_tv, rettv);
14748 else
14749 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014751 }
14752 else if (l != NULL)
14753 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 else
14755 {
14756 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 (varnumber_T)(regmatch.startp[0] - str);
14759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014762 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 }
14764 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014765 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 }
14767
14768theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 p_cpo = save_cpo;
14771}
14772
14773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 * "match()" function
14775 */
14776 static void
14777f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 typval_T *argvars;
14779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780{
14781 find_some_match(argvars, rettv, 1);
14782}
14783
14784/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014785 * "matchadd()" function
14786 */
14787 static void
14788f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014789 typval_T *argvars UNUSED;
14790 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014791{
14792#ifdef FEAT_SEARCH_EXTRA
14793 char_u buf[NUMBUFLEN];
14794 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14795 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14796 int prio = 10; /* default priority */
14797 int id = -1;
14798 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014799 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014800
14801 rettv->vval.v_number = -1;
14802
14803 if (grp == NULL || pat == NULL)
14804 return;
14805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014806 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014807 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014808 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014809 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014810 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014811 if (argvars[4].v_type != VAR_UNKNOWN)
14812 {
14813 if (argvars[4].v_type != VAR_DICT)
14814 {
14815 EMSG(_(e_dictreq));
14816 return;
14817 }
14818 if (dict_find(argvars[4].vval.v_dict,
14819 (char_u *)"conceal", -1) != NULL)
14820 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14821 (char_u *)"conceal", FALSE);
14822 }
14823 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014824 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014825 if (error == TRUE)
14826 return;
14827 if (id >= 1 && id <= 3)
14828 {
14829 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14830 return;
14831 }
14832
Bram Moolenaar6561d522015-07-21 15:48:27 +020014833 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14834 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014835#endif
14836}
14837
14838/*
14839 * "matchaddpos()" function
14840 */
14841 static void
14842f_matchaddpos(argvars, rettv)
14843 typval_T *argvars UNUSED;
14844 typval_T *rettv UNUSED;
14845{
14846#ifdef FEAT_SEARCH_EXTRA
14847 char_u buf[NUMBUFLEN];
14848 char_u *group;
14849 int prio = 10;
14850 int id = -1;
14851 int error = FALSE;
14852 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014853 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014854
14855 rettv->vval.v_number = -1;
14856
14857 group = get_tv_string_buf_chk(&argvars[0], buf);
14858 if (group == NULL)
14859 return;
14860
14861 if (argvars[1].v_type != VAR_LIST)
14862 {
14863 EMSG2(_(e_listarg), "matchaddpos()");
14864 return;
14865 }
14866 l = argvars[1].vval.v_list;
14867 if (l == NULL)
14868 return;
14869
14870 if (argvars[2].v_type != VAR_UNKNOWN)
14871 {
14872 prio = get_tv_number_chk(&argvars[2], &error);
14873 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014874 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014875 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014876 if (argvars[4].v_type != VAR_UNKNOWN)
14877 {
14878 if (argvars[4].v_type != VAR_DICT)
14879 {
14880 EMSG(_(e_dictreq));
14881 return;
14882 }
14883 if (dict_find(argvars[4].vval.v_dict,
14884 (char_u *)"conceal", -1) != NULL)
14885 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14886 (char_u *)"conceal", FALSE);
14887 }
14888 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014889 }
14890 if (error == TRUE)
14891 return;
14892
14893 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14894 if (id == 1 || id == 2)
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, group, NULL, prio, id, l,
14901 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902#endif
14903}
14904
14905/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014906 * "matcharg()" function
14907 */
14908 static void
14909f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014910 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014911 typval_T *rettv;
14912{
14913 if (rettv_list_alloc(rettv) == OK)
14914 {
14915#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014916 int id = get_tv_number(&argvars[0]);
14917 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014918
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014919 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014920 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014921 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14922 {
14923 list_append_string(rettv->vval.v_list,
14924 syn_id2name(m->hlg_id), -1);
14925 list_append_string(rettv->vval.v_list, m->pattern, -1);
14926 }
14927 else
14928 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014929 list_append_string(rettv->vval.v_list, NULL, -1);
14930 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014931 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014932 }
14933#endif
14934 }
14935}
14936
14937/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014938 * "matchdelete()" function
14939 */
14940 static void
14941f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014942 typval_T *argvars UNUSED;
14943 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014944{
14945#ifdef FEAT_SEARCH_EXTRA
14946 rettv->vval.v_number = match_delete(curwin,
14947 (int)get_tv_number(&argvars[0]), TRUE);
14948#endif
14949}
14950
14951/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952 * "matchend()" function
14953 */
14954 static void
14955f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 typval_T *argvars;
14957 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958{
14959 find_some_match(argvars, rettv, 0);
14960}
14961
14962/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963 * "matchlist()" function
14964 */
14965 static void
14966f_matchlist(argvars, rettv)
14967 typval_T *argvars;
14968 typval_T *rettv;
14969{
14970 find_some_match(argvars, rettv, 3);
14971}
14972
14973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014974 * "matchstr()" function
14975 */
14976 static void
14977f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014978 typval_T *argvars;
14979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980{
14981 find_some_match(argvars, rettv, 2);
14982}
14983
Bram Moolenaar33570922005-01-25 22:26:29 +000014984static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014985
14986 static void
14987max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 typval_T *argvars;
14989 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014990 int domax;
14991{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014992 long n = 0;
14993 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014994 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014995
14996 if (argvars[0].v_type == VAR_LIST)
14997 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014998 list_T *l;
14999 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015000
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015001 l = argvars[0].vval.v_list;
15002 if (l != NULL)
15003 {
15004 li = l->lv_first;
15005 if (li != NULL)
15006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015008 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015009 {
15010 li = li->li_next;
15011 if (li == NULL)
15012 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015013 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015014 if (domax ? i > n : i < n)
15015 n = i;
15016 }
15017 }
15018 }
15019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015020 else if (argvars[0].v_type == VAR_DICT)
15021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015023 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015026
15027 d = argvars[0].vval.v_dict;
15028 if (d != NULL)
15029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015030 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015031 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015033 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015034 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015035 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015037 if (first)
15038 {
15039 n = i;
15040 first = FALSE;
15041 }
15042 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015043 n = i;
15044 }
15045 }
15046 }
15047 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015048 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015049 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015051}
15052
15053/*
15054 * "max()" function
15055 */
15056 static void
15057f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *argvars;
15059 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015060{
15061 max_min(argvars, rettv, TRUE);
15062}
15063
15064/*
15065 * "min()" function
15066 */
15067 static void
15068f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
15070 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015071{
15072 max_min(argvars, rettv, FALSE);
15073}
15074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015075static int mkdir_recurse __ARGS((char_u *dir, int prot));
15076
15077/*
15078 * Create the directory in which "dir" is located, and higher levels when
15079 * needed.
15080 */
15081 static int
15082mkdir_recurse(dir, prot)
15083 char_u *dir;
15084 int prot;
15085{
15086 char_u *p;
15087 char_u *updir;
15088 int r = FAIL;
15089
15090 /* Get end of directory name in "dir".
15091 * We're done when it's "/" or "c:/". */
15092 p = gettail_sep(dir);
15093 if (p <= get_past_head(dir))
15094 return OK;
15095
15096 /* If the directory exists we're done. Otherwise: create it.*/
15097 updir = vim_strnsave(dir, (int)(p - dir));
15098 if (updir == NULL)
15099 return FAIL;
15100 if (mch_isdir(updir))
15101 r = OK;
15102 else if (mkdir_recurse(updir, prot) == OK)
15103 r = vim_mkdir_emsg(updir, prot);
15104 vim_free(updir);
15105 return r;
15106}
15107
15108#ifdef vim_mkdir
15109/*
15110 * "mkdir()" function
15111 */
15112 static void
15113f_mkdir(argvars, rettv)
15114 typval_T *argvars;
15115 typval_T *rettv;
15116{
15117 char_u *dir;
15118 char_u buf[NUMBUFLEN];
15119 int prot = 0755;
15120
15121 rettv->vval.v_number = FAIL;
15122 if (check_restricted() || check_secure())
15123 return;
15124
15125 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015126 if (*dir == NUL)
15127 rettv->vval.v_number = FAIL;
15128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015129 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015130 if (*gettail(dir) == NUL)
15131 /* remove trailing slashes */
15132 *gettail_sep(dir) = NUL;
15133
15134 if (argvars[1].v_type != VAR_UNKNOWN)
15135 {
15136 if (argvars[2].v_type != VAR_UNKNOWN)
15137 prot = get_tv_number_chk(&argvars[2], NULL);
15138 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15139 mkdir_recurse(dir, prot);
15140 }
15141 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015142 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015143}
15144#endif
15145
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 * "mode()" function
15148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 typval_T *argvars;
15152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015154 char_u buf[3];
15155
15156 buf[1] = NUL;
15157 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 if (VIsual_active)
15160 {
15161 if (VIsual_select)
15162 buf[0] = VIsual_mode + 's' - 'v';
15163 else
15164 buf[0] = VIsual_mode;
15165 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015166 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015167 || State == CONFIRM)
15168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015170 if (State == ASKMORE)
15171 buf[1] = 'm';
15172 else if (State == CONFIRM)
15173 buf[1] = '?';
15174 }
15175 else if (State == EXTERNCMD)
15176 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 else if (State & INSERT)
15178 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015179#ifdef FEAT_VREPLACE
15180 if (State & VREPLACE_FLAG)
15181 {
15182 buf[0] = 'R';
15183 buf[1] = 'v';
15184 }
15185 else
15186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 if (State & REPLACE_FLAG)
15188 buf[0] = 'R';
15189 else
15190 buf[0] = 'i';
15191 }
15192 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015195 if (exmode_active)
15196 buf[1] = 'v';
15197 }
15198 else if (exmode_active)
15199 {
15200 buf[0] = 'c';
15201 buf[1] = 'e';
15202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015206 if (finish_op)
15207 buf[1] = 'o';
15208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015210 /* Clear out the minor mode when the argument is not a non-zero number or
15211 * non-empty string. */
15212 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015213 buf[1] = NUL;
15214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015215 rettv->vval.v_string = vim_strsave(buf);
15216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217}
15218
Bram Moolenaar429fa852013-04-15 12:27:36 +020015219#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015220/*
15221 * "mzeval()" function
15222 */
15223 static void
15224f_mzeval(argvars, rettv)
15225 typval_T *argvars;
15226 typval_T *rettv;
15227{
15228 char_u *str;
15229 char_u buf[NUMBUFLEN];
15230
15231 str = get_tv_string_buf(&argvars[0], buf);
15232 do_mzeval(str, rettv);
15233}
Bram Moolenaar75676462013-01-30 14:55:42 +010015234
15235 void
15236mzscheme_call_vim(name, args, rettv)
15237 char_u *name;
15238 typval_T *args;
15239 typval_T *rettv;
15240{
15241 typval_T argvars[3];
15242
15243 argvars[0].v_type = VAR_STRING;
15244 argvars[0].vval.v_string = name;
15245 copy_tv(args, &argvars[1]);
15246 argvars[2].v_type = VAR_UNKNOWN;
15247 f_call(argvars, rettv);
15248 clear_tv(&argvars[1]);
15249}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015250#endif
15251
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 * "nextnonblank()" function
15254 */
15255 static void
15256f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259{
15260 linenr_T lnum;
15261
15262 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015265 {
15266 lnum = 0;
15267 break;
15268 }
15269 if (*skipwhite(ml_get(lnum)) != NUL)
15270 break;
15271 }
15272 rettv->vval.v_number = lnum;
15273}
15274
15275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 * "nr2char()" function
15277 */
15278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015279f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282{
15283 char_u buf[NUMBUFLEN];
15284
15285#ifdef FEAT_MBYTE
15286 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015287 {
15288 int utf8 = 0;
15289
15290 if (argvars[1].v_type != VAR_UNKNOWN)
15291 utf8 = get_tv_number_chk(&argvars[1], NULL);
15292 if (utf8)
15293 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15294 else
15295 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 else
15298#endif
15299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 buf[1] = NUL;
15302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 rettv->v_type = VAR_STRING;
15304 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305}
15306
15307/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015308 * "or(expr, expr)" function
15309 */
15310 static void
15311f_or(argvars, rettv)
15312 typval_T *argvars;
15313 typval_T *rettv;
15314{
15315 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15316 | get_tv_number_chk(&argvars[1], NULL);
15317}
15318
15319/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015320 * "pathshorten()" function
15321 */
15322 static void
15323f_pathshorten(argvars, rettv)
15324 typval_T *argvars;
15325 typval_T *rettv;
15326{
15327 char_u *p;
15328
15329 rettv->v_type = VAR_STRING;
15330 p = get_tv_string_chk(&argvars[0]);
15331 if (p == NULL)
15332 rettv->vval.v_string = NULL;
15333 else
15334 {
15335 p = vim_strsave(p);
15336 rettv->vval.v_string = p;
15337 if (p != NULL)
15338 shorten_dir(p);
15339 }
15340}
15341
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015342#ifdef FEAT_FLOAT
15343/*
15344 * "pow()" function
15345 */
15346 static void
15347f_pow(argvars, rettv)
15348 typval_T *argvars;
15349 typval_T *rettv;
15350{
15351 float_T fx, fy;
15352
15353 rettv->v_type = VAR_FLOAT;
15354 if (get_float_arg(argvars, &fx) == OK
15355 && get_float_arg(&argvars[1], &fy) == OK)
15356 rettv->vval.v_float = pow(fx, fy);
15357 else
15358 rettv->vval.v_float = 0.0;
15359}
15360#endif
15361
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363 * "prevnonblank()" function
15364 */
15365 static void
15366f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015367 typval_T *argvars;
15368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369{
15370 linenr_T lnum;
15371
15372 lnum = get_tv_lnum(argvars);
15373 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15374 lnum = 0;
15375 else
15376 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15377 --lnum;
15378 rettv->vval.v_number = lnum;
15379}
15380
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015381#ifdef HAVE_STDARG_H
15382/* This dummy va_list is here because:
15383 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15384 * - locally in the function results in a "used before set" warning
15385 * - using va_start() to initialize it gives "function with fixed args" error */
15386static va_list ap;
15387#endif
15388
Bram Moolenaar8c711452005-01-14 21:53:12 +000015389/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015390 * "printf()" function
15391 */
15392 static void
15393f_printf(argvars, rettv)
15394 typval_T *argvars;
15395 typval_T *rettv;
15396{
15397 rettv->v_type = VAR_STRING;
15398 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015399#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015400 {
15401 char_u buf[NUMBUFLEN];
15402 int len;
15403 char_u *s;
15404 int saved_did_emsg = did_emsg;
15405 char *fmt;
15406
15407 /* Get the required length, allocate the buffer and do it for real. */
15408 did_emsg = FALSE;
15409 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015410 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015411 if (!did_emsg)
15412 {
15413 s = alloc(len + 1);
15414 if (s != NULL)
15415 {
15416 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015417 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015418 }
15419 }
15420 did_emsg |= saved_did_emsg;
15421 }
15422#endif
15423}
15424
15425/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015426 * "pumvisible()" function
15427 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015428 static void
15429f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015430 typval_T *argvars UNUSED;
15431 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015432{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015433#ifdef FEAT_INS_EXPAND
15434 if (pum_visible())
15435 rettv->vval.v_number = 1;
15436#endif
15437}
15438
Bram Moolenaardb913952012-06-29 12:54:53 +020015439#ifdef FEAT_PYTHON3
15440/*
15441 * "py3eval()" function
15442 */
15443 static void
15444f_py3eval(argvars, rettv)
15445 typval_T *argvars;
15446 typval_T *rettv;
15447{
15448 char_u *str;
15449 char_u buf[NUMBUFLEN];
15450
15451 str = get_tv_string_buf(&argvars[0], buf);
15452 do_py3eval(str, rettv);
15453}
15454#endif
15455
15456#ifdef FEAT_PYTHON
15457/*
15458 * "pyeval()" function
15459 */
15460 static void
15461f_pyeval(argvars, rettv)
15462 typval_T *argvars;
15463 typval_T *rettv;
15464{
15465 char_u *str;
15466 char_u buf[NUMBUFLEN];
15467
15468 str = get_tv_string_buf(&argvars[0], buf);
15469 do_pyeval(str, rettv);
15470}
15471#endif
15472
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015473/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015474 * "range()" function
15475 */
15476 static void
15477f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *argvars;
15479 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015480{
15481 long start;
15482 long end;
15483 long stride = 1;
15484 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015485 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015488 if (argvars[1].v_type == VAR_UNKNOWN)
15489 {
15490 end = start - 1;
15491 start = 0;
15492 }
15493 else
15494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015495 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015496 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015497 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015498 }
15499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015500 if (error)
15501 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015502 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015503 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015504 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015505 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015506 else
15507 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015508 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015509 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015510 if (list_append_number(rettv->vval.v_list,
15511 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015512 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015513 }
15514}
15515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015516/*
15517 * "readfile()" function
15518 */
15519 static void
15520f_readfile(argvars, rettv)
15521 typval_T *argvars;
15522 typval_T *rettv;
15523{
15524 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015525 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015526 char_u *fname;
15527 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15529 int io_size = sizeof(buf);
15530 int readlen; /* size of last fread() */
15531 char_u *prev = NULL; /* previously read bytes, if any */
15532 long prevlen = 0; /* length of data in prev */
15533 long prevsize = 0; /* size of prev buffer */
15534 long maxline = MAXLNUM;
15535 long cnt = 0;
15536 char_u *p; /* position in buf */
15537 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015538
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015539 if (argvars[1].v_type != VAR_UNKNOWN)
15540 {
15541 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15542 binary = TRUE;
15543 if (argvars[2].v_type != VAR_UNKNOWN)
15544 maxline = get_tv_number(&argvars[2]);
15545 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015547 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549
15550 /* Always open the file in binary mode, library functions have a mind of
15551 * their own about CR-LF conversion. */
15552 fname = get_tv_string(&argvars[0]);
15553 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15554 {
15555 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15556 return;
15557 }
15558
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015559 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015560 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015561 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015562
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015563 /* This for loop processes what was read, but is also entered at end
15564 * of file so that either:
15565 * - an incomplete line gets written
15566 * - a "binary" file gets an empty line at the end if it ends in a
15567 * newline. */
15568 for (p = buf, start = buf;
15569 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15570 ++p)
15571 {
15572 if (*p == '\n' || readlen <= 0)
15573 {
15574 listitem_T *li;
15575 char_u *s = NULL;
15576 long_u len = p - start;
15577
15578 /* Finished a line. Remove CRs before NL. */
15579 if (readlen > 0 && !binary)
15580 {
15581 while (len > 0 && start[len - 1] == '\r')
15582 --len;
15583 /* removal may cross back to the "prev" string */
15584 if (len == 0)
15585 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15586 --prevlen;
15587 }
15588 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015589 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015590 else
15591 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015592 /* Change "prev" buffer to be the right size. This way
15593 * the bytes are only copied once, and very long lines are
15594 * allocated only once. */
15595 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015596 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015597 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015598 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015599 prev = NULL; /* the list will own the string */
15600 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015601 }
15602 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015603 if (s == NULL)
15604 {
15605 do_outofmem_msg((long_u) prevlen + len + 1);
15606 failed = TRUE;
15607 break;
15608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015610 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015611 {
15612 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015613 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015614 break;
15615 }
15616 li->li_tv.v_type = VAR_STRING;
15617 li->li_tv.v_lock = 0;
15618 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015619 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015620
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015621 start = p + 1; /* step over newline */
15622 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015623 break;
15624 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015625 else if (*p == NUL)
15626 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015627#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015628 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15629 * when finding the BF and check the previous two bytes. */
15630 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015631 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015632 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15633 * + 1, these may be in the "prev" string. */
15634 char_u back1 = p >= buf + 1 ? p[-1]
15635 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15636 char_u back2 = p >= buf + 2 ? p[-2]
15637 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15638 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015639
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015640 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015642 char_u *dest = p - 2;
15643
15644 /* Usually a BOM is at the beginning of a file, and so at
15645 * the beginning of a line; then we can just step over it.
15646 */
15647 if (start == dest)
15648 start = p + 1;
15649 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015650 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015651 /* have to shuffle buf to close gap */
15652 int adjust_prevlen = 0;
15653
15654 if (dest < buf)
15655 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015656 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015657 dest = buf;
15658 }
15659 if (readlen > p - buf + 1)
15660 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15661 readlen -= 3 - adjust_prevlen;
15662 prevlen -= adjust_prevlen;
15663 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015664 }
15665 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015666 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015667#endif
15668 } /* for */
15669
15670 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15671 break;
15672 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015673 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015674 /* There's part of a line in buf, store it in "prev". */
15675 if (p - start + prevlen >= prevsize)
15676 {
15677 /* need bigger "prev" buffer */
15678 char_u *newprev;
15679
15680 /* A common use case is ordinary text files and "prev" gets a
15681 * fragment of a line, so the first allocation is made
15682 * small, to avoid repeatedly 'allocing' large and
15683 * 'reallocing' small. */
15684 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015685 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015686 else
15687 {
15688 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015689 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015690 prevsize = grow50pc > growmin ? grow50pc : growmin;
15691 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015692 newprev = prev == NULL ? alloc(prevsize)
15693 : vim_realloc(prev, prevsize);
15694 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015695 {
15696 do_outofmem_msg((long_u)prevsize);
15697 failed = TRUE;
15698 break;
15699 }
15700 prev = newprev;
15701 }
15702 /* Add the line part to end of "prev". */
15703 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015704 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015706 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015708 /*
15709 * For a negative line count use only the lines at the end of the file,
15710 * free the rest.
15711 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015712 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015713 while (cnt > -maxline)
15714 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015715 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015716 --cnt;
15717 }
15718
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015719 if (failed)
15720 {
15721 list_free(rettv->vval.v_list, TRUE);
15722 /* readfile doc says an empty list is returned on error */
15723 rettv->vval.v_list = list_alloc();
15724 }
15725
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015726 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015727 fclose(fd);
15728}
15729
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015730#if defined(FEAT_RELTIME)
15731static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15732
15733/*
15734 * Convert a List to proftime_T.
15735 * Return FAIL when there is something wrong.
15736 */
15737 static int
15738list2proftime(arg, tm)
15739 typval_T *arg;
15740 proftime_T *tm;
15741{
15742 long n1, n2;
15743 int error = FALSE;
15744
15745 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15746 || arg->vval.v_list->lv_len != 2)
15747 return FAIL;
15748 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15749 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15750# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015751 tm->HighPart = n1;
15752 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015753# else
15754 tm->tv_sec = n1;
15755 tm->tv_usec = n2;
15756# endif
15757 return error ? FAIL : OK;
15758}
15759#endif /* FEAT_RELTIME */
15760
15761/*
15762 * "reltime()" function
15763 */
15764 static void
15765f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015766 typval_T *argvars UNUSED;
15767 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015768{
15769#ifdef FEAT_RELTIME
15770 proftime_T res;
15771 proftime_T start;
15772
15773 if (argvars[0].v_type == VAR_UNKNOWN)
15774 {
15775 /* No arguments: get current time. */
15776 profile_start(&res);
15777 }
15778 else if (argvars[1].v_type == VAR_UNKNOWN)
15779 {
15780 if (list2proftime(&argvars[0], &res) == FAIL)
15781 return;
15782 profile_end(&res);
15783 }
15784 else
15785 {
15786 /* Two arguments: compute the difference. */
15787 if (list2proftime(&argvars[0], &start) == FAIL
15788 || list2proftime(&argvars[1], &res) == FAIL)
15789 return;
15790 profile_sub(&res, &start);
15791 }
15792
15793 if (rettv_list_alloc(rettv) == OK)
15794 {
15795 long n1, n2;
15796
15797# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015798 n1 = res.HighPart;
15799 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015800# else
15801 n1 = res.tv_sec;
15802 n2 = res.tv_usec;
15803# endif
15804 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15805 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15806 }
15807#endif
15808}
15809
15810/*
15811 * "reltimestr()" function
15812 */
15813 static void
15814f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015815 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015816 typval_T *rettv;
15817{
15818#ifdef FEAT_RELTIME
15819 proftime_T tm;
15820#endif
15821
15822 rettv->v_type = VAR_STRING;
15823 rettv->vval.v_string = NULL;
15824#ifdef FEAT_RELTIME
15825 if (list2proftime(&argvars[0], &tm) == OK)
15826 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15827#endif
15828}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015829
Bram Moolenaar0d660222005-01-07 21:51:51 +000015830#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15831static void make_connection __ARGS((void));
15832static int check_connection __ARGS((void));
15833
15834 static void
15835make_connection()
15836{
15837 if (X_DISPLAY == NULL
15838# ifdef FEAT_GUI
15839 && !gui.in_use
15840# endif
15841 )
15842 {
15843 x_force_connect = TRUE;
15844 setup_term_clip();
15845 x_force_connect = FALSE;
15846 }
15847}
15848
15849 static int
15850check_connection()
15851{
15852 make_connection();
15853 if (X_DISPLAY == NULL)
15854 {
15855 EMSG(_("E240: No connection to Vim server"));
15856 return FAIL;
15857 }
15858 return OK;
15859}
15860#endif
15861
15862#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015863static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864
15865 static void
15866remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 typval_T *argvars;
15868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869 int expr;
15870{
15871 char_u *server_name;
15872 char_u *keys;
15873 char_u *r = NULL;
15874 char_u buf[NUMBUFLEN];
15875# ifdef WIN32
15876 HWND w;
15877# else
15878 Window w;
15879# endif
15880
15881 if (check_restricted() || check_secure())
15882 return;
15883
15884# ifdef FEAT_X11
15885 if (check_connection() == FAIL)
15886 return;
15887# endif
15888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015889 server_name = get_tv_string_chk(&argvars[0]);
15890 if (server_name == NULL)
15891 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892 keys = get_tv_string_buf(&argvars[1], buf);
15893# ifdef WIN32
15894 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15895# else
15896 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15897 < 0)
15898# endif
15899 {
15900 if (r != NULL)
15901 EMSG(r); /* sending worked but evaluation failed */
15902 else
15903 EMSG2(_("E241: Unable to send to %s"), server_name);
15904 return;
15905 }
15906
15907 rettv->vval.v_string = r;
15908
15909 if (argvars[2].v_type != VAR_UNKNOWN)
15910 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015911 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015912 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015915 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 v.di_tv.v_type = VAR_STRING;
15917 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015918 idvar = get_tv_string_chk(&argvars[2]);
15919 if (idvar != NULL)
15920 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015921 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 }
15923}
15924#endif
15925
15926/*
15927 * "remote_expr()" function
15928 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015929 static void
15930f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933{
15934 rettv->v_type = VAR_STRING;
15935 rettv->vval.v_string = NULL;
15936#ifdef FEAT_CLIENTSERVER
15937 remote_common(argvars, rettv, TRUE);
15938#endif
15939}
15940
15941/*
15942 * "remote_foreground()" function
15943 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944 static void
15945f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015946 typval_T *argvars UNUSED;
15947 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015949#ifdef FEAT_CLIENTSERVER
15950# ifdef WIN32
15951 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015952 {
15953 char_u *server_name = get_tv_string_chk(&argvars[0]);
15954
15955 if (server_name != NULL)
15956 serverForeground(server_name);
15957 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015958# else
15959 /* Send a foreground() expression to the server. */
15960 argvars[1].v_type = VAR_STRING;
15961 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15962 argvars[2].v_type = VAR_UNKNOWN;
15963 remote_common(argvars, rettv, TRUE);
15964 vim_free(argvars[1].vval.v_string);
15965# endif
15966#endif
15967}
15968
Bram Moolenaar0d660222005-01-07 21:51:51 +000015969 static void
15970f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015973{
15974#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976 char_u *s = NULL;
15977# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015978 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015980 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981
15982 if (check_restricted() || check_secure())
15983 {
15984 rettv->vval.v_number = -1;
15985 return;
15986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015987 serverid = get_tv_string_chk(&argvars[0]);
15988 if (serverid == NULL)
15989 {
15990 rettv->vval.v_number = -1;
15991 return; /* type error; errmsg already given */
15992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015994 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015995 if (n == 0)
15996 rettv->vval.v_number = -1;
15997 else
15998 {
15999 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16000 rettv->vval.v_number = (s != NULL);
16001 }
16002# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003 if (check_connection() == FAIL)
16004 return;
16005
16006 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016007 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016008# endif
16009
16010 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 char_u *retvar;
16013
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 v.di_tv.v_type = VAR_STRING;
16015 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016016 retvar = get_tv_string_chk(&argvars[1]);
16017 if (retvar != NULL)
16018 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020 }
16021#else
16022 rettv->vval.v_number = -1;
16023#endif
16024}
16025
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 static void
16027f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016029 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016030{
16031 char_u *r = NULL;
16032
16033#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 char_u *serverid = get_tv_string_chk(&argvars[0]);
16035
16036 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016037 {
16038# ifdef WIN32
16039 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016040 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016041
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016042 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 if (n != 0)
16044 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16045 if (r == NULL)
16046# else
16047 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016049# endif
16050 EMSG(_("E277: Unable to read a server reply"));
16051 }
16052#endif
16053 rettv->v_type = VAR_STRING;
16054 rettv->vval.v_string = r;
16055}
16056
16057/*
16058 * "remote_send()" function
16059 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016060 static void
16061f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016064{
16065 rettv->v_type = VAR_STRING;
16066 rettv->vval.v_string = NULL;
16067#ifdef FEAT_CLIENTSERVER
16068 remote_common(argvars, rettv, FALSE);
16069#endif
16070}
16071
16072/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016074 */
16075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 typval_T *argvars;
16078 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016079{
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 list_T *l;
16081 listitem_T *item, *item2;
16082 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016083 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016084 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016085 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 dict_T *d;
16087 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016088 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016089
Bram Moolenaar8c711452005-01-14 21:53:12 +000016090 if (argvars[0].v_type == VAR_DICT)
16091 {
16092 if (argvars[2].v_type != VAR_UNKNOWN)
16093 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016094 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016095 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 key = get_tv_string_chk(&argvars[1]);
16098 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 di = dict_find(d, key, -1);
16101 if (di == NULL)
16102 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016103 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16104 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 {
16106 *rettv = di->di_tv;
16107 init_tv(&di->di_tv);
16108 dictitem_remove(d, di);
16109 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016111 }
16112 }
16113 else if (argvars[0].v_type != VAR_LIST)
16114 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016115 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016116 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 int error = FALSE;
16119
16120 idx = get_tv_number_chk(&argvars[1], &error);
16121 if (error)
16122 ; /* type error: do nothing, errmsg already given */
16123 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 EMSGN(_(e_listidx), idx);
16125 else
16126 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016127 if (argvars[2].v_type == VAR_UNKNOWN)
16128 {
16129 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016130 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016131 *rettv = item->li_tv;
16132 vim_free(item);
16133 }
16134 else
16135 {
16136 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016137 end = get_tv_number_chk(&argvars[2], &error);
16138 if (error)
16139 ; /* type error: do nothing */
16140 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016141 EMSGN(_(e_listidx), end);
16142 else
16143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016144 int cnt = 0;
16145
16146 for (li = item; li != NULL; li = li->li_next)
16147 {
16148 ++cnt;
16149 if (li == item2)
16150 break;
16151 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016152 if (li == NULL) /* didn't find "item2" after "item" */
16153 EMSG(_(e_invrange));
16154 else
16155 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016156 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016157 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016158 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016160 l->lv_first = item;
16161 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016162 item->li_prev = NULL;
16163 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016164 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016165 }
16166 }
16167 }
16168 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016169 }
16170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171}
16172
16173/*
16174 * "rename({from}, {to})" function
16175 */
16176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016177f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 typval_T *argvars;
16179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180{
16181 char_u buf[NUMBUFLEN];
16182
16183 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16187 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188}
16189
16190/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016191 * "repeat()" function
16192 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016193 static void
16194f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016195 typval_T *argvars;
16196 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016197{
16198 char_u *p;
16199 int n;
16200 int slen;
16201 int len;
16202 char_u *r;
16203 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016204
16205 n = get_tv_number(&argvars[1]);
16206 if (argvars[0].v_type == VAR_LIST)
16207 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016208 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016209 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016210 if (list_extend(rettv->vval.v_list,
16211 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016212 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016213 }
16214 else
16215 {
16216 p = get_tv_string(&argvars[0]);
16217 rettv->v_type = VAR_STRING;
16218 rettv->vval.v_string = NULL;
16219
16220 slen = (int)STRLEN(p);
16221 len = slen * n;
16222 if (len <= 0)
16223 return;
16224
16225 r = alloc(len + 1);
16226 if (r != NULL)
16227 {
16228 for (i = 0; i < n; i++)
16229 mch_memmove(r + i * slen, p, (size_t)slen);
16230 r[len] = NUL;
16231 }
16232
16233 rettv->vval.v_string = r;
16234 }
16235}
16236
16237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 * "resolve()" function
16239 */
16240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016241f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *argvars;
16243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
16245 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016246#ifdef HAVE_READLINK
16247 char_u *buf = NULL;
16248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251#ifdef FEAT_SHORTCUT
16252 {
16253 char_u *v = NULL;
16254
16255 v = mch_resolve_shortcut(p);
16256 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 }
16261#else
16262# ifdef HAVE_READLINK
16263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 char_u *cpy;
16265 int len;
16266 char_u *remain = NULL;
16267 char_u *q;
16268 int is_relative_to_current = FALSE;
16269 int has_trailing_pathsep = FALSE;
16270 int limit = 100;
16271
16272 p = vim_strsave(p);
16273
16274 if (p[0] == '.' && (vim_ispathsep(p[1])
16275 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16276 is_relative_to_current = TRUE;
16277
16278 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016279 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016282 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
16285 q = getnextcomp(p);
16286 if (*q != NUL)
16287 {
16288 /* Separate the first path component in "p", and keep the
16289 * remainder (beginning with the path separator). */
16290 remain = vim_strsave(q - 1);
16291 q[-1] = NUL;
16292 }
16293
Bram Moolenaard9462e32011-04-11 21:35:11 +020016294 buf = alloc(MAXPATHL + 1);
16295 if (buf == NULL)
16296 goto fail;
16297
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 for (;;)
16299 {
16300 for (;;)
16301 {
16302 len = readlink((char *)p, (char *)buf, MAXPATHL);
16303 if (len <= 0)
16304 break;
16305 buf[len] = NUL;
16306
16307 if (limit-- == 0)
16308 {
16309 vim_free(p);
16310 vim_free(remain);
16311 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 goto fail;
16314 }
16315
16316 /* Ensure that the result will have a trailing path separator
16317 * if the argument has one. */
16318 if (remain == NULL && has_trailing_pathsep)
16319 add_pathsep(buf);
16320
16321 /* Separate the first path component in the link value and
16322 * concatenate the remainders. */
16323 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16324 if (*q != NUL)
16325 {
16326 if (remain == NULL)
16327 remain = vim_strsave(q - 1);
16328 else
16329 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016330 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 if (cpy != NULL)
16332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 vim_free(remain);
16334 remain = cpy;
16335 }
16336 }
16337 q[-1] = NUL;
16338 }
16339
16340 q = gettail(p);
16341 if (q > p && *q == NUL)
16342 {
16343 /* Ignore trailing path separator. */
16344 q[-1] = NUL;
16345 q = gettail(p);
16346 }
16347 if (q > p && !mch_isFullName(buf))
16348 {
16349 /* symlink is relative to directory of argument */
16350 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16351 if (cpy != NULL)
16352 {
16353 STRCPY(cpy, p);
16354 STRCPY(gettail(cpy), buf);
16355 vim_free(p);
16356 p = cpy;
16357 }
16358 }
16359 else
16360 {
16361 vim_free(p);
16362 p = vim_strsave(buf);
16363 }
16364 }
16365
16366 if (remain == NULL)
16367 break;
16368
16369 /* Append the first path component of "remain" to "p". */
16370 q = getnextcomp(remain + 1);
16371 len = q - remain - (*q != NUL);
16372 cpy = vim_strnsave(p, STRLEN(p) + len);
16373 if (cpy != NULL)
16374 {
16375 STRNCAT(cpy, remain, len);
16376 vim_free(p);
16377 p = cpy;
16378 }
16379 /* Shorten "remain". */
16380 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016381 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 else
16383 {
16384 vim_free(remain);
16385 remain = NULL;
16386 }
16387 }
16388
16389 /* If the result is a relative path name, make it explicitly relative to
16390 * the current directory if and only if the argument had this form. */
16391 if (!vim_ispathsep(*p))
16392 {
16393 if (is_relative_to_current
16394 && *p != NUL
16395 && !(p[0] == '.'
16396 && (p[1] == NUL
16397 || vim_ispathsep(p[1])
16398 || (p[1] == '.'
16399 && (p[2] == NUL
16400 || vim_ispathsep(p[2]))))))
16401 {
16402 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016403 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 if (cpy != NULL)
16405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 vim_free(p);
16407 p = cpy;
16408 }
16409 }
16410 else if (!is_relative_to_current)
16411 {
16412 /* Strip leading "./". */
16413 q = p;
16414 while (q[0] == '.' && vim_ispathsep(q[1]))
16415 q += 2;
16416 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016417 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 }
16419 }
16420
16421 /* Ensure that the result will have no trailing path separator
16422 * if the argument had none. But keep "/" or "//". */
16423 if (!has_trailing_pathsep)
16424 {
16425 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016426 if (after_pathsep(p, q))
16427 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 }
16429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016430 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 }
16432# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016433 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434# endif
16435#endif
16436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438
16439#ifdef HAVE_READLINK
16440fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016441 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444}
16445
16446/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 */
16449 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
Bram Moolenaar33570922005-01-25 22:26:29 +000016454 list_T *l;
16455 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 if (argvars[0].v_type != VAR_LIST)
16458 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016459 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016460 && !tv_check_lock(l->lv_lock,
16461 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 {
16463 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016464 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016465 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 while (li != NULL)
16467 {
16468 ni = li->li_prev;
16469 list_append(l, li);
16470 li = ni;
16471 }
16472 rettv->vval.v_list = l;
16473 rettv->v_type = VAR_LIST;
16474 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016475 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477}
16478
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016479#define SP_NOMOVE 0x01 /* don't move cursor */
16480#define SP_REPEAT 0x02 /* repeat to find outer pair */
16481#define SP_RETCOUNT 0x04 /* return matchcount */
16482#define SP_SETPCMARK 0x08 /* set previous context mark */
16483#define SP_START 0x10 /* accept match at start position */
16484#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16485#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016486#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016487
Bram Moolenaar33570922005-01-25 22:26:29 +000016488static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489
16490/*
16491 * Get flags for a search function.
16492 * Possibly sets "p_ws".
16493 * Returns BACKWARD, FORWARD or zero (for an error).
16494 */
16495 static int
16496get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016497 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 int *flagsp;
16499{
16500 int dir = FORWARD;
16501 char_u *flags;
16502 char_u nbuf[NUMBUFLEN];
16503 int mask;
16504
16505 if (varp->v_type != VAR_UNKNOWN)
16506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 flags = get_tv_string_buf_chk(varp, nbuf);
16508 if (flags == NULL)
16509 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 while (*flags != NUL)
16511 {
16512 switch (*flags)
16513 {
16514 case 'b': dir = BACKWARD; break;
16515 case 'w': p_ws = TRUE; break;
16516 case 'W': p_ws = FALSE; break;
16517 default: mask = 0;
16518 if (flagsp != NULL)
16519 switch (*flags)
16520 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016521 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016522 case 'e': mask = SP_END; break;
16523 case 'm': mask = SP_RETCOUNT; break;
16524 case 'n': mask = SP_NOMOVE; break;
16525 case 'p': mask = SP_SUBPAT; break;
16526 case 'r': mask = SP_REPEAT; break;
16527 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016528 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 }
16530 if (mask == 0)
16531 {
16532 EMSG2(_(e_invarg2), flags);
16533 dir = 0;
16534 }
16535 else
16536 *flagsp |= mask;
16537 }
16538 if (dir == 0)
16539 break;
16540 ++flags;
16541 }
16542 }
16543 return dir;
16544}
16545
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016547 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016550search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016552 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016553 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016555 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 char_u *pat;
16557 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016558 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 int save_p_ws = p_ws;
16560 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016561 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016562 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016563 proftime_T tm;
16564#ifdef FEAT_RELTIME
16565 long time_limit = 0;
16566#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016567 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016568 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016571 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016572 if (dir == 0)
16573 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016574 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016575 if (flags & SP_START)
16576 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016577 if (flags & SP_END)
16578 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016579 if (flags & SP_COLUMN)
16580 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016581
Bram Moolenaar76929292008-01-06 19:07:36 +000016582 /* Optional arguments: line number to stop searching and timeout. */
16583 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016584 {
16585 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16586 if (lnum_stop < 0)
16587 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016588#ifdef FEAT_RELTIME
16589 if (argvars[3].v_type != VAR_UNKNOWN)
16590 {
16591 time_limit = get_tv_number_chk(&argvars[3], NULL);
16592 if (time_limit < 0)
16593 goto theend;
16594 }
16595#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016596 }
16597
Bram Moolenaar76929292008-01-06 19:07:36 +000016598#ifdef FEAT_RELTIME
16599 /* Set the time limit, if there is one. */
16600 profile_setlimit(time_limit, &tm);
16601#endif
16602
Bram Moolenaar231334e2005-07-25 20:46:57 +000016603 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016604 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016605 * Check to make sure only those flags are set.
16606 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16607 * flags cannot be set. Check for that condition also.
16608 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016609 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016610 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016613 goto theend;
16614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016616 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016617 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016618 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016619 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016621 if (flags & SP_SUBPAT)
16622 retval = subpatnum;
16623 else
16624 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016625 if (flags & SP_SETPCMARK)
16626 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016628 if (match_pos != NULL)
16629 {
16630 /* Store the match cursor position */
16631 match_pos->lnum = pos.lnum;
16632 match_pos->col = pos.col + 1;
16633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 /* "/$" will put the cursor after the end of the line, may need to
16635 * correct that here */
16636 check_cursor();
16637 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016638
16639 /* If 'n' flag is used: restore cursor position. */
16640 if (flags & SP_NOMOVE)
16641 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016642 else
16643 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016644theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016646
16647 return retval;
16648}
16649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016650#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016651
16652/*
16653 * round() is not in C90, use ceil() or floor() instead.
16654 */
16655 float_T
16656vim_round(f)
16657 float_T f;
16658{
16659 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16660}
16661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016662/*
16663 * "round({float})" function
16664 */
16665 static void
16666f_round(argvars, rettv)
16667 typval_T *argvars;
16668 typval_T *rettv;
16669{
16670 float_T f;
16671
16672 rettv->v_type = VAR_FLOAT;
16673 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016674 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016675 else
16676 rettv->vval.v_float = 0.0;
16677}
16678#endif
16679
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016680/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016681 * "screenattr()" function
16682 */
16683 static void
16684f_screenattr(argvars, rettv)
16685 typval_T *argvars UNUSED;
16686 typval_T *rettv;
16687{
16688 int row;
16689 int col;
16690 int c;
16691
16692 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16693 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16694 if (row < 0 || row >= screen_Rows
16695 || col < 0 || col >= screen_Columns)
16696 c = -1;
16697 else
16698 c = ScreenAttrs[LineOffset[row] + col];
16699 rettv->vval.v_number = c;
16700}
16701
16702/*
16703 * "screenchar()" function
16704 */
16705 static void
16706f_screenchar(argvars, rettv)
16707 typval_T *argvars UNUSED;
16708 typval_T *rettv;
16709{
16710 int row;
16711 int col;
16712 int off;
16713 int c;
16714
16715 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16716 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16717 if (row < 0 || row >= screen_Rows
16718 || col < 0 || col >= screen_Columns)
16719 c = -1;
16720 else
16721 {
16722 off = LineOffset[row] + col;
16723#ifdef FEAT_MBYTE
16724 if (enc_utf8 && ScreenLinesUC[off] != 0)
16725 c = ScreenLinesUC[off];
16726 else
16727#endif
16728 c = ScreenLines[off];
16729 }
16730 rettv->vval.v_number = c;
16731}
16732
16733/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016734 * "screencol()" function
16735 *
16736 * First column is 1 to be consistent with virtcol().
16737 */
16738 static void
16739f_screencol(argvars, rettv)
16740 typval_T *argvars UNUSED;
16741 typval_T *rettv;
16742{
16743 rettv->vval.v_number = screen_screencol() + 1;
16744}
16745
16746/*
16747 * "screenrow()" function
16748 */
16749 static void
16750f_screenrow(argvars, rettv)
16751 typval_T *argvars UNUSED;
16752 typval_T *rettv;
16753{
16754 rettv->vval.v_number = screen_screenrow() + 1;
16755}
16756
16757/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016758 * "search()" function
16759 */
16760 static void
16761f_search(argvars, rettv)
16762 typval_T *argvars;
16763 typval_T *rettv;
16764{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016765 int flags = 0;
16766
16767 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768}
16769
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016771 * "searchdecl()" function
16772 */
16773 static void
16774f_searchdecl(argvars, rettv)
16775 typval_T *argvars;
16776 typval_T *rettv;
16777{
16778 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016779 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016780 int error = FALSE;
16781 char_u *name;
16782
16783 rettv->vval.v_number = 1; /* default: FAIL */
16784
16785 name = get_tv_string_chk(&argvars[0]);
16786 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016787 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016788 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016789 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16790 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16791 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016792 if (!error && name != NULL)
16793 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016794 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016795}
16796
16797/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016798 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016800 static int
16801searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016803 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804{
16805 char_u *spat, *mpat, *epat;
16806 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 int dir;
16809 int flags = 0;
16810 char_u nbuf1[NUMBUFLEN];
16811 char_u nbuf2[NUMBUFLEN];
16812 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016813 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016814 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016815 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 spat = get_tv_string_chk(&argvars[0]);
16819 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16820 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16821 if (spat == NULL || mpat == NULL || epat == NULL)
16822 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 /* Handle the optional fourth argument: flags */
16825 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016826 if (dir == 0)
16827 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016828
16829 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016830 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16831 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016832 if ((flags & (SP_END | SP_SUBPAT)) != 0
16833 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016834 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016835 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016836 goto theend;
16837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016839 /* Using 'r' implies 'W', otherwise it doesn't work. */
16840 if (flags & SP_REPEAT)
16841 p_ws = FALSE;
16842
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016844 if (argvars[3].v_type == VAR_UNKNOWN
16845 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 skip = (char_u *)"";
16847 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016850 if (argvars[5].v_type != VAR_UNKNOWN)
16851 {
16852 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16853 if (lnum_stop < 0)
16854 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016855#ifdef FEAT_RELTIME
16856 if (argvars[6].v_type != VAR_UNKNOWN)
16857 {
16858 time_limit = get_tv_number_chk(&argvars[6], NULL);
16859 if (time_limit < 0)
16860 goto theend;
16861 }
16862#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016863 }
16864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 if (skip == NULL)
16866 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016868 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016869 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016870
16871theend:
16872 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016873
16874 return retval;
16875}
16876
16877/*
16878 * "searchpair()" function
16879 */
16880 static void
16881f_searchpair(argvars, rettv)
16882 typval_T *argvars;
16883 typval_T *rettv;
16884{
16885 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16886}
16887
16888/*
16889 * "searchpairpos()" function
16890 */
16891 static void
16892f_searchpairpos(argvars, rettv)
16893 typval_T *argvars;
16894 typval_T *rettv;
16895{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016896 pos_T match_pos;
16897 int lnum = 0;
16898 int col = 0;
16899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016900 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016901 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016902
16903 if (searchpair_cmn(argvars, &match_pos) > 0)
16904 {
16905 lnum = match_pos.lnum;
16906 col = match_pos.col;
16907 }
16908
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016909 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16910 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016911}
16912
16913/*
16914 * Search for a start/middle/end thing.
16915 * Used by searchpair(), see its documentation for the details.
16916 * Returns 0 or -1 for no match,
16917 */
16918 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016919do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16920 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016921 char_u *spat; /* start pattern */
16922 char_u *mpat; /* middle pattern */
16923 char_u *epat; /* end pattern */
16924 int dir; /* BACKWARD or FORWARD */
16925 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016926 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016927 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016928 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016929 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016930{
16931 char_u *save_cpo;
16932 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16933 long retval = 0;
16934 pos_T pos;
16935 pos_T firstpos;
16936 pos_T foundpos;
16937 pos_T save_cursor;
16938 pos_T save_pos;
16939 int n;
16940 int r;
16941 int nest = 1;
16942 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016943 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016944 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016945
16946 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16947 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016948 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016949
Bram Moolenaar76929292008-01-06 19:07:36 +000016950#ifdef FEAT_RELTIME
16951 /* Set the time limit, if there is one. */
16952 profile_setlimit(time_limit, &tm);
16953#endif
16954
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016955 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16956 * start/middle/end (pat3, for the top pair). */
16957 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16958 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16959 if (pat2 == NULL || pat3 == NULL)
16960 goto theend;
16961 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16962 if (*mpat == NUL)
16963 STRCPY(pat3, pat2);
16964 else
16965 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16966 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016967 if (flags & SP_START)
16968 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016969
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 save_cursor = curwin->w_cursor;
16971 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016972 clearpos(&firstpos);
16973 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 pat = pat3;
16975 for (;;)
16976 {
16977 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016978 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16980 /* didn't find it or found the first match again: FAIL */
16981 break;
16982
16983 if (firstpos.lnum == 0)
16984 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016985 if (equalpos(pos, foundpos))
16986 {
16987 /* Found the same position again. Can happen with a pattern that
16988 * has "\zs" at the end and searching backwards. Advance one
16989 * character and try again. */
16990 if (dir == BACKWARD)
16991 decl(&pos);
16992 else
16993 incl(&pos);
16994 }
16995 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016997 /* clear the start flag to avoid getting stuck here */
16998 options &= ~SEARCH_START;
16999
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 /* If the skip pattern matches, ignore this match. */
17001 if (*skip != NUL)
17002 {
17003 save_pos = curwin->w_cursor;
17004 curwin->w_cursor = pos;
17005 r = eval_to_bool(skip, &err, NULL, FALSE);
17006 curwin->w_cursor = save_pos;
17007 if (err)
17008 {
17009 /* Evaluating {skip} caused an error, break here. */
17010 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017011 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 break;
17013 }
17014 if (r)
17015 continue;
17016 }
17017
17018 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17019 {
17020 /* Found end when searching backwards or start when searching
17021 * forward: nested pair. */
17022 ++nest;
17023 pat = pat2; /* nested, don't search for middle */
17024 }
17025 else
17026 {
17027 /* Found end when searching forward or start when searching
17028 * backward: end of (nested) pair; or found middle in outer pair. */
17029 if (--nest == 1)
17030 pat = pat3; /* outer level, search for middle */
17031 }
17032
17033 if (nest == 0)
17034 {
17035 /* Found the match: return matchcount or line number. */
17036 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017037 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017039 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017040 if (flags & SP_SETPCMARK)
17041 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 curwin->w_cursor = pos;
17043 if (!(flags & SP_REPEAT))
17044 break;
17045 nest = 1; /* search for next unmatched */
17046 }
17047 }
17048
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017049 if (match_pos != NULL)
17050 {
17051 /* Store the match cursor position */
17052 match_pos->lnum = curwin->w_cursor.lnum;
17053 match_pos->col = curwin->w_cursor.col + 1;
17054 }
17055
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017057 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 curwin->w_cursor = save_cursor;
17059
17060theend:
17061 vim_free(pat2);
17062 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017063 if (p_cpo == empty_option)
17064 p_cpo = save_cpo;
17065 else
17066 /* Darn, evaluating the {skip} expression changed the value. */
17067 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017068
17069 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070}
17071
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017072/*
17073 * "searchpos()" function
17074 */
17075 static void
17076f_searchpos(argvars, rettv)
17077 typval_T *argvars;
17078 typval_T *rettv;
17079{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017080 pos_T match_pos;
17081 int lnum = 0;
17082 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017083 int n;
17084 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017086 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017087 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017089 n = search_cmn(argvars, &match_pos, &flags);
17090 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017091 {
17092 lnum = match_pos.lnum;
17093 col = match_pos.col;
17094 }
17095
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017096 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17097 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017098 if (flags & SP_SUBPAT)
17099 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017100}
17101
17102
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103 static void
17104f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017105 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108#ifdef FEAT_CLIENTSERVER
17109 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 char_u *server = get_tv_string_chk(&argvars[0]);
17111 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017114 if (server == NULL || reply == NULL)
17115 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 if (check_restricted() || check_secure())
17117 return;
17118# ifdef FEAT_X11
17119 if (check_connection() == FAIL)
17120 return;
17121# endif
17122
17123 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 EMSG(_("E258: Unable to send to client"));
17126 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 rettv->vval.v_number = 0;
17129#else
17130 rettv->vval.v_number = -1;
17131#endif
17132}
17133
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134 static void
17135f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017137 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138{
17139 char_u *r = NULL;
17140
17141#ifdef FEAT_CLIENTSERVER
17142# ifdef WIN32
17143 r = serverGetVimNames();
17144# else
17145 make_connection();
17146 if (X_DISPLAY != NULL)
17147 r = serverGetVimNames(X_DISPLAY);
17148# endif
17149#endif
17150 rettv->v_type = VAR_STRING;
17151 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152}
17153
17154/*
17155 * "setbufvar()" function
17156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017158f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017160 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161{
17162 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017165 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 char_u nbuf[NUMBUFLEN];
17167
17168 if (check_restricted() || check_secure())
17169 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17171 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017172 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 varp = &argvars[2];
17174
17175 if (buf != NULL && varname != NULL && varp != NULL)
17176 {
17177 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
17180 if (*varname == '&')
17181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 long numval;
17183 char_u *strval;
17184 int error = FALSE;
17185
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 numval = get_tv_number_chk(varp, &error);
17188 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017189 if (!error && strval != NULL)
17190 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 }
17192 else
17193 {
17194 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17195 if (bufvarname != NULL)
17196 {
17197 STRCPY(bufvarname, "b:");
17198 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017199 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 vim_free(bufvarname);
17201 }
17202 }
17203
17204 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207}
17208
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017209 static void
17210f_setcharsearch(argvars, rettv)
17211 typval_T *argvars;
17212 typval_T *rettv UNUSED;
17213{
17214 dict_T *d;
17215 dictitem_T *di;
17216 char_u *csearch;
17217
17218 if (argvars[0].v_type != VAR_DICT)
17219 {
17220 EMSG(_(e_dictreq));
17221 return;
17222 }
17223
17224 if ((d = argvars[0].vval.v_dict) != NULL)
17225 {
17226 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17227 if (csearch != NULL)
17228 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017229#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017230 if (enc_utf8)
17231 {
17232 int pcc[MAX_MCO];
17233 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017234
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017235 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17236 }
17237 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017238#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017239 set_last_csearch(PTR2CHAR(csearch),
17240 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017241 }
17242
17243 di = dict_find(d, (char_u *)"forward", -1);
17244 if (di != NULL)
17245 set_csearch_direction(get_tv_number(&di->di_tv)
17246 ? FORWARD : BACKWARD);
17247
17248 di = dict_find(d, (char_u *)"until", -1);
17249 if (di != NULL)
17250 set_csearch_until(!!get_tv_number(&di->di_tv));
17251 }
17252}
17253
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254/*
17255 * "setcmdpos()" function
17256 */
17257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017258f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 typval_T *argvars;
17260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017262 int pos = (int)get_tv_number(&argvars[0]) - 1;
17263
17264 if (pos >= 0)
17265 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266}
17267
17268/*
17269 * "setline()" function
17270 */
17271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017273 typval_T *argvars;
17274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275{
17276 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017277 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017278 list_T *l = NULL;
17279 listitem_T *li = NULL;
17280 long added = 0;
17281 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017283 lnum = get_tv_lnum(&argvars[0]);
17284 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017286 l = argvars[1].vval.v_list;
17287 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017289 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017291
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017292 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017293 for (;;)
17294 {
17295 if (l != NULL)
17296 {
17297 /* list argument, get next string */
17298 if (li == NULL)
17299 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017301 li = li->li_next;
17302 }
17303
17304 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017305 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017306 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017307
17308 /* When coming here from Insert mode, sync undo, so that this can be
17309 * undone separately from what was previously inserted. */
17310 if (u_sync_once == 2)
17311 {
17312 u_sync_once = 1; /* notify that u_sync() was called */
17313 u_sync(TRUE);
17314 }
17315
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017316 if (lnum <= curbuf->b_ml.ml_line_count)
17317 {
17318 /* existing line, replace it */
17319 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17320 {
17321 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017322 if (lnum == curwin->w_cursor.lnum)
17323 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017324 rettv->vval.v_number = 0; /* OK */
17325 }
17326 }
17327 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17328 {
17329 /* lnum is one past the last line, append the line */
17330 ++added;
17331 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17332 rettv->vval.v_number = 0; /* OK */
17333 }
17334
17335 if (l == NULL) /* only one string argument */
17336 break;
17337 ++lnum;
17338 }
17339
17340 if (added > 0)
17341 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342}
17343
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017344static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17345
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017347 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017348 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017349 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017350set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017351 win_T *wp UNUSED;
17352 typval_T *list_arg UNUSED;
17353 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017354 typval_T *rettv;
17355{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017356#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017357 char_u *act;
17358 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017359#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017360
Bram Moolenaar2641f772005-03-25 21:58:17 +000017361 rettv->vval.v_number = -1;
17362
17363#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017364 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017365 EMSG(_(e_listreq));
17366 else
17367 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017368 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017369
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017370 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017371 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017372 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 if (act == NULL)
17374 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017375 if (*act == 'a' || *act == 'r')
17376 action = *act;
17377 }
17378
Bram Moolenaar81484f42012-12-05 15:16:47 +010017379 if (l != NULL && set_errorlist(wp, l, action,
17380 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017381 rettv->vval.v_number = 0;
17382 }
17383#endif
17384}
17385
17386/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017387 * "setloclist()" function
17388 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017389 static void
17390f_setloclist(argvars, rettv)
17391 typval_T *argvars;
17392 typval_T *rettv;
17393{
17394 win_T *win;
17395
17396 rettv->vval.v_number = -1;
17397
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017398 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017399 if (win != NULL)
17400 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17401}
17402
17403/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017404 * "setmatches()" function
17405 */
17406 static void
17407f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017408 typval_T *argvars UNUSED;
17409 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017410{
17411#ifdef FEAT_SEARCH_EXTRA
17412 list_T *l;
17413 listitem_T *li;
17414 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017415 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017416
17417 rettv->vval.v_number = -1;
17418 if (argvars[0].v_type != VAR_LIST)
17419 {
17420 EMSG(_(e_listreq));
17421 return;
17422 }
17423 if ((l = argvars[0].vval.v_list) != NULL)
17424 {
17425
17426 /* To some extent make sure that we are dealing with a list from
17427 * "getmatches()". */
17428 li = l->lv_first;
17429 while (li != NULL)
17430 {
17431 if (li->li_tv.v_type != VAR_DICT
17432 || (d = li->li_tv.vval.v_dict) == NULL)
17433 {
17434 EMSG(_(e_invarg));
17435 return;
17436 }
17437 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017438 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17439 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017440 && dict_find(d, (char_u *)"priority", -1) != NULL
17441 && dict_find(d, (char_u *)"id", -1) != NULL))
17442 {
17443 EMSG(_(e_invarg));
17444 return;
17445 }
17446 li = li->li_next;
17447 }
17448
17449 clear_matches(curwin);
17450 li = l->lv_first;
17451 while (li != NULL)
17452 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017453 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017454 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017455 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017456 char_u *group;
17457 int priority;
17458 int id;
17459 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017460
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017461 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017462 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17463 {
17464 if (s == NULL)
17465 {
17466 s = list_alloc();
17467 if (s == NULL)
17468 return;
17469 }
17470
17471 /* match from matchaddpos() */
17472 for (i = 1; i < 9; i++)
17473 {
17474 sprintf((char *)buf, (char *)"pos%d", i);
17475 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17476 {
17477 if (di->di_tv.v_type != VAR_LIST)
17478 return;
17479
17480 list_append_tv(s, &di->di_tv);
17481 s->lv_refcount++;
17482 }
17483 else
17484 break;
17485 }
17486 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017487
17488 group = get_dict_string(d, (char_u *)"group", FALSE);
17489 priority = (int)get_dict_number(d, (char_u *)"priority");
17490 id = (int)get_dict_number(d, (char_u *)"id");
17491 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17492 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17493 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017494 if (i == 0)
17495 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017496 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017497 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017498 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017499 }
17500 else
17501 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017502 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017503 list_unref(s);
17504 s = NULL;
17505 }
17506
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017507 li = li->li_next;
17508 }
17509 rettv->vval.v_number = 0;
17510 }
17511#endif
17512}
17513
17514/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017515 * "setpos()" function
17516 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017517 static void
17518f_setpos(argvars, rettv)
17519 typval_T *argvars;
17520 typval_T *rettv;
17521{
17522 pos_T pos;
17523 int fnum;
17524 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017525 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017526
Bram Moolenaar08250432008-02-13 11:42:46 +000017527 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017528 name = get_tv_string_chk(argvars);
17529 if (name != NULL)
17530 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017531 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017532 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017533 if (--pos.col < 0)
17534 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017535 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017536 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017537 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017538 if (fnum == curbuf->b_fnum)
17539 {
17540 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017541 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017542 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017543 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017544 curwin->w_set_curswant = FALSE;
17545 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017546 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017547 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017548 }
17549 else
17550 EMSG(_(e_invarg));
17551 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017552 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17553 {
17554 /* set mark */
17555 if (setmark_pos(name[1], &pos, fnum) == OK)
17556 rettv->vval.v_number = 0;
17557 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017558 else
17559 EMSG(_(e_invarg));
17560 }
17561 }
17562}
17563
17564/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017565 * "setqflist()" function
17566 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017567 static void
17568f_setqflist(argvars, rettv)
17569 typval_T *argvars;
17570 typval_T *rettv;
17571{
17572 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17573}
17574
17575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 * "setreg()" function
17577 */
17578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
17583 int regname;
17584 char_u *strregname;
17585 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017586 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 int append;
17588 char_u yank_type;
17589 long block_len;
17590
17591 block_len = -1;
17592 yank_type = MAUTO;
17593 append = FALSE;
17594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017598 if (strregname == NULL)
17599 return; /* type error; errmsg already given */
17600 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 if (regname == 0 || regname == '@')
17602 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017606 stropt = get_tv_string_chk(&argvars[2]);
17607 if (stropt == NULL)
17608 return; /* type error */
17609 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 switch (*stropt)
17611 {
17612 case 'a': case 'A': /* append */
17613 append = TRUE;
17614 break;
17615 case 'v': case 'c': /* character-wise selection */
17616 yank_type = MCHAR;
17617 break;
17618 case 'V': case 'l': /* line-wise selection */
17619 yank_type = MLINE;
17620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 case 'b': case Ctrl_V: /* block-wise selection */
17622 yank_type = MBLOCK;
17623 if (VIM_ISDIGIT(stropt[1]))
17624 {
17625 ++stropt;
17626 block_len = getdigits(&stropt) - 1;
17627 --stropt;
17628 }
17629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 }
17631 }
17632
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017633 if (argvars[1].v_type == VAR_LIST)
17634 {
17635 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017636 char_u **allocval;
17637 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017638 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017639 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017640 int len = argvars[1].vval.v_list->lv_len;
17641 listitem_T *li;
17642
Bram Moolenaar7d647822014-04-05 21:28:56 +020017643 /* First half: use for pointers to result lines; second half: use for
17644 * pointers to allocated copies. */
17645 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017646 if (lstval == NULL)
17647 return;
17648 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017649 allocval = lstval + len + 2;
17650 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017651
17652 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17653 li = li->li_next)
17654 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017655 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017656 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017657 goto free_lstval;
17658 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017659 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017660 /* Need to make a copy, next get_tv_string_buf_chk() will
17661 * overwrite the string. */
17662 strval = vim_strsave(buf);
17663 if (strval == NULL)
17664 goto free_lstval;
17665 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017666 }
17667 *curval++ = strval;
17668 }
17669 *curval++ = NULL;
17670
17671 write_reg_contents_lst(regname, lstval, -1,
17672 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017673free_lstval:
17674 while (curallocval > allocval)
17675 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017676 vim_free(lstval);
17677 }
17678 else
17679 {
17680 strval = get_tv_string_chk(&argvars[1]);
17681 if (strval == NULL)
17682 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017686 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687}
17688
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017689/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017690 * "settabvar()" function
17691 */
17692 static void
17693f_settabvar(argvars, rettv)
17694 typval_T *argvars;
17695 typval_T *rettv;
17696{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017697#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017698 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017699 tabpage_T *tp;
17700#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017701 char_u *varname, *tabvarname;
17702 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017703
17704 rettv->vval.v_number = 0;
17705
17706 if (check_restricted() || check_secure())
17707 return;
17708
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017709#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017710 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017711#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017712 varname = get_tv_string_chk(&argvars[1]);
17713 varp = &argvars[2];
17714
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017715 if (varname != NULL && varp != NULL
17716#ifdef FEAT_WINDOWS
17717 && tp != NULL
17718#endif
17719 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017720 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017721#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017722 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017723 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017724#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017725
17726 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17727 if (tabvarname != NULL)
17728 {
17729 STRCPY(tabvarname, "t:");
17730 STRCPY(tabvarname + 2, varname);
17731 set_var(tabvarname, varp, TRUE);
17732 vim_free(tabvarname);
17733 }
17734
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017735#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017736 /* Restore current tabpage */
17737 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017738 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017739#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017740 }
17741}
17742
17743/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017744 * "settabwinvar()" function
17745 */
17746 static void
17747f_settabwinvar(argvars, rettv)
17748 typval_T *argvars;
17749 typval_T *rettv;
17750{
17751 setwinvar(argvars, rettv, 1);
17752}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753
17754/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017755 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017762 setwinvar(argvars, rettv, 0);
17763}
17764
17765/*
17766 * "setwinvar()" and "settabwinvar()" functions
17767 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017768
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017769 static void
17770setwinvar(argvars, rettv, off)
17771 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017772 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017773 int off;
17774{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 win_T *win;
17776#ifdef FEAT_WINDOWS
17777 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017778 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017779 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780#endif
17781 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017784 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785
17786 if (check_restricted() || check_secure())
17787 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017788
17789#ifdef FEAT_WINDOWS
17790 if (off == 1)
17791 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17792 else
17793 tp = curtab;
17794#endif
17795 win = find_win_by_nr(&argvars[off], tp);
17796 varname = get_tv_string_chk(&argvars[off + 1]);
17797 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798
17799 if (win != NULL && varname != NULL && varp != NULL)
17800 {
17801#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017802 need_switch_win = !(tp == curtab && win == curwin);
17803 if (!need_switch_win
17804 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017807 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017809 long numval;
17810 char_u *strval;
17811 int error = FALSE;
17812
17813 ++varname;
17814 numval = get_tv_number_chk(varp, &error);
17815 strval = get_tv_string_buf_chk(varp, nbuf);
17816 if (!error && strval != NULL)
17817 set_option_value(varname, numval, strval, OPT_LOCAL);
17818 }
17819 else
17820 {
17821 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17822 if (winvarname != NULL)
17823 {
17824 STRCPY(winvarname, "w:");
17825 STRCPY(winvarname + 2, varname);
17826 set_var(winvarname, varp, TRUE);
17827 vim_free(winvarname);
17828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 }
17830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017832 if (need_switch_win)
17833 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834#endif
17835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836}
17837
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017838#ifdef FEAT_CRYPT
17839/*
17840 * "sha256({string})" function
17841 */
17842 static void
17843f_sha256(argvars, rettv)
17844 typval_T *argvars;
17845 typval_T *rettv;
17846{
17847 char_u *p;
17848
17849 p = get_tv_string(&argvars[0]);
17850 rettv->vval.v_string = vim_strsave(
17851 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17852 rettv->v_type = VAR_STRING;
17853}
17854#endif /* FEAT_CRYPT */
17855
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017857 * "shellescape({string})" function
17858 */
17859 static void
17860f_shellescape(argvars, rettv)
17861 typval_T *argvars;
17862 typval_T *rettv;
17863{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017864 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017865 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017866 rettv->v_type = VAR_STRING;
17867}
17868
17869/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017870 * shiftwidth() function
17871 */
17872 static void
17873f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017874 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017875 typval_T *rettv;
17876{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017877 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017878}
17879
17880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 */
17883 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017884f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017885 typval_T *argvars;
17886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017888 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889
Bram Moolenaar0d660222005-01-07 21:51:51 +000017890 p = get_tv_string(&argvars[0]);
17891 rettv->vval.v_string = vim_strsave(p);
17892 simplify_filename(rettv->vval.v_string); /* simplify in place */
17893 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894}
17895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017896#ifdef FEAT_FLOAT
17897/*
17898 * "sin()" function
17899 */
17900 static void
17901f_sin(argvars, rettv)
17902 typval_T *argvars;
17903 typval_T *rettv;
17904{
17905 float_T f;
17906
17907 rettv->v_type = VAR_FLOAT;
17908 if (get_float_arg(argvars, &f) == OK)
17909 rettv->vval.v_float = sin(f);
17910 else
17911 rettv->vval.v_float = 0.0;
17912}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017913
17914/*
17915 * "sinh()" function
17916 */
17917 static void
17918f_sinh(argvars, rettv)
17919 typval_T *argvars;
17920 typval_T *rettv;
17921{
17922 float_T f;
17923
17924 rettv->v_type = VAR_FLOAT;
17925 if (get_float_arg(argvars, &f) == OK)
17926 rettv->vval.v_float = sinh(f);
17927 else
17928 rettv->vval.v_float = 0.0;
17929}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017930#endif
17931
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932static int
17933#ifdef __BORLANDC__
17934 _RTLENTRYF
17935#endif
17936 item_compare __ARGS((const void *s1, const void *s2));
17937static int
17938#ifdef __BORLANDC__
17939 _RTLENTRYF
17940#endif
17941 item_compare2 __ARGS((const void *s1, const void *s2));
17942
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017943/* struct used in the array that's given to qsort() */
17944typedef struct
17945{
17946 listitem_T *item;
17947 int idx;
17948} sortItem_T;
17949
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017951static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017952static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017954static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017955static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017956static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017957static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017958#define ITEM_COMPARE_FAIL 999
17959
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017961 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 static int
17964#ifdef __BORLANDC__
17965_RTLENTRYF
17966#endif
17967item_compare(s1, s2)
17968 const void *s1;
17969 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017971 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017972 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017974 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017975 int res;
17976 char_u numbuf1[NUMBUFLEN];
17977 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017979 si1 = (sortItem_T *)s1;
17980 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017981 tv1 = &si1->item->li_tv;
17982 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017983
17984 if (item_compare_numbers)
17985 {
17986 long v1 = get_tv_number(tv1);
17987 long v2 = get_tv_number(tv2);
17988
17989 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17990 }
17991
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017992 /* tv2string() puts quotes around a string and allocates memory. Don't do
17993 * that for string variables. Use a single quote when comparing with a
17994 * non-string to do what the docs promise. */
17995 if (tv1->v_type == VAR_STRING)
17996 {
17997 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17998 p1 = (char_u *)"'";
17999 else
18000 p1 = tv1->vval.v_string;
18001 }
18002 else
18003 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18004 if (tv2->v_type == VAR_STRING)
18005 {
18006 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18007 p2 = (char_u *)"'";
18008 else
18009 p2 = tv2->vval.v_string;
18010 }
18011 else
18012 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018013 if (p1 == NULL)
18014 p1 = (char_u *)"";
18015 if (p2 == NULL)
18016 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018017 if (!item_compare_numeric)
18018 {
18019 if (item_compare_ic)
18020 res = STRICMP(p1, p2);
18021 else
18022 res = STRCMP(p1, p2);
18023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018025 {
18026 double n1, n2;
18027 n1 = strtod((char *)p1, (char **)&p1);
18028 n2 = strtod((char *)p2, (char **)&p2);
18029 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18030 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018031
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018032 /* When the result would be zero, compare the item indexes. Makes the
18033 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018034 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018035 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018036
Bram Moolenaar0d660222005-01-07 21:51:51 +000018037 vim_free(tofree1);
18038 vim_free(tofree2);
18039 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
18042 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018043#ifdef __BORLANDC__
18044_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046item_compare2(s1, s2)
18047 const void *s1;
18048 const void *s2;
18049{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018050 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018052 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018053 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018056 /* shortcut after failure in previous call; compare all items equal */
18057 if (item_compare_func_err)
18058 return 0;
18059
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018060 si1 = (sortItem_T *)s1;
18061 si2 = (sortItem_T *)s2;
18062
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018063 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018064 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018065 copy_tv(&si1->item->li_tv, &argv[0]);
18066 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067
18068 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018069 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018070 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18071 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 clear_tv(&argv[0]);
18073 clear_tv(&argv[1]);
18074
18075 if (res == FAIL)
18076 res = ITEM_COMPARE_FAIL;
18077 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18079 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018080 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018082
18083 /* When the result would be zero, compare the pointers themselves. Makes
18084 * the sort stable. */
18085 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018086 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018087
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 return res;
18089}
18090
18091/*
18092 * "sort({list})" function
18093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018095do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018096 typval_T *argvars;
18097 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018098 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099{
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 list_T *l;
18101 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018102 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103 long len;
18104 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
Bram Moolenaar0d660222005-01-07 21:51:51 +000018106 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018107 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 else
18109 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018111 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018112 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18113 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114 return;
18115 rettv->vval.v_list = l;
18116 rettv->v_type = VAR_LIST;
18117 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118
Bram Moolenaar0d660222005-01-07 21:51:51 +000018119 len = list_len(l);
18120 if (len <= 1)
18121 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122
Bram Moolenaar0d660222005-01-07 21:51:51 +000018123 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018124 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018125 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018127 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018128 if (argvars[1].v_type != VAR_UNKNOWN)
18129 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018130 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018131 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018132 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018133 else
18134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018135 int error = FALSE;
18136
18137 i = get_tv_number_chk(&argvars[1], &error);
18138 if (error)
18139 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018140 if (i == 1)
18141 item_compare_ic = TRUE;
18142 else
18143 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018144 if (item_compare_func != NULL)
18145 {
18146 if (STRCMP(item_compare_func, "n") == 0)
18147 {
18148 item_compare_func = NULL;
18149 item_compare_numeric = TRUE;
18150 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018151 else if (STRCMP(item_compare_func, "N") == 0)
18152 {
18153 item_compare_func = NULL;
18154 item_compare_numbers = TRUE;
18155 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018156 else if (STRCMP(item_compare_func, "i") == 0)
18157 {
18158 item_compare_func = NULL;
18159 item_compare_ic = TRUE;
18160 }
18161 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018163
18164 if (argvars[2].v_type != VAR_UNKNOWN)
18165 {
18166 /* optional third argument: {dict} */
18167 if (argvars[2].v_type != VAR_DICT)
18168 {
18169 EMSG(_(e_dictreq));
18170 return;
18171 }
18172 item_compare_selfdict = argvars[2].vval.v_dict;
18173 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018177 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018178 if (ptrs == NULL)
18179 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180
Bram Moolenaar327aa022014-03-25 18:24:23 +010018181 i = 0;
18182 if (sort)
18183 {
18184 /* sort(): ptrs will be the list to sort */
18185 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018186 {
18187 ptrs[i].item = li;
18188 ptrs[i].idx = i;
18189 ++i;
18190 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018191
18192 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018193 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018194 /* test the compare function */
18195 if (item_compare_func != NULL
18196 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018197 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018198 EMSG(_("E702: Sort compare function failed"));
18199 else
18200 {
18201 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018202 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018203 item_compare_func == NULL ? item_compare : item_compare2);
18204
18205 if (!item_compare_func_err)
18206 {
18207 /* Clear the List and append the items in sorted order. */
18208 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18209 l->lv_len = 0;
18210 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018211 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018212 }
18213 }
18214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018216 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018217 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18218
18219 /* f_uniq(): ptrs will be a stack of items to remove */
18220 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018221 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018222 item_compare_func_ptr = item_compare_func
18223 ? item_compare2 : item_compare;
18224
18225 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18226 li = li->li_next)
18227 {
18228 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18229 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018230 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018231 if (item_compare_func_err)
18232 {
18233 EMSG(_("E882: Uniq compare function failed"));
18234 break;
18235 }
18236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018238 if (!item_compare_func_err)
18239 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018240 while (--i >= 0)
18241 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018242 li = ptrs[i].item->li_next;
18243 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018244 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018245 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018246 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018247 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018248 list_fix_watch(l, li);
18249 listitem_free(li);
18250 l->lv_len--;
18251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 }
18254
18255 vim_free(ptrs);
18256 }
18257}
18258
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018259/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018260 * "sort({list})" function
18261 */
18262 static void
18263f_sort(argvars, rettv)
18264 typval_T *argvars;
18265 typval_T *rettv;
18266{
18267 do_sort_uniq(argvars, rettv, TRUE);
18268}
18269
18270/*
18271 * "uniq({list})" function
18272 */
18273 static void
18274f_uniq(argvars, rettv)
18275 typval_T *argvars;
18276 typval_T *rettv;
18277{
18278 do_sort_uniq(argvars, rettv, FALSE);
18279}
18280
18281/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018282 * "soundfold({word})" function
18283 */
18284 static void
18285f_soundfold(argvars, rettv)
18286 typval_T *argvars;
18287 typval_T *rettv;
18288{
18289 char_u *s;
18290
18291 rettv->v_type = VAR_STRING;
18292 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018293#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018294 rettv->vval.v_string = eval_soundfold(s);
18295#else
18296 rettv->vval.v_string = vim_strsave(s);
18297#endif
18298}
18299
18300/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018301 * "spellbadword()" function
18302 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018303 static void
18304f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018305 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018306 typval_T *rettv;
18307{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018308 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018309 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018310 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018311
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018312 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018313 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018314
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018315#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018316 if (argvars[0].v_type == VAR_UNKNOWN)
18317 {
18318 /* Find the start and length of the badly spelled word. */
18319 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18320 if (len != 0)
18321 word = ml_get_cursor();
18322 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018323 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018324 {
18325 char_u *str = get_tv_string_chk(&argvars[0]);
18326 int capcol = -1;
18327
18328 if (str != NULL)
18329 {
18330 /* Check the argument for spelling. */
18331 while (*str != NUL)
18332 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018333 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018334 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018335 {
18336 word = str;
18337 break;
18338 }
18339 str += len;
18340 }
18341 }
18342 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018343#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018345 list_append_string(rettv->vval.v_list, word, len);
18346 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018347 attr == HLF_SPB ? "bad" :
18348 attr == HLF_SPR ? "rare" :
18349 attr == HLF_SPL ? "local" :
18350 attr == HLF_SPC ? "caps" :
18351 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018352}
18353
18354/*
18355 * "spellsuggest()" function
18356 */
18357 static void
18358f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018359 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018360 typval_T *rettv;
18361{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018362#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018363 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018364 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018365 int maxcount;
18366 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018367 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018368 listitem_T *li;
18369 int need_capital = FALSE;
18370#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018372 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018373 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018374
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018375#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018376 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018377 {
18378 str = get_tv_string(&argvars[0]);
18379 if (argvars[1].v_type != VAR_UNKNOWN)
18380 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018381 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018382 if (maxcount <= 0)
18383 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018384 if (argvars[2].v_type != VAR_UNKNOWN)
18385 {
18386 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18387 if (typeerr)
18388 return;
18389 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018390 }
18391 else
18392 maxcount = 25;
18393
Bram Moolenaar4770d092006-01-12 23:22:24 +000018394 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018395
18396 for (i = 0; i < ga.ga_len; ++i)
18397 {
18398 str = ((char_u **)ga.ga_data)[i];
18399
18400 li = listitem_alloc();
18401 if (li == NULL)
18402 vim_free(str);
18403 else
18404 {
18405 li->li_tv.v_type = VAR_STRING;
18406 li->li_tv.v_lock = 0;
18407 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018408 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018409 }
18410 }
18411 ga_clear(&ga);
18412 }
18413#endif
18414}
18415
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018417f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018418 typval_T *argvars;
18419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420{
18421 char_u *str;
18422 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018423 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018424 regmatch_T regmatch;
18425 char_u patbuf[NUMBUFLEN];
18426 char_u *save_cpo;
18427 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018428 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018429 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018430 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431
18432 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18433 save_cpo = p_cpo;
18434 p_cpo = (char_u *)"";
18435
18436 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018437 if (argvars[1].v_type != VAR_UNKNOWN)
18438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018439 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18440 if (pat == NULL)
18441 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018443 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018444 }
18445 if (pat == NULL || *pat == NUL)
18446 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 if (typeerr)
18451 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452
Bram Moolenaar0d660222005-01-07 21:51:51 +000018453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18454 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018457 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018459 if (*str == NUL)
18460 match = FALSE; /* empty item at the end */
18461 else
18462 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018463 if (match)
18464 end = regmatch.startp[0];
18465 else
18466 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018467 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18468 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018470 if (list_append_string(rettv->vval.v_list, str,
18471 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473 }
18474 if (!match)
18475 break;
18476 /* Advance to just after the match. */
18477 if (regmatch.endp[0] > str)
18478 col = 0;
18479 else
18480 {
18481 /* Don't get stuck at the same match. */
18482#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018483 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018484#else
18485 col = 1;
18486#endif
18487 }
18488 str = regmatch.endp[0];
18489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
Bram Moolenaar473de612013-06-08 18:19:48 +020018491 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495}
18496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018497#ifdef FEAT_FLOAT
18498/*
18499 * "sqrt()" function
18500 */
18501 static void
18502f_sqrt(argvars, rettv)
18503 typval_T *argvars;
18504 typval_T *rettv;
18505{
18506 float_T f;
18507
18508 rettv->v_type = VAR_FLOAT;
18509 if (get_float_arg(argvars, &f) == OK)
18510 rettv->vval.v_float = sqrt(f);
18511 else
18512 rettv->vval.v_float = 0.0;
18513}
18514
18515/*
18516 * "str2float()" function
18517 */
18518 static void
18519f_str2float(argvars, rettv)
18520 typval_T *argvars;
18521 typval_T *rettv;
18522{
18523 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18524
18525 if (*p == '+')
18526 p = skipwhite(p + 1);
18527 (void)string2float(p, &rettv->vval.v_float);
18528 rettv->v_type = VAR_FLOAT;
18529}
18530#endif
18531
Bram Moolenaar2c932302006-03-18 21:42:09 +000018532/*
18533 * "str2nr()" function
18534 */
18535 static void
18536f_str2nr(argvars, rettv)
18537 typval_T *argvars;
18538 typval_T *rettv;
18539{
18540 int base = 10;
18541 char_u *p;
18542 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018543 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018544
18545 if (argvars[1].v_type != VAR_UNKNOWN)
18546 {
18547 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018548 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018549 {
18550 EMSG(_(e_invarg));
18551 return;
18552 }
18553 }
18554
18555 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018556 if (*p == '+')
18557 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018558 switch (base)
18559 {
18560 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18561 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18562 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18563 default: what = 0;
18564 }
18565 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018566 rettv->vval.v_number = n;
18567}
18568
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569#ifdef HAVE_STRFTIME
18570/*
18571 * "strftime({format}[, {time}])" function
18572 */
18573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018575 typval_T *argvars;
18576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577{
18578 char_u result_buf[256];
18579 struct tm *curtime;
18580 time_t seconds;
18581 char_u *p;
18582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018586 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 seconds = time(NULL);
18588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 curtime = localtime(&seconds);
18591 /* MSVC returns NULL for an invalid value of seconds. */
18592 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018593 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 else
18595 {
18596# ifdef FEAT_MBYTE
18597 vimconv_T conv;
18598 char_u *enc;
18599
18600 conv.vc_type = CONV_NONE;
18601 enc = enc_locale();
18602 convert_setup(&conv, p_enc, enc);
18603 if (conv.vc_type != CONV_NONE)
18604 p = string_convert(&conv, p, NULL);
18605# endif
18606 if (p != NULL)
18607 (void)strftime((char *)result_buf, sizeof(result_buf),
18608 (char *)p, curtime);
18609 else
18610 result_buf[0] = NUL;
18611
18612# ifdef FEAT_MBYTE
18613 if (conv.vc_type != CONV_NONE)
18614 vim_free(p);
18615 convert_setup(&conv, enc, p_enc);
18616 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 else
18619# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621
18622# ifdef FEAT_MBYTE
18623 /* Release conversion descriptors */
18624 convert_setup(&conv, NULL, NULL);
18625 vim_free(enc);
18626# endif
18627 }
18628}
18629#endif
18630
18631/*
18632 * "stridx()" function
18633 */
18634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *argvars;
18637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 char_u buf[NUMBUFLEN];
18640 char_u *needle;
18641 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018644 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018646 needle = get_tv_string_chk(&argvars[1]);
18647 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018648 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 if (needle == NULL || haystack == NULL)
18650 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651
Bram Moolenaar33570922005-01-25 22:26:29 +000018652 if (argvars[2].v_type != VAR_UNKNOWN)
18653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018654 int error = FALSE;
18655
18656 start_idx = get_tv_number_chk(&argvars[2], &error);
18657 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018658 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018659 if (start_idx >= 0)
18660 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 }
18662
18663 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18664 if (pos != NULL)
18665 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666}
18667
18668/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018669 * "string()" function
18670 */
18671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018673 typval_T *argvars;
18674 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018675{
18676 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018677 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018680 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018681 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018682 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684}
18685
18686/*
18687 * "strlen()" function
18688 */
18689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018691 typval_T *argvars;
18692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->vval.v_number = (varnumber_T)(STRLEN(
18695 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696}
18697
18698/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018699 * "strchars()" function
18700 */
18701 static void
18702f_strchars(argvars, rettv)
18703 typval_T *argvars;
18704 typval_T *rettv;
18705{
18706 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018707 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018708#ifdef FEAT_MBYTE
18709 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018710 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018711#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018712
18713 if (argvars[1].v_type != VAR_UNKNOWN)
18714 skipcc = get_tv_number_chk(&argvars[1], NULL);
18715 if (skipcc < 0 || skipcc > 1)
18716 EMSG(_(e_invarg));
18717 else
18718 {
18719#ifdef FEAT_MBYTE
18720 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18721 while (*s != NUL)
18722 {
18723 func_mb_ptr2char_adv(&s);
18724 ++len;
18725 }
18726 rettv->vval.v_number = len;
18727#else
18728 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18729#endif
18730 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018731}
18732
18733/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018734 * "strdisplaywidth()" function
18735 */
18736 static void
18737f_strdisplaywidth(argvars, rettv)
18738 typval_T *argvars;
18739 typval_T *rettv;
18740{
18741 char_u *s = get_tv_string(&argvars[0]);
18742 int col = 0;
18743
18744 if (argvars[1].v_type != VAR_UNKNOWN)
18745 col = get_tv_number(&argvars[1]);
18746
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018747 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018748}
18749
18750/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018751 * "strwidth()" function
18752 */
18753 static void
18754f_strwidth(argvars, rettv)
18755 typval_T *argvars;
18756 typval_T *rettv;
18757{
18758 char_u *s = get_tv_string(&argvars[0]);
18759
18760 rettv->vval.v_number = (varnumber_T)(
18761#ifdef FEAT_MBYTE
18762 mb_string2cells(s, -1)
18763#else
18764 STRLEN(s)
18765#endif
18766 );
18767}
18768
18769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 * "strpart()" function
18771 */
18772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018773f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 typval_T *argvars;
18775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776{
18777 char_u *p;
18778 int n;
18779 int len;
18780 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018781 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018783 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 slen = (int)STRLEN(p);
18785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018786 n = get_tv_number_chk(&argvars[1], &error);
18787 if (error)
18788 len = 0;
18789 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 else
18792 len = slen - n; /* default len: all bytes that are available. */
18793
18794 /*
18795 * Only return the overlap between the specified part and the actual
18796 * string.
18797 */
18798 if (n < 0)
18799 {
18800 len += n;
18801 n = 0;
18802 }
18803 else if (n > slen)
18804 n = slen;
18805 if (len < 0)
18806 len = 0;
18807 else if (n + len > slen)
18808 len = slen - n;
18809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810 rettv->v_type = VAR_STRING;
18811 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812}
18813
18814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018815 * "strridx()" function
18816 */
18817 static void
18818f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018819 typval_T *argvars;
18820 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018821{
18822 char_u buf[NUMBUFLEN];
18823 char_u *needle;
18824 char_u *haystack;
18825 char_u *rest;
18826 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018827 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018829 needle = get_tv_string_chk(&argvars[1]);
18830 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018831
18832 rettv->vval.v_number = -1;
18833 if (needle == NULL || haystack == NULL)
18834 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018835
18836 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018837 if (argvars[2].v_type != VAR_UNKNOWN)
18838 {
18839 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018840 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018841 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018842 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018843 }
18844 else
18845 end_idx = haystack_len;
18846
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018848 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018849 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018850 lastmatch = haystack + end_idx;
18851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018852 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018853 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018854 for (rest = haystack; *rest != '\0'; ++rest)
18855 {
18856 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018857 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018858 break;
18859 lastmatch = rest;
18860 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018861 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018862
18863 if (lastmatch == NULL)
18864 rettv->vval.v_number = -1;
18865 else
18866 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18867}
18868
18869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 * "strtrans()" function
18871 */
18872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 typval_T *argvars;
18875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->v_type = VAR_STRING;
18878 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879}
18880
18881/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 * "submatch()" function
18883 */
18884 static void
18885f_submatch(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{
Bram Moolenaar41571762014-04-02 19:00:58 +020018889 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018890 int no;
18891 int retList = 0;
18892
18893 no = (int)get_tv_number_chk(&argvars[0], &error);
18894 if (error)
18895 return;
18896 error = FALSE;
18897 if (argvars[1].v_type != VAR_UNKNOWN)
18898 retList = get_tv_number_chk(&argvars[1], &error);
18899 if (error)
18900 return;
18901
18902 if (retList == 0)
18903 {
18904 rettv->v_type = VAR_STRING;
18905 rettv->vval.v_string = reg_submatch(no);
18906 }
18907 else
18908 {
18909 rettv->v_type = VAR_LIST;
18910 rettv->vval.v_list = reg_submatch_list(no);
18911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018912}
18913
18914/*
18915 * "substitute()" function
18916 */
18917 static void
18918f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018919 typval_T *argvars;
18920 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921{
18922 char_u patbuf[NUMBUFLEN];
18923 char_u subbuf[NUMBUFLEN];
18924 char_u flagsbuf[NUMBUFLEN];
18925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018926 char_u *str = get_tv_string_chk(&argvars[0]);
18927 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18928 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18929 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18930
Bram Moolenaar0d660222005-01-07 21:51:51 +000018931 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018932 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18933 rettv->vval.v_string = NULL;
18934 else
18935 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936}
18937
18938/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018939 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945{
18946 int id = 0;
18947#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018948 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 long col;
18950 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018951 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018953 lnum = get_tv_lnum(argvars); /* -1 on type error */
18954 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18955 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018958 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018959 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960#endif
18961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963}
18964
18965/*
18966 * "synIDattr(id, what [, mode])" function
18967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972{
18973 char_u *p = NULL;
18974#ifdef FEAT_SYN_HL
18975 int id;
18976 char_u *what;
18977 char_u *mode;
18978 char_u modebuf[NUMBUFLEN];
18979 int modec;
18980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018981 id = get_tv_number(&argvars[0]);
18982 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018985 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018987 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 modec = 0; /* replace invalid with current */
18989 }
18990 else
18991 {
18992#ifdef FEAT_GUI
18993 if (gui.in_use)
18994 modec = 'g';
18995 else
18996#endif
18997 if (t_colors > 1)
18998 modec = 'c';
18999 else
19000 modec = 't';
19001 }
19002
19003
19004 switch (TOLOWER_ASC(what[0]))
19005 {
19006 case 'b':
19007 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19008 p = highlight_color(id, what, modec);
19009 else /* bold */
19010 p = highlight_has_attr(id, HL_BOLD, modec);
19011 break;
19012
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019013 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 p = highlight_color(id, what, modec);
19015 break;
19016
19017 case 'i':
19018 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19019 p = highlight_has_attr(id, HL_INVERSE, modec);
19020 else /* italic */
19021 p = highlight_has_attr(id, HL_ITALIC, modec);
19022 break;
19023
19024 case 'n': /* name */
19025 p = get_highlight_name(NULL, id - 1);
19026 break;
19027
19028 case 'r': /* reverse */
19029 p = highlight_has_attr(id, HL_INVERSE, modec);
19030 break;
19031
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019032 case 's':
19033 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19034 p = highlight_color(id, what, modec);
19035 else /* standout */
19036 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 break;
19038
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019039 case 'u':
19040 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19041 /* underline */
19042 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19043 else
19044 /* undercurl */
19045 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 break;
19047 }
19048
19049 if (p != NULL)
19050 p = vim_strsave(p);
19051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019052 rettv->v_type = VAR_STRING;
19053 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054}
19055
19056/*
19057 * "synIDtrans(id)" function
19058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019060f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063{
19064 int id;
19065
19066#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019067 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068
19069 if (id > 0)
19070 id = syn_get_final_id(id);
19071 else
19072#endif
19073 id = 0;
19074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019075 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076}
19077
19078/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019079 * "synconcealed(lnum, col)" function
19080 */
19081 static void
19082f_synconcealed(argvars, rettv)
19083 typval_T *argvars UNUSED;
19084 typval_T *rettv;
19085{
19086#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19087 long lnum;
19088 long col;
19089 int syntax_flags = 0;
19090 int cchar;
19091 int matchid = 0;
19092 char_u str[NUMBUFLEN];
19093#endif
19094
19095 rettv->v_type = VAR_LIST;
19096 rettv->vval.v_list = NULL;
19097
19098#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19099 lnum = get_tv_lnum(argvars); /* -1 on type error */
19100 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19101
19102 vim_memset(str, NUL, sizeof(str));
19103
19104 if (rettv_list_alloc(rettv) != FAIL)
19105 {
19106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19107 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19108 && curwin->w_p_cole > 0)
19109 {
19110 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19111 syntax_flags = get_syntax_info(&matchid);
19112
19113 /* get the conceal character */
19114 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19115 {
19116 cchar = syn_get_sub_char();
19117 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19118 cchar = lcs_conceal;
19119 if (cchar != NUL)
19120 {
19121# ifdef FEAT_MBYTE
19122 if (has_mbyte)
19123 (*mb_char2bytes)(cchar, str);
19124 else
19125# endif
19126 str[0] = cchar;
19127 }
19128 }
19129 }
19130
19131 list_append_number(rettv->vval.v_list,
19132 (syntax_flags & HL_CONCEAL) != 0);
19133 /* -1 to auto-determine strlen */
19134 list_append_string(rettv->vval.v_list, str, -1);
19135 list_append_number(rettv->vval.v_list, matchid);
19136 }
19137#endif
19138}
19139
19140/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019141 * "synstack(lnum, col)" function
19142 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019143 static void
19144f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019145 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019146 typval_T *rettv;
19147{
19148#ifdef FEAT_SYN_HL
19149 long lnum;
19150 long col;
19151 int i;
19152 int id;
19153#endif
19154
19155 rettv->v_type = VAR_LIST;
19156 rettv->vval.v_list = NULL;
19157
19158#ifdef FEAT_SYN_HL
19159 lnum = get_tv_lnum(argvars); /* -1 on type error */
19160 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19161
19162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019163 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019164 && rettv_list_alloc(rettv) != FAIL)
19165 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019166 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019167 for (i = 0; ; ++i)
19168 {
19169 id = syn_get_stack_item(i);
19170 if (id < 0)
19171 break;
19172 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19173 break;
19174 }
19175 }
19176#endif
19177}
19178
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019180get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 typval_T *argvars;
19182 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019183 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019185 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019187 char_u *infile = NULL;
19188 char_u buf[NUMBUFLEN];
19189 int err = FALSE;
19190 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019191 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019192 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019194 rettv->v_type = VAR_STRING;
19195 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019196 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019197 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019200 {
19201 /*
19202 * Write the string to a temp file, to be used for input of the shell
19203 * command.
19204 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019205 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019206 {
19207 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019208 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019209 }
19210
19211 fd = mch_fopen((char *)infile, WRITEBIN);
19212 if (fd == NULL)
19213 {
19214 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019215 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019216 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019217 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019218 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019219 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19220 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019221 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019222 else
19223 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019224 size_t len;
19225
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019226 p = get_tv_string_buf_chk(&argvars[1], buf);
19227 if (p == NULL)
19228 {
19229 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019230 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019231 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019232 len = STRLEN(p);
19233 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019234 err = TRUE;
19235 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019236 if (fclose(fd) != 0)
19237 err = TRUE;
19238 if (err)
19239 {
19240 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019241 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019242 }
19243 }
19244
Bram Moolenaar52a72462014-08-29 15:53:52 +020019245 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19246 * echoes typeahead, that messes up the display. */
19247 if (!msg_silent)
19248 flags += SHELL_COOKED;
19249
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019250 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019252 int len;
19253 listitem_T *li;
19254 char_u *s = NULL;
19255 char_u *start;
19256 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019257 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258
Bram Moolenaar52a72462014-08-29 15:53:52 +020019259 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019260 if (res == NULL)
19261 goto errret;
19262
19263 list = list_alloc();
19264 if (list == NULL)
19265 goto errret;
19266
19267 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019269 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019270 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019272 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019273
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019274 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019275 if (s == NULL)
19276 goto errret;
19277
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019278 for (p = s; start < end; ++p, ++start)
19279 *p = *start == NUL ? NL : *start;
19280 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019281
19282 li = listitem_alloc();
19283 if (li == NULL)
19284 {
19285 vim_free(s);
19286 goto errret;
19287 }
19288 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019289 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019290 li->li_tv.vval.v_string = s;
19291 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019294 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019295 rettv->v_type = VAR_LIST;
19296 rettv->vval.v_list = list;
19297 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019299 else
19300 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019301 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019302#ifdef USE_CR
19303 /* translate <CR> into <NL> */
19304 if (res != NULL)
19305 {
19306 char_u *s;
19307
19308 for (s = res; *s; ++s)
19309 {
19310 if (*s == CAR)
19311 *s = NL;
19312 }
19313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314#else
19315# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019316 /* translate <CR><NL> into <NL> */
19317 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 char_u *s, *d;
19320
19321 d = res;
19322 for (s = res; *s; ++s)
19323 {
19324 if (s[0] == CAR && s[1] == NL)
19325 ++s;
19326 *d++ = *s;
19327 }
19328 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330# endif
19331#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019332 rettv->vval.v_string = res;
19333 res = NULL;
19334 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019335
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019336errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019337 if (infile != NULL)
19338 {
19339 mch_remove(infile);
19340 vim_free(infile);
19341 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019342 if (res != NULL)
19343 vim_free(res);
19344 if (list != NULL)
19345 list_free(list, TRUE);
19346}
19347
19348/*
19349 * "system()" function
19350 */
19351 static void
19352f_system(argvars, rettv)
19353 typval_T *argvars;
19354 typval_T *rettv;
19355{
19356 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19357}
19358
19359/*
19360 * "systemlist()" function
19361 */
19362 static void
19363f_systemlist(argvars, rettv)
19364 typval_T *argvars;
19365 typval_T *rettv;
19366{
19367 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368}
19369
19370/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019371 * "tabpagebuflist()" function
19372 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019373 static void
19374f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019375 typval_T *argvars UNUSED;
19376 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019377{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019378#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019379 tabpage_T *tp;
19380 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019381
19382 if (argvars[0].v_type == VAR_UNKNOWN)
19383 wp = firstwin;
19384 else
19385 {
19386 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19387 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019388 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019389 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019390 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019391 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019392 for (; wp != NULL; wp = wp->w_next)
19393 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019394 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019395 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019396 }
19397#endif
19398}
19399
19400
19401/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019402 * "tabpagenr()" function
19403 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019404 static void
19405f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019406 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019407 typval_T *rettv;
19408{
19409 int nr = 1;
19410#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019411 char_u *arg;
19412
19413 if (argvars[0].v_type != VAR_UNKNOWN)
19414 {
19415 arg = get_tv_string_chk(&argvars[0]);
19416 nr = 0;
19417 if (arg != NULL)
19418 {
19419 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019420 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019421 else
19422 EMSG2(_(e_invexpr2), arg);
19423 }
19424 }
19425 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019426 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019427#endif
19428 rettv->vval.v_number = nr;
19429}
19430
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019431
19432#ifdef FEAT_WINDOWS
19433static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19434
19435/*
19436 * Common code for tabpagewinnr() and winnr().
19437 */
19438 static int
19439get_winnr(tp, argvar)
19440 tabpage_T *tp;
19441 typval_T *argvar;
19442{
19443 win_T *twin;
19444 int nr = 1;
19445 win_T *wp;
19446 char_u *arg;
19447
19448 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19449 if (argvar->v_type != VAR_UNKNOWN)
19450 {
19451 arg = get_tv_string_chk(argvar);
19452 if (arg == NULL)
19453 nr = 0; /* type error; errmsg already given */
19454 else if (STRCMP(arg, "$") == 0)
19455 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19456 else if (STRCMP(arg, "#") == 0)
19457 {
19458 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19459 if (twin == NULL)
19460 nr = 0;
19461 }
19462 else
19463 {
19464 EMSG2(_(e_invexpr2), arg);
19465 nr = 0;
19466 }
19467 }
19468
19469 if (nr > 0)
19470 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19471 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019472 {
19473 if (wp == NULL)
19474 {
19475 /* didn't find it in this tabpage */
19476 nr = 0;
19477 break;
19478 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019479 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019480 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019481 return nr;
19482}
19483#endif
19484
19485/*
19486 * "tabpagewinnr()" function
19487 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019488 static void
19489f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019490 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019491 typval_T *rettv;
19492{
19493 int nr = 1;
19494#ifdef FEAT_WINDOWS
19495 tabpage_T *tp;
19496
19497 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19498 if (tp == NULL)
19499 nr = 0;
19500 else
19501 nr = get_winnr(tp, &argvars[1]);
19502#endif
19503 rettv->vval.v_number = nr;
19504}
19505
19506
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019507/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019508 * "tagfiles()" function
19509 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019510 static void
19511f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019512 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019513 typval_T *rettv;
19514{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019515 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019516 tagname_T tn;
19517 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019519 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019520 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019521 fname = alloc(MAXPATHL);
19522 if (fname == NULL)
19523 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019524
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019525 for (first = TRUE; ; first = FALSE)
19526 if (get_tagfname(&tn, first, fname) == FAIL
19527 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019528 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019529 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019530 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019531}
19532
19533/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019534 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019535 */
19536 static void
19537f_taglist(argvars, rettv)
19538 typval_T *argvars;
19539 typval_T *rettv;
19540{
19541 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019542
19543 tag_pattern = get_tv_string(&argvars[0]);
19544
19545 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019546 if (*tag_pattern == NUL)
19547 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019548
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019549 if (rettv_list_alloc(rettv) == OK)
19550 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019551}
19552
19553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 * "tempname()" function
19555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019558 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560{
19561 static int x = 'A';
19562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019564 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565
19566 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19567 * names. Skip 'I' and 'O', they are used for shell redirection. */
19568 do
19569 {
19570 if (x == 'Z')
19571 x = '0';
19572 else if (x == '9')
19573 x = 'A';
19574 else
19575 {
19576#ifdef EBCDIC
19577 if (x == 'I')
19578 x = 'J';
19579 else if (x == 'R')
19580 x = 'S';
19581 else
19582#endif
19583 ++x;
19584 }
19585 } while (x == 'I' || x == 'O');
19586}
19587
19588/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019589 * "test(list)" function: Just checking the walls...
19590 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019591 static void
19592f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019593 typval_T *argvars UNUSED;
19594 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019595{
19596 /* Used for unit testing. Change the code below to your liking. */
19597#if 0
19598 listitem_T *li;
19599 list_T *l;
19600 char_u *bad, *good;
19601
19602 if (argvars[0].v_type != VAR_LIST)
19603 return;
19604 l = argvars[0].vval.v_list;
19605 if (l == NULL)
19606 return;
19607 li = l->lv_first;
19608 if (li == NULL)
19609 return;
19610 bad = get_tv_string(&li->li_tv);
19611 li = li->li_next;
19612 if (li == NULL)
19613 return;
19614 good = get_tv_string(&li->li_tv);
19615 rettv->vval.v_number = test_edit_score(bad, good);
19616#endif
19617}
19618
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019619#ifdef FEAT_FLOAT
19620/*
19621 * "tan()" function
19622 */
19623 static void
19624f_tan(argvars, rettv)
19625 typval_T *argvars;
19626 typval_T *rettv;
19627{
19628 float_T f;
19629
19630 rettv->v_type = VAR_FLOAT;
19631 if (get_float_arg(argvars, &f) == OK)
19632 rettv->vval.v_float = tan(f);
19633 else
19634 rettv->vval.v_float = 0.0;
19635}
19636
19637/*
19638 * "tanh()" function
19639 */
19640 static void
19641f_tanh(argvars, rettv)
19642 typval_T *argvars;
19643 typval_T *rettv;
19644{
19645 float_T f;
19646
19647 rettv->v_type = VAR_FLOAT;
19648 if (get_float_arg(argvars, &f) == OK)
19649 rettv->vval.v_float = tanh(f);
19650 else
19651 rettv->vval.v_float = 0.0;
19652}
19653#endif
19654
Bram Moolenaard52d9742005-08-21 22:20:28 +000019655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 * "tolower(string)" function
19657 */
19658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019660 typval_T *argvars;
19661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662{
19663 char_u *p;
19664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019665 p = vim_strsave(get_tv_string(&argvars[0]));
19666 rettv->v_type = VAR_STRING;
19667 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668
19669 if (p != NULL)
19670 while (*p != NUL)
19671 {
19672#ifdef FEAT_MBYTE
19673 int l;
19674
19675 if (enc_utf8)
19676 {
19677 int c, lc;
19678
19679 c = utf_ptr2char(p);
19680 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019681 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 /* TODO: reallocate string when byte count changes. */
19683 if (utf_char2len(lc) == l)
19684 utf_char2bytes(lc, p);
19685 p += l;
19686 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019687 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 p += l; /* skip multi-byte character */
19689 else
19690#endif
19691 {
19692 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19693 ++p;
19694 }
19695 }
19696}
19697
19698/*
19699 * "toupper(string)" function
19700 */
19701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 typval_T *argvars;
19704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019707 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708}
19709
19710/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019711 * "tr(string, fromstr, tostr)" function
19712 */
19713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 typval_T *argvars;
19716 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019717{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019718 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019719 char_u *fromstr;
19720 char_u *tostr;
19721 char_u *p;
19722#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019723 int inlen;
19724 int fromlen;
19725 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019726 int idx;
19727 char_u *cpstr;
19728 int cplen;
19729 int first = TRUE;
19730#endif
19731 char_u buf[NUMBUFLEN];
19732 char_u buf2[NUMBUFLEN];
19733 garray_T ga;
19734
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019735 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019736 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19737 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019738
19739 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019740 rettv->v_type = VAR_STRING;
19741 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019742 if (fromstr == NULL || tostr == NULL)
19743 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019744 ga_init2(&ga, (int)sizeof(char), 80);
19745
19746#ifdef FEAT_MBYTE
19747 if (!has_mbyte)
19748#endif
19749 /* not multi-byte: fromstr and tostr must be the same length */
19750 if (STRLEN(fromstr) != STRLEN(tostr))
19751 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019752#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019753error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019754#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019755 EMSG2(_(e_invarg2), fromstr);
19756 ga_clear(&ga);
19757 return;
19758 }
19759
19760 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019761 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019762 {
19763#ifdef FEAT_MBYTE
19764 if (has_mbyte)
19765 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019766 inlen = (*mb_ptr2len)(in_str);
19767 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019768 cplen = inlen;
19769 idx = 0;
19770 for (p = fromstr; *p != NUL; p += fromlen)
19771 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019772 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019773 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019774 {
19775 for (p = tostr; *p != NUL; p += tolen)
19776 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019777 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019778 if (idx-- == 0)
19779 {
19780 cplen = tolen;
19781 cpstr = p;
19782 break;
19783 }
19784 }
19785 if (*p == NUL) /* tostr is shorter than fromstr */
19786 goto error;
19787 break;
19788 }
19789 ++idx;
19790 }
19791
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019792 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019793 {
19794 /* Check that fromstr and tostr have the same number of
19795 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019796 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019797 first = FALSE;
19798 for (p = tostr; *p != NUL; p += tolen)
19799 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019800 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019801 --idx;
19802 }
19803 if (idx != 0)
19804 goto error;
19805 }
19806
Bram Moolenaarcde88542015-08-11 19:14:00 +020019807 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019808 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019809 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019810
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019811 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019812 }
19813 else
19814#endif
19815 {
19816 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019817 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019818 if (p != NULL)
19819 ga_append(&ga, tostr[p - fromstr]);
19820 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019821 ga_append(&ga, *in_str);
19822 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019823 }
19824 }
19825
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019826 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019827 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019828 ga_append(&ga, NUL);
19829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019831}
19832
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019833#ifdef FEAT_FLOAT
19834/*
19835 * "trunc({float})" function
19836 */
19837 static void
19838f_trunc(argvars, rettv)
19839 typval_T *argvars;
19840 typval_T *rettv;
19841{
19842 float_T f;
19843
19844 rettv->v_type = VAR_FLOAT;
19845 if (get_float_arg(argvars, &f) == OK)
19846 /* trunc() is not in C90, use floor() or ceil() instead. */
19847 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19848 else
19849 rettv->vval.v_float = 0.0;
19850}
19851#endif
19852
Bram Moolenaar8299df92004-07-10 09:47:34 +000019853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 * "type(expr)" function
19855 */
19856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 typval_T *argvars;
19859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019861 int n;
19862
19863 switch (argvars[0].v_type)
19864 {
19865 case VAR_NUMBER: n = 0; break;
19866 case VAR_STRING: n = 1; break;
19867 case VAR_FUNC: n = 2; break;
19868 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019869 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019870#ifdef FEAT_FLOAT
19871 case VAR_FLOAT: n = 5; break;
19872#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019873 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19874 }
19875 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876}
19877
19878/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019879 * "undofile(name)" function
19880 */
19881 static void
19882f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019883 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019884 typval_T *rettv;
19885{
19886 rettv->v_type = VAR_STRING;
19887#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019888 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019889 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019890
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019891 if (*fname == NUL)
19892 {
19893 /* If there is no file name there will be no undo file. */
19894 rettv->vval.v_string = NULL;
19895 }
19896 else
19897 {
19898 char_u *ffname = FullName_save(fname, FALSE);
19899
19900 if (ffname != NULL)
19901 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19902 vim_free(ffname);
19903 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019904 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019905#else
19906 rettv->vval.v_string = NULL;
19907#endif
19908}
19909
19910/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019911 * "undotree()" function
19912 */
19913 static void
19914f_undotree(argvars, rettv)
19915 typval_T *argvars UNUSED;
19916 typval_T *rettv;
19917{
19918 if (rettv_dict_alloc(rettv) == OK)
19919 {
19920 dict_T *dict = rettv->vval.v_dict;
19921 list_T *list;
19922
Bram Moolenaar730cde92010-06-27 05:18:54 +020019923 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019924 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019925 dict_add_nr_str(dict, "save_last",
19926 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019927 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19928 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019929 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019930
19931 list = list_alloc();
19932 if (list != NULL)
19933 {
19934 u_eval_tree(curbuf->b_u_oldhead, list);
19935 dict_add_list(dict, "entries", list);
19936 }
19937 }
19938}
19939
19940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019941 * "values(dict)" function
19942 */
19943 static void
19944f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 typval_T *argvars;
19946 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019947{
19948 dict_list(argvars, rettv, 1);
19949}
19950
19951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 * "virtcol(string)" function
19953 */
19954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 typval_T *argvars;
19957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958{
19959 colnr_T vcol = 0;
19960 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019961 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019963 fp = var2fpos(&argvars[0], FALSE, &fnum);
19964 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19965 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 {
19967 getvvcol(curwin, fp, NULL, NULL, &vcol);
19968 ++vcol;
19969 }
19970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972}
19973
19974/*
19975 * "visualmode()" function
19976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019979 typval_T *argvars UNUSED;
19980 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 char_u str[2];
19983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 str[0] = curbuf->b_visual_mode_eval;
19986 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988
19989 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019990 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992}
19993
19994/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019995 * "wildmenumode()" function
19996 */
19997 static void
19998f_wildmenumode(argvars, rettv)
19999 typval_T *argvars UNUSED;
20000 typval_T *rettv UNUSED;
20001{
20002#ifdef FEAT_WILDMENU
20003 if (wild_menu_showing)
20004 rettv->vval.v_number = 1;
20005#endif
20006}
20007
20008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 * "winbufnr(nr)" function
20010 */
20011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 typval_T *argvars;
20014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015{
20016 win_T *wp;
20017
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020018 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023}
20024
20025/*
20026 * "wincol()" function
20027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020030 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032{
20033 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035}
20036
20037/*
20038 * "winheight(nr)" function
20039 */
20040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 typval_T *argvars;
20043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
20045 win_T *wp;
20046
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020047 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052}
20053
20054/*
20055 * "winline()" function
20056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061{
20062 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064}
20065
20066/*
20067 * "winnr()" function
20068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020070f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020071 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073{
20074 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020075
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020077 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080}
20081
20082/*
20083 * "winrestcmd()" function
20084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089{
20090#ifdef FEAT_WINDOWS
20091 win_T *wp;
20092 int winnr = 1;
20093 garray_T ga;
20094 char_u buf[50];
20095
20096 ga_init2(&ga, (int)sizeof(char), 70);
20097 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20098 {
20099 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20100 ga_concat(&ga, buf);
20101# ifdef FEAT_VERTSPLIT
20102 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20103 ga_concat(&ga, buf);
20104# endif
20105 ++winnr;
20106 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020107 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114}
20115
20116/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020117 * "winrestview()" function
20118 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020119 static void
20120f_winrestview(argvars, rettv)
20121 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020122 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020123{
20124 dict_T *dict;
20125
20126 if (argvars[0].v_type != VAR_DICT
20127 || (dict = argvars[0].vval.v_dict) == NULL)
20128 EMSG(_(e_invarg));
20129 else
20130 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020131 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20132 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20133 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20134 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020135#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020136 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20137 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020138#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020139 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20140 {
20141 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20142 curwin->w_set_curswant = FALSE;
20143 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020144
Bram Moolenaar82c25852014-05-28 16:47:16 +020020145 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20146 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020147#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020148 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20149 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020150#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020151 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20152 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20153 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20154 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020155
20156 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020157 win_new_height(curwin, curwin->w_height);
20158# ifdef FEAT_VERTSPLIT
20159 win_new_width(curwin, W_WIDTH(curwin));
20160# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020161 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020162
Bram Moolenaarb851a962014-10-31 15:45:52 +010020163 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020164 curwin->w_topline = 1;
20165 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20166 curwin->w_topline = curbuf->b_ml.ml_line_count;
20167#ifdef FEAT_DIFF
20168 check_topfill(curwin, TRUE);
20169#endif
20170 }
20171}
20172
20173/*
20174 * "winsaveview()" function
20175 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020176 static void
20177f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020178 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020179 typval_T *rettv;
20180{
20181 dict_T *dict;
20182
Bram Moolenaara800b422010-06-27 01:15:55 +020020183 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020184 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020185 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020186
20187 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20188 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20189#ifdef FEAT_VIRTUALEDIT
20190 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20191#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020192 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020193 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20194
20195 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20196#ifdef FEAT_DIFF
20197 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20198#endif
20199 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20200 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20201}
20202
20203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 * "winwidth(nr)" function
20205 */
20206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 typval_T *argvars;
20209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210{
20211 win_T *wp;
20212
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020213 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 else
20217#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221#endif
20222}
20223
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020225 * "wordcount()" function
20226 */
20227 static void
20228f_wordcount(argvars, rettv)
20229 typval_T *argvars UNUSED;
20230 typval_T *rettv;
20231{
20232 if (rettv_dict_alloc(rettv) == FAIL)
20233 return;
20234 cursor_pos_info(rettv->vval.v_dict);
20235}
20236
20237/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020238 * Write list of strings to file
20239 */
20240 static int
20241write_list(fd, list, binary)
20242 FILE *fd;
20243 list_T *list;
20244 int binary;
20245{
20246 listitem_T *li;
20247 int c;
20248 int ret = OK;
20249 char_u *s;
20250
20251 for (li = list->lv_first; li != NULL; li = li->li_next)
20252 {
20253 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20254 {
20255 if (*s == '\n')
20256 c = putc(NUL, fd);
20257 else
20258 c = putc(*s, fd);
20259 if (c == EOF)
20260 {
20261 ret = FAIL;
20262 break;
20263 }
20264 }
20265 if (!binary || li->li_next != NULL)
20266 if (putc('\n', fd) == EOF)
20267 {
20268 ret = FAIL;
20269 break;
20270 }
20271 if (ret == FAIL)
20272 {
20273 EMSG(_(e_write));
20274 break;
20275 }
20276 }
20277 return ret;
20278}
20279
20280/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020281 * "writefile()" function
20282 */
20283 static void
20284f_writefile(argvars, rettv)
20285 typval_T *argvars;
20286 typval_T *rettv;
20287{
20288 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020289 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020290 char_u *fname;
20291 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020292 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020293
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020294 if (check_restricted() || check_secure())
20295 return;
20296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020297 if (argvars[0].v_type != VAR_LIST)
20298 {
20299 EMSG2(_(e_listarg), "writefile()");
20300 return;
20301 }
20302 if (argvars[0].vval.v_list == NULL)
20303 return;
20304
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020305 if (argvars[2].v_type != VAR_UNKNOWN)
20306 {
20307 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20308 binary = TRUE;
20309 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20310 append = TRUE;
20311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020312
20313 /* Always open the file in binary mode, library functions have a mind of
20314 * their own about CR-LF conversion. */
20315 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020316 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20317 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020318 {
20319 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20320 ret = -1;
20321 }
20322 else
20323 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020324 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20325 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020326 fclose(fd);
20327 }
20328
20329 rettv->vval.v_number = ret;
20330}
20331
20332/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020333 * "xor(expr, expr)" function
20334 */
20335 static void
20336f_xor(argvars, rettv)
20337 typval_T *argvars;
20338 typval_T *rettv;
20339{
20340 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20341 ^ get_tv_number_chk(&argvars[1], NULL);
20342}
20343
20344
20345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020347 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 */
20349 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020350var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020352 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020353 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020355 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020357 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358
Bram Moolenaara5525202006-03-02 22:52:09 +000020359 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020360 if (varp->v_type == VAR_LIST)
20361 {
20362 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020363 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020364 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020365 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020366
20367 l = varp->vval.v_list;
20368 if (l == NULL)
20369 return NULL;
20370
20371 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020372 pos.lnum = list_find_nr(l, 0L, &error);
20373 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020374 return NULL; /* invalid line number */
20375
20376 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020377 pos.col = list_find_nr(l, 1L, &error);
20378 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020379 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020380 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020381
20382 /* We accept "$" for the column number: last column. */
20383 li = list_find(l, 1L);
20384 if (li != NULL && li->li_tv.v_type == VAR_STRING
20385 && li->li_tv.vval.v_string != NULL
20386 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20387 pos.col = len + 1;
20388
Bram Moolenaara5525202006-03-02 22:52:09 +000020389 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020390 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020391 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020392 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020393
Bram Moolenaara5525202006-03-02 22:52:09 +000020394#ifdef FEAT_VIRTUALEDIT
20395 /* Get the virtual offset. Defaults to zero. */
20396 pos.coladd = list_find_nr(l, 2L, &error);
20397 if (error)
20398 pos.coladd = 0;
20399#endif
20400
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020401 return &pos;
20402 }
20403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020404 name = get_tv_string_chk(varp);
20405 if (name == NULL)
20406 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020407 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020409 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20410 {
20411 if (VIsual_active)
20412 return &VIsual;
20413 return &curwin->w_cursor;
20414 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020415 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020417 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20419 return NULL;
20420 return pp;
20421 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020422
20423#ifdef FEAT_VIRTUALEDIT
20424 pos.coladd = 0;
20425#endif
20426
Bram Moolenaar477933c2007-07-17 14:32:23 +000020427 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020428 {
20429 pos.col = 0;
20430 if (name[1] == '0') /* "w0": first visible line */
20431 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020432 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020433 pos.lnum = curwin->w_topline;
20434 return &pos;
20435 }
20436 else if (name[1] == '$') /* "w$": last visible line */
20437 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020438 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020439 pos.lnum = curwin->w_botline - 1;
20440 return &pos;
20441 }
20442 }
20443 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020445 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 {
20447 pos.lnum = curbuf->b_ml.ml_line_count;
20448 pos.col = 0;
20449 }
20450 else
20451 {
20452 pos.lnum = curwin->w_cursor.lnum;
20453 pos.col = (colnr_T)STRLEN(ml_get_curline());
20454 }
20455 return &pos;
20456 }
20457 return NULL;
20458}
20459
20460/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020461 * Convert list in "arg" into a position and optional file number.
20462 * When "fnump" is NULL there is no file number, only 3 items.
20463 * Note that the column is passed on as-is, the caller may want to decrement
20464 * it to use 1 for the first column.
20465 * Return FAIL when conversion is not possible, doesn't check the position for
20466 * validity.
20467 */
20468 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020469list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020470 typval_T *arg;
20471 pos_T *posp;
20472 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020473 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020474{
20475 list_T *l = arg->vval.v_list;
20476 long i = 0;
20477 long n;
20478
Bram Moolenaar493c1782014-05-28 14:34:46 +020020479 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20480 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020481 if (arg->v_type != VAR_LIST
20482 || l == NULL
20483 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020484 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020485 return FAIL;
20486
20487 if (fnump != NULL)
20488 {
20489 n = list_find_nr(l, i++, NULL); /* fnum */
20490 if (n < 0)
20491 return FAIL;
20492 if (n == 0)
20493 n = curbuf->b_fnum; /* current buffer */
20494 *fnump = n;
20495 }
20496
20497 n = list_find_nr(l, i++, NULL); /* lnum */
20498 if (n < 0)
20499 return FAIL;
20500 posp->lnum = n;
20501
20502 n = list_find_nr(l, i++, NULL); /* col */
20503 if (n < 0)
20504 return FAIL;
20505 posp->col = n;
20506
20507#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020508 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020509 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020510 posp->coladd = 0;
20511 else
20512 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020513#endif
20514
Bram Moolenaar493c1782014-05-28 14:34:46 +020020515 if (curswantp != NULL)
20516 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20517
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020518 return OK;
20519}
20520
20521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 * Get the length of an environment variable name.
20523 * Advance "arg" to the first character after the name.
20524 * Return 0 for error.
20525 */
20526 static int
20527get_env_len(arg)
20528 char_u **arg;
20529{
20530 char_u *p;
20531 int len;
20532
20533 for (p = *arg; vim_isIDc(*p); ++p)
20534 ;
20535 if (p == *arg) /* no name found */
20536 return 0;
20537
20538 len = (int)(p - *arg);
20539 *arg = p;
20540 return len;
20541}
20542
20543/*
20544 * Get the length of the name of a function or internal variable.
20545 * "arg" is advanced to the first non-white character after the name.
20546 * Return 0 if something is wrong.
20547 */
20548 static int
20549get_id_len(arg)
20550 char_u **arg;
20551{
20552 char_u *p;
20553 int len;
20554
20555 /* Find the end of the name. */
20556 for (p = *arg; eval_isnamec(*p); ++p)
20557 ;
20558 if (p == *arg) /* no name found */
20559 return 0;
20560
20561 len = (int)(p - *arg);
20562 *arg = skipwhite(p);
20563
20564 return len;
20565}
20566
20567/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020568 * Get the length of the name of a variable or function.
20569 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020571 * Return -1 if curly braces expansion failed.
20572 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 * If the name contains 'magic' {}'s, expand them and return the
20574 * expanded name in an allocated string via 'alias' - caller must free.
20575 */
20576 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020577get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 char_u **arg;
20579 char_u **alias;
20580 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020581 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582{
20583 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 char_u *p;
20585 char_u *expr_start;
20586 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587
20588 *alias = NULL; /* default to no alias */
20589
20590 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20591 && (*arg)[2] == (int)KE_SNR)
20592 {
20593 /* hard coded <SNR>, already translated */
20594 *arg += 3;
20595 return get_id_len(arg) + 3;
20596 }
20597 len = eval_fname_script(*arg);
20598 if (len > 0)
20599 {
20600 /* literal "<SID>", "s:" or "<SNR>" */
20601 *arg += len;
20602 }
20603
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020605 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020607 p = find_name_end(*arg, &expr_start, &expr_end,
20608 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 if (expr_start != NULL)
20610 {
20611 char_u *temp_string;
20612
20613 if (!evaluate)
20614 {
20615 len += (int)(p - *arg);
20616 *arg = skipwhite(p);
20617 return len;
20618 }
20619
20620 /*
20621 * Include any <SID> etc in the expanded string:
20622 * Thus the -len here.
20623 */
20624 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20625 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020626 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 *alias = temp_string;
20628 *arg = skipwhite(p);
20629 return (int)STRLEN(temp_string);
20630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631
20632 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020633 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 EMSG2(_(e_invexpr2), *arg);
20635
20636 return len;
20637}
20638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639/*
20640 * Find the end of a variable or function name, taking care of magic braces.
20641 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20642 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020643 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020644 * Return a pointer to just after the name. Equal to "arg" if there is no
20645 * valid name.
20646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020648find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 char_u *arg;
20650 char_u **expr_start;
20651 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020652 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020654 int mb_nest = 0;
20655 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 char_u *p;
20657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020658 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 *expr_start = NULL;
20661 *expr_end = NULL;
20662 }
20663
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020664 /* Quick check for valid starting character. */
20665 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20666 return arg;
20667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020668 for (p = arg; *p != NUL
20669 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020670 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020671 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020672 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020673 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020675 if (*p == '\'')
20676 {
20677 /* skip over 'string' to avoid counting [ and ] inside it. */
20678 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20679 ;
20680 if (*p == NUL)
20681 break;
20682 }
20683 else if (*p == '"')
20684 {
20685 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20686 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20687 if (*p == '\\' && p[1] != NUL)
20688 ++p;
20689 if (*p == NUL)
20690 break;
20691 }
20692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020695 if (*p == '[')
20696 ++br_nest;
20697 else if (*p == ']')
20698 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020701 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020703 if (*p == '{')
20704 {
20705 mb_nest++;
20706 if (expr_start != NULL && *expr_start == NULL)
20707 *expr_start = p;
20708 }
20709 else if (*p == '}')
20710 {
20711 mb_nest--;
20712 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20713 *expr_end = p;
20714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 }
20717
20718 return p;
20719}
20720
20721/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020722 * Expands out the 'magic' {}'s in a variable/function name.
20723 * Note that this can call itself recursively, to deal with
20724 * constructs like foo{bar}{baz}{bam}
20725 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20726 * "in_start" ^
20727 * "expr_start" ^
20728 * "expr_end" ^
20729 * "in_end" ^
20730 *
20731 * Returns a new allocated string, which the caller must free.
20732 * Returns NULL for failure.
20733 */
20734 static char_u *
20735make_expanded_name(in_start, expr_start, expr_end, in_end)
20736 char_u *in_start;
20737 char_u *expr_start;
20738 char_u *expr_end;
20739 char_u *in_end;
20740{
20741 char_u c1;
20742 char_u *retval = NULL;
20743 char_u *temp_result;
20744 char_u *nextcmd = NULL;
20745
20746 if (expr_end == NULL || in_end == NULL)
20747 return NULL;
20748 *expr_start = NUL;
20749 *expr_end = NUL;
20750 c1 = *in_end;
20751 *in_end = NUL;
20752
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020753 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020754 if (temp_result != NULL && nextcmd == NULL)
20755 {
20756 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20757 + (in_end - expr_end) + 1));
20758 if (retval != NULL)
20759 {
20760 STRCPY(retval, in_start);
20761 STRCAT(retval, temp_result);
20762 STRCAT(retval, expr_end + 1);
20763 }
20764 }
20765 vim_free(temp_result);
20766
20767 *in_end = c1; /* put char back for error messages */
20768 *expr_start = '{';
20769 *expr_end = '}';
20770
20771 if (retval != NULL)
20772 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020773 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020774 if (expr_start != NULL)
20775 {
20776 /* Further expansion! */
20777 temp_result = make_expanded_name(retval, expr_start,
20778 expr_end, temp_result);
20779 vim_free(retval);
20780 retval = temp_result;
20781 }
20782 }
20783
20784 return retval;
20785}
20786
20787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020789 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 */
20791 static int
20792eval_isnamec(c)
20793 int c;
20794{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020795 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20796}
20797
20798/*
20799 * Return TRUE if character "c" can be used as the first character in a
20800 * variable or function name (excluding '{' and '}').
20801 */
20802 static int
20803eval_isnamec1(c)
20804 int c;
20805{
20806 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807}
20808
20809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 * Set number v: variable to "val".
20811 */
20812 void
20813set_vim_var_nr(idx, val)
20814 int idx;
20815 long val;
20816{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020817 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818}
20819
20820/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020821 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 */
20823 long
20824get_vim_var_nr(idx)
20825 int idx;
20826{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020827 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828}
20829
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020830/*
20831 * Get string v: variable value. Uses a static buffer, can only be used once.
20832 */
20833 char_u *
20834get_vim_var_str(idx)
20835 int idx;
20836{
20837 return get_tv_string(&vimvars[idx].vv_tv);
20838}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020839
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020841 * Get List v: variable value. Caller must take care of reference count when
20842 * needed.
20843 */
20844 list_T *
20845get_vim_var_list(idx)
20846 int idx;
20847{
20848 return vimvars[idx].vv_list;
20849}
20850
20851/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020852 * Set v:char to character "c".
20853 */
20854 void
20855set_vim_var_char(c)
20856 int c;
20857{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020858 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020859
20860#ifdef FEAT_MBYTE
20861 if (has_mbyte)
20862 buf[(*mb_char2bytes)(c, buf)] = NUL;
20863 else
20864#endif
20865 {
20866 buf[0] = c;
20867 buf[1] = NUL;
20868 }
20869 set_vim_var_string(VV_CHAR, buf, -1);
20870}
20871
20872/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020873 * Set v:count to "count" and v:count1 to "count1".
20874 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 */
20876 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020877set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 long count;
20879 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020880 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020882 if (set_prevcount)
20883 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020884 vimvars[VV_COUNT].vv_nr = count;
20885 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886}
20887
20888/*
20889 * Set string v: variable to a copy of "val".
20890 */
20891 void
20892set_vim_var_string(idx, val, len)
20893 int idx;
20894 char_u *val;
20895 int len; /* length of "val" to use or -1 (whole string) */
20896{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020897 /* Need to do this (at least) once, since we can't initialize a union.
20898 * Will always be invoked when "v:progname" is set. */
20899 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20900
Bram Moolenaare9a41262005-01-15 22:18:47 +000020901 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020903 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020905 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020907 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908}
20909
20910/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020911 * Set List v: variable to "val".
20912 */
20913 void
20914set_vim_var_list(idx, val)
20915 int idx;
20916 list_T *val;
20917{
20918 list_unref(vimvars[idx].vv_list);
20919 vimvars[idx].vv_list = val;
20920 if (val != NULL)
20921 ++val->lv_refcount;
20922}
20923
20924/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020925 * Set Dictionary v: variable to "val".
20926 */
20927 void
20928set_vim_var_dict(idx, val)
20929 int idx;
20930 dict_T *val;
20931{
20932 int todo;
20933 hashitem_T *hi;
20934
20935 dict_unref(vimvars[idx].vv_dict);
20936 vimvars[idx].vv_dict = val;
20937 if (val != NULL)
20938 {
20939 ++val->dv_refcount;
20940
20941 /* Set readonly */
20942 todo = (int)val->dv_hashtab.ht_used;
20943 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20944 {
20945 if (HASHITEM_EMPTY(hi))
20946 continue;
20947 --todo;
20948 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20949 }
20950 }
20951}
20952
20953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 * Set v:register if needed.
20955 */
20956 void
20957set_reg_var(c)
20958 int c;
20959{
20960 char_u regname;
20961
20962 if (c == 0 || c == ' ')
20963 regname = '"';
20964 else
20965 regname = c;
20966 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 set_vim_var_string(VV_REG, &regname, 1);
20969}
20970
20971/*
20972 * Get or set v:exception. If "oldval" == NULL, return the current value.
20973 * Otherwise, restore the value to "oldval" and return NULL.
20974 * Must always be called in pairs to save and restore v:exception! Does not
20975 * take care of memory allocations.
20976 */
20977 char_u *
20978v_exception(oldval)
20979 char_u *oldval;
20980{
20981 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020982 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983
Bram Moolenaare9a41262005-01-15 22:18:47 +000020984 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 return NULL;
20986}
20987
20988/*
20989 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20990 * Otherwise, restore the value to "oldval" and return NULL.
20991 * Must always be called in pairs to save and restore v:throwpoint! Does not
20992 * take care of memory allocations.
20993 */
20994 char_u *
20995v_throwpoint(oldval)
20996 char_u *oldval;
20997{
20998 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020999 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000
Bram Moolenaare9a41262005-01-15 22:18:47 +000021001 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 return NULL;
21003}
21004
21005#if defined(FEAT_AUTOCMD) || defined(PROTO)
21006/*
21007 * Set v:cmdarg.
21008 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21009 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21010 * Must always be called in pairs!
21011 */
21012 char_u *
21013set_cmdarg(eap, oldarg)
21014 exarg_T *eap;
21015 char_u *oldarg;
21016{
21017 char_u *oldval;
21018 char_u *newval;
21019 unsigned len;
21020
Bram Moolenaare9a41262005-01-15 22:18:47 +000021021 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021022 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021024 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021025 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021026 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 }
21028
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021029 if (eap->force_bin == FORCE_BIN)
21030 len = 6;
21031 else if (eap->force_bin == FORCE_NOBIN)
21032 len = 8;
21033 else
21034 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021035
21036 if (eap->read_edit)
21037 len += 7;
21038
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021039 if (eap->force_ff != 0)
21040 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21041# ifdef FEAT_MBYTE
21042 if (eap->force_enc != 0)
21043 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021044 if (eap->bad_char != 0)
21045 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021046# endif
21047
21048 newval = alloc(len + 1);
21049 if (newval == NULL)
21050 return NULL;
21051
21052 if (eap->force_bin == FORCE_BIN)
21053 sprintf((char *)newval, " ++bin");
21054 else if (eap->force_bin == FORCE_NOBIN)
21055 sprintf((char *)newval, " ++nobin");
21056 else
21057 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021058
21059 if (eap->read_edit)
21060 STRCAT(newval, " ++edit");
21061
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021062 if (eap->force_ff != 0)
21063 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21064 eap->cmd + eap->force_ff);
21065# ifdef FEAT_MBYTE
21066 if (eap->force_enc != 0)
21067 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21068 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021069 if (eap->bad_char == BAD_KEEP)
21070 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21071 else if (eap->bad_char == BAD_DROP)
21072 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21073 else if (eap->bad_char != 0)
21074 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021075# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021077 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078}
21079#endif
21080
21081/*
21082 * Get the value of internal variable "name".
21083 * Return OK or FAIL.
21084 */
21085 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021086get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 char_u *name;
21088 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021089 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021090 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021091 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021092 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
21094 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 typval_T *tv = NULL;
21096 typval_T atv;
21097 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099
21100 /* truncate the name, so that we can use strcmp() */
21101 cc = name[len];
21102 name[len] = NUL;
21103
21104 /*
21105 * Check for "b:changedtick".
21106 */
21107 if (STRCMP(name, "b:changedtick") == 0)
21108 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021109 atv.v_type = VAR_NUMBER;
21110 atv.vval.v_number = curbuf->b_changedtick;
21111 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 }
21113
21114 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 * Check for user-defined variables.
21116 */
21117 else
21118 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021119 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021121 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021123 if (dip != NULL)
21124 *dip = v;
21125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 }
21127
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021130 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021131 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 ret = FAIL;
21133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021134 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021135 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136
21137 name[len] = cc;
21138
21139 return ret;
21140}
21141
21142/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021143 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21144 * Also handle function call with Funcref variable: func(expr)
21145 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21146 */
21147 static int
21148handle_subscript(arg, rettv, evaluate, verbose)
21149 char_u **arg;
21150 typval_T *rettv;
21151 int evaluate; /* do more than finding the end */
21152 int verbose; /* give error messages */
21153{
21154 int ret = OK;
21155 dict_T *selfdict = NULL;
21156 char_u *s;
21157 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021158 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159
21160 while (ret == OK
21161 && (**arg == '['
21162 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021163 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021164 && !vim_iswhite(*(*arg - 1)))
21165 {
21166 if (**arg == '(')
21167 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021168 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021169 if (evaluate)
21170 {
21171 functv = *rettv;
21172 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021173
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021174 /* Invoke the function. Recursive! */
21175 s = functv.vval.v_string;
21176 }
21177 else
21178 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021179 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021180 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21181 &len, evaluate, selfdict);
21182
21183 /* Clear the funcref afterwards, so that deleting it while
21184 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021185 if (evaluate)
21186 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021187
21188 /* Stop the expression evaluation when immediately aborting on
21189 * error, or when an interrupt occurred or an exception was thrown
21190 * but not caught. */
21191 if (aborting())
21192 {
21193 if (ret == OK)
21194 clear_tv(rettv);
21195 ret = FAIL;
21196 }
21197 dict_unref(selfdict);
21198 selfdict = NULL;
21199 }
21200 else /* **arg == '[' || **arg == '.' */
21201 {
21202 dict_unref(selfdict);
21203 if (rettv->v_type == VAR_DICT)
21204 {
21205 selfdict = rettv->vval.v_dict;
21206 if (selfdict != NULL)
21207 ++selfdict->dv_refcount;
21208 }
21209 else
21210 selfdict = NULL;
21211 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21212 {
21213 clear_tv(rettv);
21214 ret = FAIL;
21215 }
21216 }
21217 }
21218 dict_unref(selfdict);
21219 return ret;
21220}
21221
21222/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021223 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021224 * value).
21225 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021226 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021228{
Bram Moolenaar33570922005-01-25 22:26:29 +000021229 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021230}
21231
21232/*
21233 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 * The string "s" must have been allocated, it is consumed.
21235 * Return NULL for out of memory, the variable otherwise.
21236 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021237 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021238alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 char_u *s;
21240{
Bram Moolenaar33570922005-01-25 22:26:29 +000021241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 rettv = alloc_tv();
21244 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv->v_type = VAR_STRING;
21247 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 }
21249 else
21250 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021251 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252}
21253
21254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021255 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021257 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021259 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260{
21261 if (varp != NULL)
21262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021263 switch (varp->v_type)
21264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021265 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021266 func_unref(varp->vval.v_string);
21267 /*FALLTHROUGH*/
21268 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021269 vim_free(varp->vval.v_string);
21270 break;
21271 case VAR_LIST:
21272 list_unref(varp->vval.v_list);
21273 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021274 case VAR_DICT:
21275 dict_unref(varp->vval.v_dict);
21276 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021277 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021278#ifdef FEAT_FLOAT
21279 case VAR_FLOAT:
21280#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021281 case VAR_UNKNOWN:
21282 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021283 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021284 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021285 break;
21286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 vim_free(varp);
21288 }
21289}
21290
21291/*
21292 * Free the memory for a variable value and set the value to NULL or 0.
21293 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021294 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021295clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021296 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297{
21298 if (varp != NULL)
21299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021300 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021302 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303 func_unref(varp->vval.v_string);
21304 /*FALLTHROUGH*/
21305 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021306 vim_free(varp->vval.v_string);
21307 varp->vval.v_string = NULL;
21308 break;
21309 case VAR_LIST:
21310 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021311 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021312 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021313 case VAR_DICT:
21314 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021315 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021316 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021317 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021318 varp->vval.v_number = 0;
21319 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021320#ifdef FEAT_FLOAT
21321 case VAR_FLOAT:
21322 varp->vval.v_float = 0.0;
21323 break;
21324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325 case VAR_UNKNOWN:
21326 break;
21327 default:
21328 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021330 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 }
21332}
21333
21334/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 * Set the value of a variable to NULL without freeing items.
21336 */
21337 static void
21338init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021339 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021340{
21341 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021343}
21344
21345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 * Get the number value of a variable.
21347 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021348 * For incompatible types, return 0.
21349 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21350 * caller of incompatible types: it sets *denote to TRUE if "denote"
21351 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 */
21353 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021355 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021357 int error = FALSE;
21358
21359 return get_tv_number_chk(varp, &error); /* return 0L on error */
21360}
21361
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021362 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021363get_tv_number_chk(varp, denote)
21364 typval_T *varp;
21365 int *denote;
21366{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021367 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021369 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021372 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021373#ifdef FEAT_FLOAT
21374 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021375 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021376 break;
21377#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021378 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021379 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021380 break;
21381 case VAR_STRING:
21382 if (varp->vval.v_string != NULL)
21383 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021384 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021385 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021386 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021387 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021388 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021389 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021390 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021391 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021392 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021393 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021396 if (denote == NULL) /* useful for values that must be unsigned */
21397 n = -1;
21398 else
21399 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021400 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401}
21402
21403/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021404 * Get the lnum from the first argument.
21405 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021406 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 */
21408 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021409get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411{
Bram Moolenaar33570922005-01-25 22:26:29 +000021412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 linenr_T lnum;
21414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021415 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 if (lnum == 0) /* no valid number, try using line() */
21417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 rettv.v_type = VAR_NUMBER;
21419 f_line(argvars, &rettv);
21420 lnum = rettv.vval.v_number;
21421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 }
21423 return lnum;
21424}
21425
21426/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021427 * Get the lnum from the first argument.
21428 * Also accepts "$", then "buf" is used.
21429 * Returns 0 on error.
21430 */
21431 static linenr_T
21432get_tv_lnum_buf(argvars, buf)
21433 typval_T *argvars;
21434 buf_T *buf;
21435{
21436 if (argvars[0].v_type == VAR_STRING
21437 && argvars[0].vval.v_string != NULL
21438 && argvars[0].vval.v_string[0] == '$'
21439 && buf != NULL)
21440 return buf->b_ml.ml_line_count;
21441 return get_tv_number_chk(&argvars[0], NULL);
21442}
21443
21444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 * Get the string value of a variable.
21446 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021447 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21448 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 * If the String variable has never been set, return an empty string.
21450 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021451 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21452 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 */
21454 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457{
21458 static char_u mybuf[NUMBUFLEN];
21459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461}
21462
21463 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021464get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021466 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021468 char_u *res = get_tv_string_buf_chk(varp, buf);
21469
21470 return res != NULL ? res : (char_u *)"";
21471}
21472
Bram Moolenaar7d647822014-04-05 21:28:56 +020021473/*
21474 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21475 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021476 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021477get_tv_string_chk(varp)
21478 typval_T *varp;
21479{
21480 static char_u mybuf[NUMBUFLEN];
21481
21482 return get_tv_string_buf_chk(varp, mybuf);
21483}
21484
21485 static char_u *
21486get_tv_string_buf_chk(varp, buf)
21487 typval_T *varp;
21488 char_u *buf;
21489{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021490 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021492 case VAR_NUMBER:
21493 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21494 return buf;
21495 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021497 break;
21498 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021499 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021500 break;
21501 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021504#ifdef FEAT_FLOAT
21505 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021506 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507 break;
21508#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021509 case VAR_STRING:
21510 if (varp->vval.v_string != NULL)
21511 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021512 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021513 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021514 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021517 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518}
21519
21520/*
21521 * Find variable "name" in the list of variables.
21522 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021523 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021524 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021527 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021528find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021530 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021531 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535
Bram Moolenaara7043832005-01-21 11:56:39 +000021536 ht = find_var_ht(name, &varname);
21537 if (htp != NULL)
21538 *htp = ht;
21539 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021541 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542}
21543
21544/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021545 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021546 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021548 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021549find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021551 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021552 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021553 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021554{
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 hashitem_T *hi;
21556
21557 if (*varname == NUL)
21558 {
21559 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021560 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021561 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021562 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 case 'g': return &globvars_var;
21564 case 'v': return &vimvars_var;
21565 case 'b': return &curbuf->b_bufvar;
21566 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021567#ifdef FEAT_WINDOWS
21568 case 't': return &curtab->tp_winvar;
21569#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021570 case 'l': return current_funccal == NULL
21571 ? NULL : &current_funccal->l_vars_var;
21572 case 'a': return current_funccal == NULL
21573 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 }
21575 return NULL;
21576 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021577
21578 hi = hash_find(ht, varname);
21579 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021580 {
21581 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021582 * worked find the variable again. Don't auto-load a script if it was
21583 * loaded already, otherwise it would be loaded every time when
21584 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021585 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021586 {
21587 /* Note: script_autoload() may make "hi" invalid. It must either
21588 * be obtained again or not used. */
21589 if (!script_autoload(varname, FALSE) || aborting())
21590 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021591 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021592 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021593 if (HASHITEM_EMPTY(hi))
21594 return NULL;
21595 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021596 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021597}
21598
21599/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021600 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021601 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021602 * Set "varname" to the start of name without ':'.
21603 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021604 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021605find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 char_u *name;
21607 char_u **varname;
21608{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021609 hashitem_T *hi;
21610
Bram Moolenaar73627d02015-08-11 15:46:09 +020021611 if (name[0] == NUL)
21612 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 if (name[1] != ':')
21614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021615 /* The name must not start with a colon or #. */
21616 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 return NULL;
21618 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021619
21620 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021621 hi = hash_find(&compat_hashtab, name);
21622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021623 return &compat_hashtab;
21624
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 return &globvarht; /* global variable */
21627 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 }
21629 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630 if (*name == 'g') /* global variable */
21631 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021632 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21633 */
21634 if (vim_strchr(name + 2, ':') != NULL
21635 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021636 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021638 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021640 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021641#ifdef FEAT_WINDOWS
21642 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021643 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 if (*name == 'v') /* v: variable */
21646 return &vimvarht;
21647 if (*name == 'a' && current_funccal != NULL) /* function argument */
21648 return &current_funccal->l_avars.dv_hashtab;
21649 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21650 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 if (*name == 's' /* script variable */
21652 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21653 return &SCRIPT_VARS(current_SID);
21654 return NULL;
21655}
21656
21657/*
21658 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021659 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 * Returns NULL when it doesn't exist.
21661 */
21662 char_u *
21663get_var_value(name)
21664 char_u *name;
21665{
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021668 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 if (v == NULL)
21670 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672}
21673
21674/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 * sourcing this script and when executing functions defined in the script.
21677 */
21678 void
21679new_script_vars(id)
21680 scid_T id;
21681{
Bram Moolenaara7043832005-01-21 11:56:39 +000021682 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 hashtab_T *ht;
21684 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021685
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21687 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021688 /* Re-allocating ga_data means that an ht_array pointing to
21689 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021690 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 for (i = 1; i <= ga_scripts.ga_len; ++i)
21692 {
21693 ht = &SCRIPT_VARS(i);
21694 if (ht->ht_mask == HT_INIT_SIZE - 1)
21695 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021696 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021697 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021698 }
21699
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 while (ga_scripts.ga_len < id)
21701 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021702 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021703 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021704 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 }
21707 }
21708}
21709
21710/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21712 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 */
21714 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021715init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 dict_T *dict;
21717 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021718 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719{
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021721 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021722 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021723 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021724 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021725 dict_var->di_tv.vval.v_dict = dict;
21726 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021727 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21729 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730}
21731
21732/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021733 * Unreference a dictionary initialized by init_var_dict().
21734 */
21735 void
21736unref_var_dict(dict)
21737 dict_T *dict;
21738{
21739 /* Now the dict needs to be freed if no one else is using it, go back to
21740 * normal reference counting. */
21741 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21742 dict_unref(dict);
21743}
21744
21745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 * Frees all allocated variables and the value they contain.
21748 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 */
21750 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021751vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 hashtab_T *ht;
21753{
21754 vars_clear_ext(ht, TRUE);
21755}
21756
21757/*
21758 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21759 */
21760 static void
21761vars_clear_ext(ht, free_val)
21762 hashtab_T *ht;
21763 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764{
Bram Moolenaara7043832005-01-21 11:56:39 +000021765 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021766 hashitem_T *hi;
21767 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021770 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021771 for (hi = ht->ht_array; todo > 0; ++hi)
21772 {
21773 if (!HASHITEM_EMPTY(hi))
21774 {
21775 --todo;
21776
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 * ht_array might change then. hash_clear() takes care of it
21779 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 v = HI2DI(hi);
21781 if (free_val)
21782 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021783 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021784 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021785 }
21786 }
21787 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021788 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789}
21790
Bram Moolenaara7043832005-01-21 11:56:39 +000021791/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 * Delete a variable from hashtab "ht" at item "hi".
21793 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021796delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021797 hashtab_T *ht;
21798 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799{
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021801
21802 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 clear_tv(&di->di_tv);
21804 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805}
21806
21807/*
21808 * List the value of one internal variable.
21809 */
21810 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021811list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021814 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021816 char_u *tofree;
21817 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021819
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021820 current_copyID += COPYID_INC;
21821 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021822 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021823 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021824 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825}
21826
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021828list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 char_u *prefix;
21830 char_u *name;
21831 int type;
21832 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021833 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834{
Bram Moolenaar31859182007-08-14 20:41:13 +000021835 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21836 msg_start();
21837 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 if (name != NULL) /* "a:" vars don't have a name stored */
21839 msg_puts(name);
21840 msg_putchar(' ');
21841 msg_advance(22);
21842 if (type == VAR_NUMBER)
21843 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021844 else if (type == VAR_FUNC)
21845 msg_putchar('*');
21846 else if (type == VAR_LIST)
21847 {
21848 msg_putchar('[');
21849 if (*string == '[')
21850 ++string;
21851 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021852 else if (type == VAR_DICT)
21853 {
21854 msg_putchar('{');
21855 if (*string == '{')
21856 ++string;
21857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 else
21859 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021860
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021862
21863 if (type == VAR_FUNC)
21864 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021865 if (*first)
21866 {
21867 msg_clr_eos();
21868 *first = FALSE;
21869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870}
21871
21872/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021873 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 * If the variable already exists, the value is updated.
21875 * Otherwise the variable is created.
21876 */
21877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882{
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021887 ht = find_var_ht(name, &varname);
21888 if (ht == NULL || *varname == NUL)
21889 {
21890 EMSG2(_(e_illvar), name);
21891 return;
21892 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021893 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021894
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021895 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21896 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897
Bram Moolenaar33570922005-01-25 22:26:29 +000021898 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021901 if (var_check_ro(v->di_flags, name, FALSE)
21902 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 return;
21904 if (v->di_tv.v_type != tv->v_type
21905 && !((v->di_tv.v_type == VAR_STRING
21906 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021907 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021908 || tv->v_type == VAR_NUMBER))
21909#ifdef FEAT_FLOAT
21910 && !((v->di_tv.v_type == VAR_NUMBER
21911 || v->di_tv.v_type == VAR_FLOAT)
21912 && (tv->v_type == VAR_NUMBER
21913 || tv->v_type == VAR_FLOAT))
21914#endif
21915 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021917 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 return;
21919 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021920
21921 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021922 * Handle setting internal v: variables separately where needed to
21923 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 */
21925 if (ht == &vimvarht)
21926 {
21927 if (v->di_tv.v_type == VAR_STRING)
21928 {
21929 vim_free(v->di_tv.vval.v_string);
21930 if (copy || tv->v_type != VAR_STRING)
21931 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21932 else
21933 {
21934 /* Take over the string to avoid an extra alloc/free. */
21935 v->di_tv.vval.v_string = tv->vval.v_string;
21936 tv->vval.v_string = NULL;
21937 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021938 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021939 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021940 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021941 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021943 if (STRCMP(varname, "searchforward") == 0)
21944 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021945#ifdef FEAT_SEARCH_EXTRA
21946 else if (STRCMP(varname, "hlsearch") == 0)
21947 {
21948 no_hlsearch = !v->di_tv.vval.v_number;
21949 redraw_all_later(SOME_VALID);
21950 }
21951#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021952 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021953 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021954 else if (v->di_tv.v_type != tv->v_type)
21955 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021956 }
21957
21958 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 }
21960 else /* add a new variable */
21961 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021962 /* Can't add "v:" variable. */
21963 if (ht == &vimvarht)
21964 {
21965 EMSG2(_(e_illvar), name);
21966 return;
21967 }
21968
Bram Moolenaar92124a32005-06-17 22:03:40 +000021969 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021970 if (!valid_varname(varname))
21971 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021972
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021973 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21974 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021975 if (v == NULL)
21976 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021977 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021980 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 return;
21982 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021983 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021985
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021986 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021988 else
21989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021991 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994}
21995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021996/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021997 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021998 * Also give an error message.
21999 */
22000 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022001var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 int flags;
22003 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022004 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005{
22006 if (flags & DI_FLAGS_RO)
22007 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022008 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 return TRUE;
22010 }
22011 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22012 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022013 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 return TRUE;
22015 }
22016 return FALSE;
22017}
22018
22019/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022020 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22021 * Also give an error message.
22022 */
22023 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022024var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022025 int flags;
22026 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022027 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022028{
22029 if (flags & DI_FLAGS_FIX)
22030 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022031 EMSG2(_("E795: Cannot delete variable %s"),
22032 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022033 return TRUE;
22034 }
22035 return FALSE;
22036}
22037
22038/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022039 * Check if a funcref is assigned to a valid variable name.
22040 * Return TRUE and give an error if not.
22041 */
22042 static int
22043var_check_func_name(name, new_var)
22044 char_u *name; /* points to start of variable name */
22045 int new_var; /* TRUE when creating the variable */
22046{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022047 /* Allow for w: b: s: and t:. */
22048 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022049 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22050 ? name[2] : name[0]))
22051 {
22052 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22053 name);
22054 return TRUE;
22055 }
22056 /* Don't allow hiding a function. When "v" is not NULL we might be
22057 * assigning another function to the same var, the type is checked
22058 * below. */
22059 if (new_var && function_exists(name))
22060 {
22061 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22062 name);
22063 return TRUE;
22064 }
22065 return FALSE;
22066}
22067
22068/*
22069 * Check if a variable name is valid.
22070 * Return FALSE and give an error if not.
22071 */
22072 static int
22073valid_varname(varname)
22074 char_u *varname;
22075{
22076 char_u *p;
22077
22078 for (p = varname; *p != NUL; ++p)
22079 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22080 && *p != AUTOLOAD_CHAR)
22081 {
22082 EMSG2(_(e_illvar), varname);
22083 return FALSE;
22084 }
22085 return TRUE;
22086}
22087
22088/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022089 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022090 * Also give an error message, using "name" or _("name") when use_gettext is
22091 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022092 */
22093 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022094tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022095 int lock;
22096 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022097 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022098{
22099 if (lock & VAR_LOCKED)
22100 {
22101 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022102 name == NULL ? (char_u *)_("Unknown")
22103 : use_gettext ? (char_u *)_(name)
22104 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022105 return TRUE;
22106 }
22107 if (lock & VAR_FIXED)
22108 {
22109 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022110 name == NULL ? (char_u *)_("Unknown")
22111 : use_gettext ? (char_u *)_(name)
22112 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022113 return TRUE;
22114 }
22115 return FALSE;
22116}
22117
22118/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022120 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022121 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022122 * It is OK for "from" and "to" to point to the same item. This is used to
22123 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022124 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022125 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022126copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 typval_T *from;
22128 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022130 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022131 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022132 switch (from->v_type)
22133 {
22134 case VAR_NUMBER:
22135 to->vval.v_number = from->vval.v_number;
22136 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022137#ifdef FEAT_FLOAT
22138 case VAR_FLOAT:
22139 to->vval.v_float = from->vval.v_float;
22140 break;
22141#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022142 case VAR_STRING:
22143 case VAR_FUNC:
22144 if (from->vval.v_string == NULL)
22145 to->vval.v_string = NULL;
22146 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022147 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022148 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022149 if (from->v_type == VAR_FUNC)
22150 func_ref(to->vval.v_string);
22151 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022152 break;
22153 case VAR_LIST:
22154 if (from->vval.v_list == NULL)
22155 to->vval.v_list = NULL;
22156 else
22157 {
22158 to->vval.v_list = from->vval.v_list;
22159 ++to->vval.v_list->lv_refcount;
22160 }
22161 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022162 case VAR_DICT:
22163 if (from->vval.v_dict == NULL)
22164 to->vval.v_dict = NULL;
22165 else
22166 {
22167 to->vval.v_dict = from->vval.v_dict;
22168 ++to->vval.v_dict->dv_refcount;
22169 }
22170 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022171 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022172 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022173 break;
22174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175}
22176
22177/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022178 * Make a copy of an item.
22179 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022180 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22181 * reference to an already copied list/dict can be used.
22182 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022183 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022184 static int
22185item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 typval_T *from;
22187 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022189 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022190{
22191 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022192 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022193
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022195 {
22196 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022197 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022198 }
22199 ++recurse;
22200
22201 switch (from->v_type)
22202 {
22203 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022204#ifdef FEAT_FLOAT
22205 case VAR_FLOAT:
22206#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022207 case VAR_STRING:
22208 case VAR_FUNC:
22209 copy_tv(from, to);
22210 break;
22211 case VAR_LIST:
22212 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022213 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022214 if (from->vval.v_list == NULL)
22215 to->vval.v_list = NULL;
22216 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22217 {
22218 /* use the copy made earlier */
22219 to->vval.v_list = from->vval.v_list->lv_copylist;
22220 ++to->vval.v_list->lv_refcount;
22221 }
22222 else
22223 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22224 if (to->vval.v_list == NULL)
22225 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022226 break;
22227 case VAR_DICT:
22228 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022229 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022230 if (from->vval.v_dict == NULL)
22231 to->vval.v_dict = NULL;
22232 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22233 {
22234 /* use the copy made earlier */
22235 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22236 ++to->vval.v_dict->dv_refcount;
22237 }
22238 else
22239 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22240 if (to->vval.v_dict == NULL)
22241 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022242 break;
22243 default:
22244 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022245 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022246 }
22247 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022248 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022249}
22250
22251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 * ":echo expr1 ..." print each argument separated with a space, add a
22253 * newline at the end.
22254 * ":echon expr1 ..." print each argument plain.
22255 */
22256 void
22257ex_echo(eap)
22258 exarg_T *eap;
22259{
22260 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022261 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022262 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 char_u *p;
22264 int needclr = TRUE;
22265 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022266 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267
22268 if (eap->skip)
22269 ++emsg_skip;
22270 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22271 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022272 /* If eval1() causes an error message the text from the command may
22273 * still need to be cleared. E.g., "echo 22,44". */
22274 need_clr_eos = needclr;
22275
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022277 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 {
22279 /*
22280 * Report the invalid expression unless the expression evaluation
22281 * has been cancelled due to an aborting error, an interrupt, or an
22282 * exception.
22283 */
22284 if (!aborting())
22285 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022286 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 break;
22288 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022289 need_clr_eos = FALSE;
22290
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 if (!eap->skip)
22292 {
22293 if (atstart)
22294 {
22295 atstart = FALSE;
22296 /* Call msg_start() after eval1(), evaluating the expression
22297 * may cause a message to appear. */
22298 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022299 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022300 /* Mark the saved text as finishing the line, so that what
22301 * follows is displayed on a new line when scrolling back
22302 * at the more prompt. */
22303 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 }
22307 else if (eap->cmdidx == CMD_echo)
22308 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022309 current_copyID += COPYID_INC;
22310 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022311 if (p != NULL)
22312 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022314 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022316 if (*p != TAB && needclr)
22317 {
22318 /* remove any text still there from the command */
22319 msg_clr_eos();
22320 needclr = FALSE;
22321 }
22322 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 }
22324 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022325 {
22326#ifdef FEAT_MBYTE
22327 if (has_mbyte)
22328 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022329 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022330
22331 (void)msg_outtrans_len_attr(p, i, echo_attr);
22332 p += i - 1;
22333 }
22334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022336 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022341 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 arg = skipwhite(arg);
22343 }
22344 eap->nextcmd = check_nextcmd(arg);
22345
22346 if (eap->skip)
22347 --emsg_skip;
22348 else
22349 {
22350 /* remove text that may still be there from the command */
22351 if (needclr)
22352 msg_clr_eos();
22353 if (eap->cmdidx == CMD_echo)
22354 msg_end();
22355 }
22356}
22357
22358/*
22359 * ":echohl {name}".
22360 */
22361 void
22362ex_echohl(eap)
22363 exarg_T *eap;
22364{
22365 int id;
22366
22367 id = syn_name2id(eap->arg);
22368 if (id == 0)
22369 echo_attr = 0;
22370 else
22371 echo_attr = syn_id2attr(id);
22372}
22373
22374/*
22375 * ":execute expr1 ..." execute the result of an expression.
22376 * ":echomsg expr1 ..." Print a message
22377 * ":echoerr expr1 ..." Print an error
22378 * Each gets spaces around each argument and a newline at the end for
22379 * echo commands
22380 */
22381 void
22382ex_execute(eap)
22383 exarg_T *eap;
22384{
22385 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 int ret = OK;
22388 char_u *p;
22389 garray_T ga;
22390 int len;
22391 int save_did_emsg;
22392
22393 ga_init2(&ga, 1, 80);
22394
22395 if (eap->skip)
22396 ++emsg_skip;
22397 while (*arg != NUL && *arg != '|' && *arg != '\n')
22398 {
22399 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 {
22402 /*
22403 * Report the invalid expression unless the expression evaluation
22404 * has been cancelled due to an aborting error, an interrupt, or an
22405 * exception.
22406 */
22407 if (!aborting())
22408 EMSG2(_(e_invexpr2), p);
22409 ret = FAIL;
22410 break;
22411 }
22412
22413 if (!eap->skip)
22414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 len = (int)STRLEN(p);
22417 if (ga_grow(&ga, len + 2) == FAIL)
22418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 ret = FAIL;
22421 break;
22422 }
22423 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 ga.ga_len += len;
22427 }
22428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 arg = skipwhite(arg);
22431 }
22432
22433 if (ret != FAIL && ga.ga_data != NULL)
22434 {
22435 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022438 out_flush();
22439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 else if (eap->cmdidx == CMD_echoerr)
22441 {
22442 /* We don't want to abort following commands, restore did_emsg. */
22443 save_did_emsg = did_emsg;
22444 EMSG((char_u *)ga.ga_data);
22445 if (!force_abort)
22446 did_emsg = save_did_emsg;
22447 }
22448 else if (eap->cmdidx == CMD_execute)
22449 do_cmdline((char_u *)ga.ga_data,
22450 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22451 }
22452
22453 ga_clear(&ga);
22454
22455 if (eap->skip)
22456 --emsg_skip;
22457
22458 eap->nextcmd = check_nextcmd(arg);
22459}
22460
22461/*
22462 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22463 * "arg" points to the "&" or '+' when called, to "option" when returning.
22464 * Returns NULL when no option name found. Otherwise pointer to the char
22465 * after the option name.
22466 */
22467 static char_u *
22468find_option_end(arg, opt_flags)
22469 char_u **arg;
22470 int *opt_flags;
22471{
22472 char_u *p = *arg;
22473
22474 ++p;
22475 if (*p == 'g' && p[1] == ':')
22476 {
22477 *opt_flags = OPT_GLOBAL;
22478 p += 2;
22479 }
22480 else if (*p == 'l' && p[1] == ':')
22481 {
22482 *opt_flags = OPT_LOCAL;
22483 p += 2;
22484 }
22485 else
22486 *opt_flags = 0;
22487
22488 if (!ASCII_ISALPHA(*p))
22489 return NULL;
22490 *arg = p;
22491
22492 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22493 p += 4; /* termcap option */
22494 else
22495 while (ASCII_ISALPHA(*p))
22496 ++p;
22497 return p;
22498}
22499
22500/*
22501 * ":function"
22502 */
22503 void
22504ex_function(eap)
22505 exarg_T *eap;
22506{
22507 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022508 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 int j;
22510 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022512 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 char_u *name = NULL;
22514 char_u *p;
22515 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022516 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 garray_T newargs;
22518 garray_T newlines;
22519 int varargs = FALSE;
22520 int mustend = FALSE;
22521 int flags = 0;
22522 ufunc_T *fp;
22523 int indent;
22524 int nesting;
22525 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 dictitem_T *v;
22527 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022528 static int func_nr = 0; /* number for nameless function */
22529 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022530 hashtab_T *ht;
22531 int todo;
22532 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022533 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534
22535 /*
22536 * ":function" without argument: list functions.
22537 */
22538 if (ends_excmd(*eap->arg))
22539 {
22540 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022541 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022542 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022543 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 {
22545 if (!HASHITEM_EMPTY(hi))
22546 {
22547 --todo;
22548 fp = HI2UF(hi);
22549 if (!isdigit(*fp->uf_name))
22550 list_func_head(fp, FALSE);
22551 }
22552 }
22553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 eap->nextcmd = check_nextcmd(eap->arg);
22555 return;
22556 }
22557
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022558 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022559 * ":function /pat": list functions matching pattern.
22560 */
22561 if (*eap->arg == '/')
22562 {
22563 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22564 if (!eap->skip)
22565 {
22566 regmatch_T regmatch;
22567
22568 c = *p;
22569 *p = NUL;
22570 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22571 *p = c;
22572 if (regmatch.regprog != NULL)
22573 {
22574 regmatch.rm_ic = p_ic;
22575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022576 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022577 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22578 {
22579 if (!HASHITEM_EMPTY(hi))
22580 {
22581 --todo;
22582 fp = HI2UF(hi);
22583 if (!isdigit(*fp->uf_name)
22584 && vim_regexec(&regmatch, fp->uf_name, 0))
22585 list_func_head(fp, FALSE);
22586 }
22587 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022588 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022589 }
22590 }
22591 if (*p == '/')
22592 ++p;
22593 eap->nextcmd = check_nextcmd(p);
22594 return;
22595 }
22596
22597 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022598 * Get the function name. There are these situations:
22599 * func normal function name
22600 * "name" == func, "fudi.fd_dict" == NULL
22601 * dict.func new dictionary entry
22602 * "name" == NULL, "fudi.fd_dict" set,
22603 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22604 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022605 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022606 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22607 * dict.func existing dict entry that's not a Funcref
22608 * "name" == NULL, "fudi.fd_dict" set,
22609 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022610 * s:func script-local function name
22611 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022614 name = trans_function_name(&p, eap->skip, 0, &fudi);
22615 paren = (vim_strchr(p, '(') != NULL);
22616 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 {
22618 /*
22619 * Return on an invalid expression in braces, unless the expression
22620 * evaluation has been cancelled due to an aborting error, an
22621 * interrupt, or an exception.
22622 */
22623 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022625 if (!eap->skip && fudi.fd_newkey != NULL)
22626 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 else
22631 eap->skip = TRUE;
22632 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022633
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 /* An error in a function call during evaluation of an expression in magic
22635 * braces should not cause the function not to be defined. */
22636 saved_did_emsg = did_emsg;
22637 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638
22639 /*
22640 * ":function func" with only function name: list function.
22641 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022642 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 {
22644 if (!ends_excmd(*skipwhite(p)))
22645 {
22646 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022647 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 }
22649 eap->nextcmd = check_nextcmd(p);
22650 if (eap->nextcmd != NULL)
22651 *p = NUL;
22652 if (!eap->skip && !got_int)
22653 {
22654 fp = find_func(name);
22655 if (fp != NULL)
22656 {
22657 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022658 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022660 if (FUNCLINE(fp, j) == NULL)
22661 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 msg_putchar('\n');
22663 msg_outnum((long)(j + 1));
22664 if (j < 9)
22665 msg_putchar(' ');
22666 if (j < 99)
22667 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022668 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 out_flush(); /* show a line at a time */
22670 ui_breakcheck();
22671 }
22672 if (!got_int)
22673 {
22674 msg_putchar('\n');
22675 msg_puts((char_u *)" endfunction");
22676 }
22677 }
22678 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022679 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022681 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 }
22683
22684 /*
22685 * ":function name(arg1, arg2)" Define function.
22686 */
22687 p = skipwhite(p);
22688 if (*p != '(')
22689 {
22690 if (!eap->skip)
22691 {
22692 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022693 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 }
22695 /* attempt to continue by skipping some text */
22696 if (vim_strchr(p, '(') != NULL)
22697 p = vim_strchr(p, '(');
22698 }
22699 p = skipwhite(p + 1);
22700
22701 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22702 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22703
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022704 if (!eap->skip)
22705 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022706 /* Check the name of the function. Unless it's a dictionary function
22707 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022708 if (name != NULL)
22709 arg = name;
22710 else
22711 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022712 if (arg != NULL && (fudi.fd_di == NULL
22713 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022714 {
22715 if (*arg == K_SPECIAL)
22716 j = 3;
22717 else
22718 j = 0;
22719 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22720 : eval_isnamec(arg[j])))
22721 ++j;
22722 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022723 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022724 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022725 /* Disallow using the g: dict. */
22726 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22727 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022728 }
22729
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 /*
22731 * Isolate the arguments: "arg1, arg2, ...)"
22732 */
22733 while (*p != ')')
22734 {
22735 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22736 {
22737 varargs = TRUE;
22738 p += 3;
22739 mustend = TRUE;
22740 }
22741 else
22742 {
22743 arg = p;
22744 while (ASCII_ISALNUM(*p) || *p == '_')
22745 ++p;
22746 if (arg == p || isdigit(*arg)
22747 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22748 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22749 {
22750 if (!eap->skip)
22751 EMSG2(_("E125: Illegal argument: %s"), arg);
22752 break;
22753 }
22754 if (ga_grow(&newargs, 1) == FAIL)
22755 goto erret;
22756 c = *p;
22757 *p = NUL;
22758 arg = vim_strsave(arg);
22759 if (arg == NULL)
22760 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022761
22762 /* Check for duplicate argument name. */
22763 for (i = 0; i < newargs.ga_len; ++i)
22764 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22765 {
22766 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022767 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022768 goto erret;
22769 }
22770
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22772 *p = c;
22773 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 if (*p == ',')
22775 ++p;
22776 else
22777 mustend = TRUE;
22778 }
22779 p = skipwhite(p);
22780 if (mustend && *p != ')')
22781 {
22782 if (!eap->skip)
22783 EMSG2(_(e_invarg2), eap->arg);
22784 break;
22785 }
22786 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022787 if (*p != ')')
22788 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 ++p; /* skip the ')' */
22790
Bram Moolenaare9a41262005-01-15 22:18:47 +000022791 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 for (;;)
22793 {
22794 p = skipwhite(p);
22795 if (STRNCMP(p, "range", 5) == 0)
22796 {
22797 flags |= FC_RANGE;
22798 p += 5;
22799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022800 else if (STRNCMP(p, "dict", 4) == 0)
22801 {
22802 flags |= FC_DICT;
22803 p += 4;
22804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 else if (STRNCMP(p, "abort", 5) == 0)
22806 {
22807 flags |= FC_ABORT;
22808 p += 5;
22809 }
22810 else
22811 break;
22812 }
22813
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022814 /* When there is a line break use what follows for the function body.
22815 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22816 if (*p == '\n')
22817 line_arg = p + 1;
22818 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 EMSG(_(e_trailing));
22820
22821 /*
22822 * Read the body of the function, until ":endfunction" is found.
22823 */
22824 if (KeyTyped)
22825 {
22826 /* Check if the function already exists, don't let the user type the
22827 * whole function before telling him it doesn't work! For a script we
22828 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 if (!eap->skip && !eap->forceit)
22830 {
22831 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22832 EMSG(_(e_funcdict));
22833 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022834 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022837 if (!eap->skip && did_emsg)
22838 goto erret;
22839
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 msg_putchar('\n'); /* don't overwrite the function name */
22841 cmdline_row = msg_row;
22842 }
22843
22844 indent = 2;
22845 nesting = 0;
22846 for (;;)
22847 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022848 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022849 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022850 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022851 saved_wait_return = FALSE;
22852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022854 sourcing_lnum_off = sourcing_lnum;
22855
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022856 if (line_arg != NULL)
22857 {
22858 /* Use eap->arg, split up in parts by line breaks. */
22859 theline = line_arg;
22860 p = vim_strchr(theline, '\n');
22861 if (p == NULL)
22862 line_arg += STRLEN(line_arg);
22863 else
22864 {
22865 *p = NUL;
22866 line_arg = p + 1;
22867 }
22868 }
22869 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 theline = getcmdline(':', 0L, indent);
22871 else
22872 theline = eap->getline(':', eap->cookie, indent);
22873 if (KeyTyped)
22874 lines_left = Rows - 1;
22875 if (theline == NULL)
22876 {
22877 EMSG(_("E126: Missing :endfunction"));
22878 goto erret;
22879 }
22880
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022881 /* Detect line continuation: sourcing_lnum increased more than one. */
22882 if (sourcing_lnum > sourcing_lnum_off + 1)
22883 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22884 else
22885 sourcing_lnum_off = 0;
22886
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 if (skip_until != NULL)
22888 {
22889 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22890 * don't check for ":endfunc". */
22891 if (STRCMP(theline, skip_until) == 0)
22892 {
22893 vim_free(skip_until);
22894 skip_until = NULL;
22895 }
22896 }
22897 else
22898 {
22899 /* skip ':' and blanks*/
22900 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22901 ;
22902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022903 /* Check for "endfunction". */
22904 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022906 if (line_arg == NULL)
22907 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 break;
22909 }
22910
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022911 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 * at "end". */
22913 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22914 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022915 else if (STRNCMP(p, "if", 2) == 0
22916 || STRNCMP(p, "wh", 2) == 0
22917 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 || STRNCMP(p, "try", 3) == 0)
22919 indent += 2;
22920
22921 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022922 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022924 if (*p == '!')
22925 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022927 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22928 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022930 ++nesting;
22931 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 }
22933 }
22934
22935 /* Check for ":append" or ":insert". */
22936 p = skip_range(p, NULL);
22937 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22938 || (p[0] == 'i'
22939 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22940 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22941 skip_until = vim_strsave((char_u *)".");
22942
22943 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22944 arg = skipwhite(skiptowhite(p));
22945 if (arg[0] == '<' && arg[1] =='<'
22946 && ((p[0] == 'p' && p[1] == 'y'
22947 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22948 || (p[0] == 'p' && p[1] == 'e'
22949 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22950 || (p[0] == 't' && p[1] == 'c'
22951 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022952 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22953 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22955 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022956 || (p[0] == 'm' && p[1] == 'z'
22957 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 ))
22959 {
22960 /* ":python <<" continues until a dot, like ":append" */
22961 p = skipwhite(arg + 2);
22962 if (*p == NUL)
22963 skip_until = vim_strsave((char_u *)".");
22964 else
22965 skip_until = vim_strsave(p);
22966 }
22967 }
22968
22969 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022970 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022971 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022972 if (line_arg == NULL)
22973 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022975 }
22976
22977 /* Copy the line to newly allocated memory. get_one_sourceline()
22978 * allocates 250 bytes per line, this saves 80% on average. The cost
22979 * is an extra alloc/free. */
22980 p = vim_strsave(theline);
22981 if (p != NULL)
22982 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022983 if (line_arg == NULL)
22984 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022985 theline = p;
22986 }
22987
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022988 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22989
22990 /* Add NULL lines for continuation lines, so that the line count is
22991 * equal to the index in the growarray. */
22992 while (sourcing_lnum_off-- > 0)
22993 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022994
22995 /* Check for end of eap->arg. */
22996 if (line_arg != NULL && *line_arg == NUL)
22997 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 }
22999
23000 /* Don't define the function when skipping commands or when an error was
23001 * detected. */
23002 if (eap->skip || did_emsg)
23003 goto erret;
23004
23005 /*
23006 * If there are no errors, add the function
23007 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023008 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023009 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023010 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023012 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023013 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023014 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023015 goto erret;
23016 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023018 fp = find_func(name);
23019 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023021 if (!eap->forceit)
23022 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023023 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023024 goto erret;
23025 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023026 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023027 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023028 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023029 name);
23030 goto erret;
23031 }
23032 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023033 ga_clear_strings(&(fp->uf_args));
23034 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023035 vim_free(name);
23036 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
23039 else
23040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023041 char numbuf[20];
23042
23043 fp = NULL;
23044 if (fudi.fd_newkey == NULL && !eap->forceit)
23045 {
23046 EMSG(_(e_funcdict));
23047 goto erret;
23048 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023049 if (fudi.fd_di == NULL)
23050 {
23051 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023052 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023053 goto erret;
23054 }
23055 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023056 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023057 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023058
23059 /* Give the function a sequential number. Can only be used with a
23060 * Funcref! */
23061 vim_free(name);
23062 sprintf(numbuf, "%d", ++func_nr);
23063 name = vim_strsave((char_u *)numbuf);
23064 if (name == NULL)
23065 goto erret;
23066 }
23067
23068 if (fp == NULL)
23069 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023070 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023071 {
23072 int slen, plen;
23073 char_u *scriptname;
23074
23075 /* Check that the autoload name matches the script name. */
23076 j = FAIL;
23077 if (sourcing_name != NULL)
23078 {
23079 scriptname = autoload_name(name);
23080 if (scriptname != NULL)
23081 {
23082 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023083 plen = (int)STRLEN(p);
23084 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023085 if (slen > plen && fnamecmp(p,
23086 sourcing_name + slen - plen) == 0)
23087 j = OK;
23088 vim_free(scriptname);
23089 }
23090 }
23091 if (j == FAIL)
23092 {
23093 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23094 goto erret;
23095 }
23096 }
23097
23098 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 if (fp == NULL)
23100 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023101
23102 if (fudi.fd_dict != NULL)
23103 {
23104 if (fudi.fd_di == NULL)
23105 {
23106 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023107 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023108 if (fudi.fd_di == NULL)
23109 {
23110 vim_free(fp);
23111 goto erret;
23112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023113 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23114 {
23115 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023116 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023117 goto erret;
23118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 }
23120 else
23121 /* overwrite existing dict entry */
23122 clear_tv(&fudi.fd_di->di_tv);
23123 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023124 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023125 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023126 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023127
23128 /* behave like "dict" was used */
23129 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023130 }
23131
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023133 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023134 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23135 {
23136 vim_free(fp);
23137 goto erret;
23138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023140 fp->uf_args = newargs;
23141 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023142#ifdef FEAT_PROFILE
23143 fp->uf_tml_count = NULL;
23144 fp->uf_tml_total = NULL;
23145 fp->uf_tml_self = NULL;
23146 fp->uf_profiling = FALSE;
23147 if (prof_def_func())
23148 func_do_profile(fp);
23149#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 fp->uf_varargs = varargs;
23151 fp->uf_flags = flags;
23152 fp->uf_calls = 0;
23153 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023154 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155
23156erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 ga_clear_strings(&newargs);
23158 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023159ret_free:
23160 vim_free(skip_until);
23161 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023164 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165}
23166
23167/*
23168 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023169 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023171 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023172 * TFN_INT: internal function name OK
23173 * TFN_QUIET: be quiet
23174 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 * Advances "pp" to just after the function name (if no error).
23176 */
23177 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023178trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 char_u **pp;
23180 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023184 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 char_u *start;
23186 char_u *end;
23187 int lead;
23188 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023191
23192 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023195
23196 /* Check for hard coded <SNR>: already translated function ID (from a user
23197 * command). */
23198 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23199 && (*pp)[2] == (int)KE_SNR)
23200 {
23201 *pp += 3;
23202 len = get_id_len(pp) + 3;
23203 return vim_strnsave(start, len);
23204 }
23205
23206 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23207 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023209 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023211
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023212 /* Note that TFN_ flags use the same values as GLV_ flags. */
23213 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023214 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023215 if (end == start)
23216 {
23217 if (!skip)
23218 EMSG(_("E129: Function name required"));
23219 goto theend;
23220 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023221 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023222 {
23223 /*
23224 * Report an invalid expression in braces, unless the expression
23225 * evaluation has been cancelled due to an aborting error, an
23226 * interrupt, or an exception.
23227 */
23228 if (!aborting())
23229 {
23230 if (end != NULL)
23231 EMSG2(_(e_invarg2), start);
23232 }
23233 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023234 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023235 goto theend;
23236 }
23237
23238 if (lv.ll_tv != NULL)
23239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023240 if (fdp != NULL)
23241 {
23242 fdp->fd_dict = lv.ll_dict;
23243 fdp->fd_newkey = lv.ll_newkey;
23244 lv.ll_newkey = NULL;
23245 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023247 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23248 {
23249 name = vim_strsave(lv.ll_tv->vval.v_string);
23250 *pp = end;
23251 }
23252 else
23253 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023254 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23255 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023256 EMSG(_(e_funcref));
23257 else
23258 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023259 name = NULL;
23260 }
23261 goto theend;
23262 }
23263
23264 if (lv.ll_name == NULL)
23265 {
23266 /* Error found, but continue after the function name. */
23267 *pp = end;
23268 goto theend;
23269 }
23270
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023271 /* Check if the name is a Funcref. If so, use the value. */
23272 if (lv.ll_exp_name != NULL)
23273 {
23274 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023275 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023276 if (name == lv.ll_exp_name)
23277 name = NULL;
23278 }
23279 else
23280 {
23281 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023282 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023283 if (name == *pp)
23284 name = NULL;
23285 }
23286 if (name != NULL)
23287 {
23288 name = vim_strsave(name);
23289 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023290 if (STRNCMP(name, "<SNR>", 5) == 0)
23291 {
23292 /* Change "<SNR>" to the byte sequence. */
23293 name[0] = K_SPECIAL;
23294 name[1] = KS_EXTRA;
23295 name[2] = (int)KE_SNR;
23296 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23297 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023298 goto theend;
23299 }
23300
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023301 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023302 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023303 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023304 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23305 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23306 {
23307 /* When there was "s:" already or the name expanded to get a
23308 * leading "s:" then remove it. */
23309 lv.ll_name += 2;
23310 len -= 2;
23311 lead = 2;
23312 }
23313 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023314 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023315 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023316 /* skip over "s:" and "g:" */
23317 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023318 lv.ll_name += 2;
23319 len = (int)(end - lv.ll_name);
23320 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023321
23322 /*
23323 * Copy the function name to allocated memory.
23324 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23325 * Accept <SNR>123_name() outside a script.
23326 */
23327 if (skip)
23328 lead = 0; /* do nothing */
23329 else if (lead > 0)
23330 {
23331 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023332 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23333 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023334 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023335 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023336 if (current_SID <= 0)
23337 {
23338 EMSG(_(e_usingsid));
23339 goto theend;
23340 }
23341 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23342 lead += (int)STRLEN(sid_buf);
23343 }
23344 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023345 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023346 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023347 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023348 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023349 goto theend;
23350 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023351 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023352 {
23353 char_u *cp = vim_strchr(lv.ll_name, ':');
23354
23355 if (cp != NULL && cp < end)
23356 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023357 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023358 goto theend;
23359 }
23360 }
23361
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023362 name = alloc((unsigned)(len + lead + 1));
23363 if (name != NULL)
23364 {
23365 if (lead > 0)
23366 {
23367 name[0] = K_SPECIAL;
23368 name[1] = KS_EXTRA;
23369 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023370 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023371 STRCPY(name + 3, sid_buf);
23372 }
23373 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023374 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023375 }
23376 *pp = end;
23377
23378theend:
23379 clear_lval(&lv);
23380 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381}
23382
23383/*
23384 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23385 * Return 2 if "p" starts with "s:".
23386 * Return 0 otherwise.
23387 */
23388 static int
23389eval_fname_script(p)
23390 char_u *p;
23391{
23392 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23393 || STRNICMP(p + 1, "SNR>", 4) == 0))
23394 return 5;
23395 if (p[0] == 's' && p[1] == ':')
23396 return 2;
23397 return 0;
23398}
23399
23400/*
23401 * Return TRUE if "p" starts with "<SID>" or "s:".
23402 * Only works if eval_fname_script() returned non-zero for "p"!
23403 */
23404 static int
23405eval_fname_sid(p)
23406 char_u *p;
23407{
23408 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23409}
23410
23411/*
23412 * List the head of the function: "name(arg1, arg2)".
23413 */
23414 static void
23415list_func_head(fp, indent)
23416 ufunc_T *fp;
23417 int indent;
23418{
23419 int j;
23420
23421 msg_start();
23422 if (indent)
23423 MSG_PUTS(" ");
23424 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023425 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 {
23427 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023428 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 }
23430 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023431 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023433 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 {
23435 if (j)
23436 MSG_PUTS(", ");
23437 msg_puts(FUNCARG(fp, j));
23438 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023439 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 {
23441 if (j)
23442 MSG_PUTS(", ");
23443 MSG_PUTS("...");
23444 }
23445 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023446 if (fp->uf_flags & FC_ABORT)
23447 MSG_PUTS(" abort");
23448 if (fp->uf_flags & FC_RANGE)
23449 MSG_PUTS(" range");
23450 if (fp->uf_flags & FC_DICT)
23451 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023452 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023453 if (p_verbose > 0)
23454 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455}
23456
23457/*
23458 * Find a function by name, return pointer to it in ufuncs.
23459 * Return NULL for unknown function.
23460 */
23461 static ufunc_T *
23462find_func(name)
23463 char_u *name;
23464{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023467 hi = hash_find(&func_hashtab, name);
23468 if (!HASHITEM_EMPTY(hi))
23469 return HI2UF(hi);
23470 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471}
23472
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023473#if defined(EXITFREE) || defined(PROTO)
23474 void
23475free_all_functions()
23476{
23477 hashitem_T *hi;
23478
23479 /* Need to start all over every time, because func_free() may change the
23480 * hash table. */
23481 while (func_hashtab.ht_used > 0)
23482 for (hi = func_hashtab.ht_array; ; ++hi)
23483 if (!HASHITEM_EMPTY(hi))
23484 {
23485 func_free(HI2UF(hi));
23486 break;
23487 }
23488}
23489#endif
23490
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023491 int
23492translated_function_exists(name)
23493 char_u *name;
23494{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023495 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023496 return find_internal_func(name) >= 0;
23497 return find_func(name) != NULL;
23498}
23499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023500/*
23501 * Return TRUE if a function "name" exists.
23502 */
23503 static int
23504function_exists(name)
23505 char_u *name;
23506{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023507 char_u *nm = name;
23508 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023509 int n = FALSE;
23510
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023511 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23512 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023513 nm = skipwhite(nm);
23514
23515 /* Only accept "funcname", "funcname ", "funcname (..." and
23516 * "funcname(...", not "funcname!...". */
23517 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023518 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023519 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023520 return n;
23521}
23522
Bram Moolenaara1544c02013-05-30 12:35:52 +020023523 char_u *
23524get_expanded_name(name, check)
23525 char_u *name;
23526 int check;
23527{
23528 char_u *nm = name;
23529 char_u *p;
23530
23531 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23532
23533 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023534 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023535 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023536
Bram Moolenaara1544c02013-05-30 12:35:52 +020023537 vim_free(p);
23538 return NULL;
23539}
23540
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023541/*
23542 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023543 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23544 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023545 */
23546 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023547builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023548 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023549 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023550{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023551 char_u *p;
23552
23553 if (!ASCII_ISLOWER(name[0]))
23554 return FALSE;
23555 p = vim_strchr(name, AUTOLOAD_CHAR);
23556 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023557}
23558
Bram Moolenaar05159a02005-02-26 23:04:13 +000023559#if defined(FEAT_PROFILE) || defined(PROTO)
23560/*
23561 * Start profiling function "fp".
23562 */
23563 static void
23564func_do_profile(fp)
23565 ufunc_T *fp;
23566{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023567 int len = fp->uf_lines.ga_len;
23568
23569 if (len == 0)
23570 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023571 fp->uf_tm_count = 0;
23572 profile_zero(&fp->uf_tm_self);
23573 profile_zero(&fp->uf_tm_total);
23574 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023575 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023576 if (fp->uf_tml_total == NULL)
23577 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023578 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023579 if (fp->uf_tml_self == NULL)
23580 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023581 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023582 fp->uf_tml_idx = -1;
23583 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23584 || fp->uf_tml_self == NULL)
23585 return; /* out of memory */
23586
23587 fp->uf_profiling = TRUE;
23588}
23589
23590/*
23591 * Dump the profiling results for all functions in file "fd".
23592 */
23593 void
23594func_dump_profile(fd)
23595 FILE *fd;
23596{
23597 hashitem_T *hi;
23598 int todo;
23599 ufunc_T *fp;
23600 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023601 ufunc_T **sorttab;
23602 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023604 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023605 if (todo == 0)
23606 return; /* nothing to dump */
23607
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023608 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023609
Bram Moolenaar05159a02005-02-26 23:04:13 +000023610 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23611 {
23612 if (!HASHITEM_EMPTY(hi))
23613 {
23614 --todo;
23615 fp = HI2UF(hi);
23616 if (fp->uf_profiling)
23617 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023618 if (sorttab != NULL)
23619 sorttab[st_len++] = fp;
23620
Bram Moolenaar05159a02005-02-26 23:04:13 +000023621 if (fp->uf_name[0] == K_SPECIAL)
23622 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23623 else
23624 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23625 if (fp->uf_tm_count == 1)
23626 fprintf(fd, "Called 1 time\n");
23627 else
23628 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23629 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23630 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23631 fprintf(fd, "\n");
23632 fprintf(fd, "count total (s) self (s)\n");
23633
23634 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23635 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023636 if (FUNCLINE(fp, i) == NULL)
23637 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023638 prof_func_line(fd, fp->uf_tml_count[i],
23639 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023640 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23641 }
23642 fprintf(fd, "\n");
23643 }
23644 }
23645 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023646
23647 if (sorttab != NULL && st_len > 0)
23648 {
23649 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23650 prof_total_cmp);
23651 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23652 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23653 prof_self_cmp);
23654 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23655 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023656
23657 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023658}
Bram Moolenaar73830342005-02-28 22:48:19 +000023659
23660 static void
23661prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23662 FILE *fd;
23663 ufunc_T **sorttab;
23664 int st_len;
23665 char *title;
23666 int prefer_self; /* when equal print only self time */
23667{
23668 int i;
23669 ufunc_T *fp;
23670
23671 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23672 fprintf(fd, "count total (s) self (s) function\n");
23673 for (i = 0; i < 20 && i < st_len; ++i)
23674 {
23675 fp = sorttab[i];
23676 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23677 prefer_self);
23678 if (fp->uf_name[0] == K_SPECIAL)
23679 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23680 else
23681 fprintf(fd, " %s()\n", fp->uf_name);
23682 }
23683 fprintf(fd, "\n");
23684}
23685
23686/*
23687 * Print the count and times for one function or function line.
23688 */
23689 static void
23690prof_func_line(fd, count, total, self, prefer_self)
23691 FILE *fd;
23692 int count;
23693 proftime_T *total;
23694 proftime_T *self;
23695 int prefer_self; /* when equal print only self time */
23696{
23697 if (count > 0)
23698 {
23699 fprintf(fd, "%5d ", count);
23700 if (prefer_self && profile_equal(total, self))
23701 fprintf(fd, " ");
23702 else
23703 fprintf(fd, "%s ", profile_msg(total));
23704 if (!prefer_self && profile_equal(total, self))
23705 fprintf(fd, " ");
23706 else
23707 fprintf(fd, "%s ", profile_msg(self));
23708 }
23709 else
23710 fprintf(fd, " ");
23711}
23712
23713/*
23714 * Compare function for total time sorting.
23715 */
23716 static int
23717#ifdef __BORLANDC__
23718_RTLENTRYF
23719#endif
23720prof_total_cmp(s1, s2)
23721 const void *s1;
23722 const void *s2;
23723{
23724 ufunc_T *p1, *p2;
23725
23726 p1 = *(ufunc_T **)s1;
23727 p2 = *(ufunc_T **)s2;
23728 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23729}
23730
23731/*
23732 * Compare function for self time sorting.
23733 */
23734 static int
23735#ifdef __BORLANDC__
23736_RTLENTRYF
23737#endif
23738prof_self_cmp(s1, s2)
23739 const void *s1;
23740 const void *s2;
23741{
23742 ufunc_T *p1, *p2;
23743
23744 p1 = *(ufunc_T **)s1;
23745 p2 = *(ufunc_T **)s2;
23746 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23747}
23748
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749#endif
23750
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023751/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023752 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023753 * Return TRUE if a package was loaded.
23754 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023755 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023756script_autoload(name, reload)
23757 char_u *name;
23758 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023759{
23760 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023761 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023762 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023763 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023764
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023765 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023766 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023767 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023768 return FALSE;
23769
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023770 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023771
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023772 /* Find the name in the list of previously loaded package names. Skip
23773 * "autoload/", it's always the same. */
23774 for (i = 0; i < ga_loaded.ga_len; ++i)
23775 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23776 break;
23777 if (!reload && i < ga_loaded.ga_len)
23778 ret = FALSE; /* was loaded already */
23779 else
23780 {
23781 /* Remember the name if it wasn't loaded already. */
23782 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23783 {
23784 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23785 tofree = NULL;
23786 }
23787
23788 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023789 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023790 ret = TRUE;
23791 }
23792
23793 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023794 return ret;
23795}
23796
23797/*
23798 * Return the autoload script name for a function or variable name.
23799 * Returns NULL when out of memory.
23800 */
23801 static char_u *
23802autoload_name(name)
23803 char_u *name;
23804{
23805 char_u *p;
23806 char_u *scriptname;
23807
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023808 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023809 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23810 if (scriptname == NULL)
23811 return FALSE;
23812 STRCPY(scriptname, "autoload/");
23813 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023814 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023815 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023816 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023817 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023819}
23820
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23822
23823/*
23824 * Function given to ExpandGeneric() to obtain the list of user defined
23825 * function names.
23826 */
23827 char_u *
23828get_user_func_name(xp, idx)
23829 expand_T *xp;
23830 int idx;
23831{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023832 static long_u done;
23833 static hashitem_T *hi;
23834 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835
23836 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023838 done = 0;
23839 hi = func_hashtab.ht_array;
23840 }
23841 if (done < func_hashtab.ht_used)
23842 {
23843 if (done++ > 0)
23844 ++hi;
23845 while (HASHITEM_EMPTY(hi))
23846 ++hi;
23847 fp = HI2UF(hi);
23848
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023849 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023850 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023851
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023852 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23853 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854
23855 cat_func_name(IObuff, fp);
23856 if (xp->xp_context != EXPAND_USER_FUNC)
23857 {
23858 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023859 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 STRCAT(IObuff, ")");
23861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 return IObuff;
23863 }
23864 return NULL;
23865}
23866
23867#endif /* FEAT_CMDL_COMPL */
23868
23869/*
23870 * Copy the function name of "fp" to buffer "buf".
23871 * "buf" must be able to hold the function name plus three bytes.
23872 * Takes care of script-local function names.
23873 */
23874 static void
23875cat_func_name(buf, fp)
23876 char_u *buf;
23877 ufunc_T *fp;
23878{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023879 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 {
23881 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 }
23884 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886}
23887
23888/*
23889 * ":delfunction {name}"
23890 */
23891 void
23892ex_delfunction(eap)
23893 exarg_T *eap;
23894{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023895 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 char_u *p;
23897 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023898 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899
23900 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023901 name = trans_function_name(&p, eap->skip, 0, &fudi);
23902 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904 {
23905 if (fudi.fd_dict != NULL && !eap->skip)
23906 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 if (!ends_excmd(*skipwhite(p)))
23910 {
23911 vim_free(name);
23912 EMSG(_(e_trailing));
23913 return;
23914 }
23915 eap->nextcmd = check_nextcmd(p);
23916 if (eap->nextcmd != NULL)
23917 *p = NUL;
23918
23919 if (!eap->skip)
23920 fp = find_func(name);
23921 vim_free(name);
23922
23923 if (!eap->skip)
23924 {
23925 if (fp == NULL)
23926 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023927 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 return;
23929 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023930 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 {
23932 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23933 return;
23934 }
23935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023936 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023938 /* Delete the dict item that refers to the function, it will
23939 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023940 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942 else
23943 func_free(fp);
23944 }
23945}
23946
23947/*
23948 * Free a function and remove it from the list of functions.
23949 */
23950 static void
23951func_free(fp)
23952 ufunc_T *fp;
23953{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023954 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023955
23956 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 ga_clear_strings(&(fp->uf_args));
23958 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023959#ifdef FEAT_PROFILE
23960 vim_free(fp->uf_tml_count);
23961 vim_free(fp->uf_tml_total);
23962 vim_free(fp->uf_tml_self);
23963#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023964
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023965 /* remove the function from the function hashtable */
23966 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23967 if (HASHITEM_EMPTY(hi))
23968 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023969 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023970 hash_remove(&func_hashtab, hi);
23971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 vim_free(fp);
23973}
23974
23975/*
23976 * Unreference a Function: decrement the reference count and free it when it
23977 * becomes zero. Only for numbered functions.
23978 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023979 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023980func_unref(name)
23981 char_u *name;
23982{
23983 ufunc_T *fp;
23984
23985 if (name != NULL && isdigit(*name))
23986 {
23987 fp = find_func(name);
23988 if (fp == NULL)
23989 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023991 {
23992 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 * when "uf_calls" becomes zero. */
23994 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 func_free(fp);
23996 }
23997 }
23998}
23999
24000/*
24001 * Count a reference to a Function.
24002 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024003 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004func_ref(name)
24005 char_u *name;
24006{
24007 ufunc_T *fp;
24008
24009 if (name != NULL && isdigit(*name))
24010 {
24011 fp = find_func(name);
24012 if (fp == NULL)
24013 EMSG2(_(e_intern2), "func_ref()");
24014 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024015 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 }
24017}
24018
24019/*
24020 * Call a user function.
24021 */
24022 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024023call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 ufunc_T *fp; /* pointer to function */
24025 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024026 typval_T *argvars; /* arguments */
24027 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 linenr_T firstline; /* first line of range */
24029 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024030 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031{
Bram Moolenaar33570922005-01-25 22:26:29 +000024032 char_u *save_sourcing_name;
24033 linenr_T save_sourcing_lnum;
24034 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024035 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024036 int save_did_emsg;
24037 static int depth = 0;
24038 dictitem_T *v;
24039 int fixvar_idx = 0; /* index in fixvar[] */
24040 int i;
24041 int ai;
24042 char_u numbuf[NUMBUFLEN];
24043 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024044 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024045#ifdef FEAT_PROFILE
24046 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024047 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049
24050 /* If depth of calling is getting too high, don't execute the function */
24051 if (depth >= p_mfd)
24052 {
24053 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024054 rettv->v_type = VAR_NUMBER;
24055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 return;
24057 }
24058 ++depth;
24059
24060 line_breakcheck(); /* check for CTRL-C hit */
24061
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024062 fc = (funccall_T *)alloc(sizeof(funccall_T));
24063 fc->caller = current_funccal;
24064 current_funccal = fc;
24065 fc->func = fp;
24066 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024067 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024068 fc->linenr = 0;
24069 fc->returned = FALSE;
24070 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024072 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24073 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074
Bram Moolenaar33570922005-01-25 22:26:29 +000024075 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024076 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024077 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24078 * each argument variable and saves a lot of time.
24079 */
24080 /*
24081 * Init l: variables.
24082 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024083 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024084 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024085 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024086 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24087 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024088 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024089 name = v->di_key;
24090 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024091 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024092 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024094 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024095 v->di_tv.vval.v_dict = selfdict;
24096 ++selfdict->dv_refcount;
24097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024098
Bram Moolenaar33570922005-01-25 22:26:29 +000024099 /*
24100 * Init a: variables.
24101 * Set a:0 to "argcount".
24102 * Set a:000 to a list with room for the "..." arguments.
24103 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024104 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024105 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024106 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024107 /* Use "name" to avoid a warning from some compiler that checks the
24108 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024109 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024110 name = v->di_key;
24111 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024112 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024113 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024114 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024115 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024116 v->di_tv.vval.v_list = &fc->l_varlist;
24117 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24118 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24119 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024120
24121 /*
24122 * Set a:firstline to "firstline" and a:lastline to "lastline".
24123 * Set a:name to named arguments.
24124 * Set a:N to the "..." arguments.
24125 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024126 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024127 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024128 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024129 (varnumber_T)lastline);
24130 for (i = 0; i < argcount; ++i)
24131 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024133 if (ai < 0)
24134 /* named argument a:name */
24135 name = FUNCARG(fp, i);
24136 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024137 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024138 /* "..." argument a:1, a:2, etc. */
24139 sprintf((char *)numbuf, "%d", ai + 1);
24140 name = numbuf;
24141 }
24142 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24143 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024144 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024145 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24146 }
24147 else
24148 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024149 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24150 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024151 if (v == NULL)
24152 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024153 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024154 }
24155 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024156 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024157
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024158 /* Note: the values are copied directly to avoid alloc/free.
24159 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024160 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024161 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024162
24163 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24164 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024165 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24166 fc->l_listitems[ai].li_tv = argvars[i];
24167 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024168 }
24169 }
24170
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 /* Don't redraw while executing the function. */
24172 ++RedrawingDisabled;
24173 save_sourcing_name = sourcing_name;
24174 save_sourcing_lnum = sourcing_lnum;
24175 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024176 /* need space for function name + ("function " + 3) or "[number]" */
24177 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24178 + STRLEN(fp->uf_name) + 20;
24179 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 if (sourcing_name != NULL)
24181 {
24182 if (save_sourcing_name != NULL
24183 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024184 sprintf((char *)sourcing_name, "%s[%d]..",
24185 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 else
24187 STRCPY(sourcing_name, "function ");
24188 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24189
24190 if (p_verbose >= 12)
24191 {
24192 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024193 verbose_enter_scroll();
24194
Bram Moolenaar555b2802005-05-19 21:08:39 +000024195 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 if (p_verbose >= 14)
24197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024199 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024200 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024201 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202
24203 msg_puts((char_u *)"(");
24204 for (i = 0; i < argcount; ++i)
24205 {
24206 if (i > 0)
24207 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024208 if (argvars[i].v_type == VAR_NUMBER)
24209 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210 else
24211 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024212 /* Do not want errors such as E724 here. */
24213 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024214 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024215 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024216 if (s != NULL)
24217 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024218 if (vim_strsize(s) > MSG_BUF_CLEN)
24219 {
24220 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24221 s = buf;
24222 }
24223 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024224 vim_free(tofree);
24225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 }
24227 }
24228 msg_puts((char_u *)")");
24229 }
24230 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024231
24232 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 --no_wait_return;
24234 }
24235 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024236#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024237 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024238 {
24239 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24240 func_do_profile(fp);
24241 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024242 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024243 {
24244 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024245 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024246 profile_zero(&fp->uf_tm_children);
24247 }
24248 script_prof_save(&wait_start);
24249 }
24250#endif
24251
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024253 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 save_did_emsg = did_emsg;
24255 did_emsg = FALSE;
24256
24257 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024258 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24260
24261 --RedrawingDisabled;
24262
24263 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024266 clear_tv(rettv);
24267 rettv->v_type = VAR_NUMBER;
24268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270
Bram Moolenaar05159a02005-02-26 23:04:13 +000024271#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024272 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024273 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024274 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024275 profile_end(&call_start);
24276 profile_sub_wait(&wait_start, &call_start);
24277 profile_add(&fp->uf_tm_total, &call_start);
24278 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024279 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024280 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024281 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24282 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024283 }
24284 }
24285#endif
24286
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 /* when being verbose, mention the return value */
24288 if (p_verbose >= 12)
24289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024291 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024294 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024295 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024296 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024297 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024300 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024301 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024302 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024303 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024304
Bram Moolenaar555b2802005-05-19 21:08:39 +000024305 /* The value may be very long. Skip the middle part, so that we
24306 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024307 * truncate it at the end. Don't want errors such as E724 here. */
24308 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024309 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024310 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024311 if (s != NULL)
24312 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024313 if (vim_strsize(s) > MSG_BUF_CLEN)
24314 {
24315 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24316 s = buf;
24317 }
24318 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024319 vim_free(tofree);
24320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 }
24322 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024323
24324 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 --no_wait_return;
24326 }
24327
24328 vim_free(sourcing_name);
24329 sourcing_name = save_sourcing_name;
24330 sourcing_lnum = save_sourcing_lnum;
24331 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024332#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024333 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024334 script_prof_restore(&wait_start);
24335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336
24337 if (p_verbose >= 12 && sourcing_name != NULL)
24338 {
24339 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024340 verbose_enter_scroll();
24341
Bram Moolenaar555b2802005-05-19 21:08:39 +000024342 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024344
24345 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 --no_wait_return;
24347 }
24348
24349 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024350 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024352
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024353 /* If the a:000 list and the l: and a: dicts are not referenced we can
24354 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024355 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24356 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24357 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24358 {
24359 free_funccal(fc, FALSE);
24360 }
24361 else
24362 {
24363 hashitem_T *hi;
24364 listitem_T *li;
24365 int todo;
24366
24367 /* "fc" is still in use. This can happen when returning "a:000" or
24368 * assigning "l:" to a global variable.
24369 * Link "fc" in the list for garbage collection later. */
24370 fc->caller = previous_funccal;
24371 previous_funccal = fc;
24372
24373 /* Make a copy of the a: variables, since we didn't do that above. */
24374 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24375 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24376 {
24377 if (!HASHITEM_EMPTY(hi))
24378 {
24379 --todo;
24380 v = HI2DI(hi);
24381 copy_tv(&v->di_tv, &v->di_tv);
24382 }
24383 }
24384
24385 /* Make a copy of the a:000 items, since we didn't do that above. */
24386 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24387 copy_tv(&li->li_tv, &li->li_tv);
24388 }
24389}
24390
24391/*
24392 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024393 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024394 */
24395 static int
24396can_free_funccal(fc, copyID)
24397 funccall_T *fc;
24398 int copyID;
24399{
24400 return (fc->l_varlist.lv_copyID != copyID
24401 && fc->l_vars.dv_copyID != copyID
24402 && fc->l_avars.dv_copyID != copyID);
24403}
24404
24405/*
24406 * Free "fc" and what it contains.
24407 */
24408 static void
24409free_funccal(fc, free_val)
24410 funccall_T *fc;
24411 int free_val; /* a: vars were allocated */
24412{
24413 listitem_T *li;
24414
24415 /* The a: variables typevals may not have been allocated, only free the
24416 * allocated variables. */
24417 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24418
24419 /* free all l: variables */
24420 vars_clear(&fc->l_vars.dv_hashtab);
24421
24422 /* Free the a:000 variables if they were allocated. */
24423 if (free_val)
24424 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24425 clear_tv(&li->li_tv);
24426
24427 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428}
24429
24430/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024431 * Add a number variable "name" to dict "dp" with value "nr".
24432 */
24433 static void
24434add_nr_var(dp, v, name, nr)
24435 dict_T *dp;
24436 dictitem_T *v;
24437 char *name;
24438 varnumber_T nr;
24439{
24440 STRCPY(v->di_key, name);
24441 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24442 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24443 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024444 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024445 v->di_tv.vval.v_number = nr;
24446}
24447
24448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 * ":return [expr]"
24450 */
24451 void
24452ex_return(eap)
24453 exarg_T *eap;
24454{
24455 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 int returning = FALSE;
24458
24459 if (current_funccal == NULL)
24460 {
24461 EMSG(_("E133: :return not inside a function"));
24462 return;
24463 }
24464
24465 if (eap->skip)
24466 ++emsg_skip;
24467
24468 eap->nextcmd = NULL;
24469 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024470 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 {
24472 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024473 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 }
24477 /* It's safer to return also on error. */
24478 else if (!eap->skip)
24479 {
24480 /*
24481 * Return unless the expression evaluation has been cancelled due to an
24482 * aborting error, an interrupt, or an exception.
24483 */
24484 if (!aborting())
24485 returning = do_return(eap, FALSE, TRUE, NULL);
24486 }
24487
24488 /* When skipping or the return gets pending, advance to the next command
24489 * in this line (!returning). Otherwise, ignore the rest of the line.
24490 * Following lines will be ignored by get_func_line(). */
24491 if (returning)
24492 eap->nextcmd = NULL;
24493 else if (eap->nextcmd == NULL) /* no argument */
24494 eap->nextcmd = check_nextcmd(arg);
24495
24496 if (eap->skip)
24497 --emsg_skip;
24498}
24499
24500/*
24501 * Return from a function. Possibly makes the return pending. Also called
24502 * for a pending return at the ":endtry" or after returning from an extra
24503 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024504 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024505 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 * FALSE when the return gets pending.
24507 */
24508 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024509do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 exarg_T *eap;
24511 int reanimate;
24512 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024513 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514{
24515 int idx;
24516 struct condstack *cstack = eap->cstack;
24517
24518 if (reanimate)
24519 /* Undo the return. */
24520 current_funccal->returned = FALSE;
24521
24522 /*
24523 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24524 * not in its finally clause (which then is to be executed next) is found.
24525 * In this case, make the ":return" pending for execution at the ":endtry".
24526 * Otherwise, return normally.
24527 */
24528 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24529 if (idx >= 0)
24530 {
24531 cstack->cs_pending[idx] = CSTP_RETURN;
24532
24533 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024534 /* A pending return again gets pending. "rettv" points to an
24535 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024537 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 else
24539 {
24540 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024541 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024543 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024545 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 {
24547 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024548 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024549 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 else
24551 EMSG(_(e_outofmem));
24552 }
24553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024554 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555
24556 if (reanimate)
24557 {
24558 /* The pending return value could be overwritten by a ":return"
24559 * without argument in a finally clause; reset the default
24560 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024561 current_funccal->rettv->v_type = VAR_NUMBER;
24562 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 }
24564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024565 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 }
24567 else
24568 {
24569 current_funccal->returned = TRUE;
24570
24571 /* If the return is carried out now, store the return value. For
24572 * a return immediately after reanimation, the value is already
24573 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024574 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024576 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024577 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024579 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 }
24581 }
24582
24583 return idx < 0;
24584}
24585
24586/*
24587 * Free the variable with a pending return value.
24588 */
24589 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024590discard_pending_return(rettv)
24591 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592{
Bram Moolenaar33570922005-01-25 22:26:29 +000024593 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594}
24595
24596/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024597 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 * is an allocated string. Used by report_pending() for verbose messages.
24599 */
24600 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024601get_return_cmd(rettv)
24602 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024604 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024605 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024606 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024608 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024609 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024610 if (s == NULL)
24611 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024612
24613 STRCPY(IObuff, ":return ");
24614 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24615 if (STRLEN(s) + 8 >= IOSIZE)
24616 STRCPY(IObuff + IOSIZE - 4, "...");
24617 vim_free(tofree);
24618 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619}
24620
24621/*
24622 * Get next function line.
24623 * Called by do_cmdline() to get the next line.
24624 * Returns allocated string, or NULL for end of function.
24625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 char_u *
24627get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024628 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024630 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631{
Bram Moolenaar33570922005-01-25 22:26:29 +000024632 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024633 ufunc_T *fp = fcp->func;
24634 char_u *retval;
24635 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636
24637 /* If breakpoints have been added/deleted need to check for it. */
24638 if (fcp->dbg_tick != debug_tick)
24639 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024640 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 sourcing_lnum);
24642 fcp->dbg_tick = debug_tick;
24643 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024644#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024645 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024646 func_line_end(cookie);
24647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648
Bram Moolenaar05159a02005-02-26 23:04:13 +000024649 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024650 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24651 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 retval = NULL;
24653 else
24654 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024655 /* Skip NULL lines (continuation lines). */
24656 while (fcp->linenr < gap->ga_len
24657 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24658 ++fcp->linenr;
24659 if (fcp->linenr >= gap->ga_len)
24660 retval = NULL;
24661 else
24662 {
24663 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24664 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024665#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024666 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024667 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024668#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 }
24671
24672 /* Did we encounter a breakpoint? */
24673 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24674 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024675 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024677 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 sourcing_lnum);
24679 fcp->dbg_tick = debug_tick;
24680 }
24681
24682 return retval;
24683}
24684
Bram Moolenaar05159a02005-02-26 23:04:13 +000024685#if defined(FEAT_PROFILE) || defined(PROTO)
24686/*
24687 * Called when starting to read a function line.
24688 * "sourcing_lnum" must be correct!
24689 * When skipping lines it may not actually be executed, but we won't find out
24690 * until later and we need to store the time now.
24691 */
24692 void
24693func_line_start(cookie)
24694 void *cookie;
24695{
24696 funccall_T *fcp = (funccall_T *)cookie;
24697 ufunc_T *fp = fcp->func;
24698
24699 if (fp->uf_profiling && sourcing_lnum >= 1
24700 && sourcing_lnum <= fp->uf_lines.ga_len)
24701 {
24702 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024703 /* Skip continuation lines. */
24704 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24705 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024706 fp->uf_tml_execed = FALSE;
24707 profile_start(&fp->uf_tml_start);
24708 profile_zero(&fp->uf_tml_children);
24709 profile_get_wait(&fp->uf_tml_wait);
24710 }
24711}
24712
24713/*
24714 * Called when actually executing a function line.
24715 */
24716 void
24717func_line_exec(cookie)
24718 void *cookie;
24719{
24720 funccall_T *fcp = (funccall_T *)cookie;
24721 ufunc_T *fp = fcp->func;
24722
24723 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24724 fp->uf_tml_execed = TRUE;
24725}
24726
24727/*
24728 * Called when done with a function line.
24729 */
24730 void
24731func_line_end(cookie)
24732 void *cookie;
24733{
24734 funccall_T *fcp = (funccall_T *)cookie;
24735 ufunc_T *fp = fcp->func;
24736
24737 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24738 {
24739 if (fp->uf_tml_execed)
24740 {
24741 ++fp->uf_tml_count[fp->uf_tml_idx];
24742 profile_end(&fp->uf_tml_start);
24743 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024745 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24746 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024747 }
24748 fp->uf_tml_idx = -1;
24749 }
24750}
24751#endif
24752
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753/*
24754 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024755 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 */
24757 int
24758func_has_ended(cookie)
24759 void *cookie;
24760{
Bram Moolenaar33570922005-01-25 22:26:29 +000024761 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762
24763 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24764 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024765 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 || fcp->returned);
24767}
24768
24769/*
24770 * return TRUE if cookie indicates a function which "abort"s on errors.
24771 */
24772 int
24773func_has_abort(cookie)
24774 void *cookie;
24775{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777}
24778
24779#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24780typedef enum
24781{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024782 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24783 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24784 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785} var_flavour_T;
24786
24787static var_flavour_T var_flavour __ARGS((char_u *varname));
24788
24789 static var_flavour_T
24790var_flavour(varname)
24791 char_u *varname;
24792{
24793 char_u *p = varname;
24794
24795 if (ASCII_ISUPPER(*p))
24796 {
24797 while (*(++p))
24798 if (ASCII_ISLOWER(*p))
24799 return VAR_FLAVOUR_SESSION;
24800 return VAR_FLAVOUR_VIMINFO;
24801 }
24802 else
24803 return VAR_FLAVOUR_DEFAULT;
24804}
24805#endif
24806
24807#if defined(FEAT_VIMINFO) || defined(PROTO)
24808/*
24809 * Restore global vars that start with a capital from the viminfo file
24810 */
24811 int
24812read_viminfo_varlist(virp, writing)
24813 vir_T *virp;
24814 int writing;
24815{
24816 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024817 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819
24820 if (!writing && (find_viminfo_parameter('!') != NULL))
24821 {
24822 tab = vim_strchr(virp->vir_line + 1, '\t');
24823 if (tab != NULL)
24824 {
24825 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024826 switch (*tab)
24827 {
24828 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024829#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024830 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024831#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024832 case 'D': type = VAR_DICT; break;
24833 case 'L': type = VAR_LIST; break;
24834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835
24836 tab = vim_strchr(tab, '\t');
24837 if (tab != NULL)
24838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024839 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024840 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024841 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024843#ifdef FEAT_FLOAT
24844 else if (type == VAR_FLOAT)
24845 (void)string2float(tab + 1, &tv.vval.v_float);
24846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024848 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024849 if (type == VAR_DICT || type == VAR_LIST)
24850 {
24851 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24852
24853 if (etv == NULL)
24854 /* Failed to parse back the dict or list, use it as a
24855 * string. */
24856 tv.v_type = VAR_STRING;
24857 else
24858 {
24859 vim_free(tv.vval.v_string);
24860 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024861 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024862 }
24863 }
24864
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024865 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024866
24867 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024868 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024869 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24870 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 }
24872 }
24873 }
24874
24875 return viminfo_readline(virp);
24876}
24877
24878/*
24879 * Write global vars that start with a capital to the viminfo file
24880 */
24881 void
24882write_viminfo_varlist(fp)
24883 FILE *fp;
24884{
Bram Moolenaar33570922005-01-25 22:26:29 +000024885 hashitem_T *hi;
24886 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024887 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024888 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024889 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024890 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024891 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892
24893 if (find_viminfo_parameter('!') == NULL)
24894 return;
24895
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024896 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024897
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024898 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024901 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024904 this_var = HI2DI(hi);
24905 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024906 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024907 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024908 {
24909 case VAR_STRING: s = "STR"; break;
24910 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024911#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024912 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024913#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024914 case VAR_DICT: s = "DIC"; break;
24915 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024916 default: continue;
24917 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024918 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024919 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024920 if (p != NULL)
24921 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024922 vim_free(tofree);
24923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 }
24925 }
24926}
24927#endif
24928
24929#if defined(FEAT_SESSION) || defined(PROTO)
24930 int
24931store_session_globals(fd)
24932 FILE *fd;
24933{
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 hashitem_T *hi;
24935 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024936 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 char_u *p, *t;
24938
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024939 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024940 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024942 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024944 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024945 this_var = HI2DI(hi);
24946 if ((this_var->di_tv.v_type == VAR_NUMBER
24947 || this_var->di_tv.v_type == VAR_STRING)
24948 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024949 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024950 /* Escape special characters with a backslash. Turn a LF and
24951 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024952 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024953 (char_u *)"\\\"\n\r");
24954 if (p == NULL) /* out of memory */
24955 break;
24956 for (t = p; *t != NUL; ++t)
24957 if (*t == '\n')
24958 *t = 'n';
24959 else if (*t == '\r')
24960 *t = 'r';
24961 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024962 this_var->di_key,
24963 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24964 : ' ',
24965 p,
24966 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24967 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024968 || put_eol(fd) == FAIL)
24969 {
24970 vim_free(p);
24971 return FAIL;
24972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 vim_free(p);
24974 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024975#ifdef FEAT_FLOAT
24976 else if (this_var->di_tv.v_type == VAR_FLOAT
24977 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24978 {
24979 float_T f = this_var->di_tv.vval.v_float;
24980 int sign = ' ';
24981
24982 if (f < 0)
24983 {
24984 f = -f;
24985 sign = '-';
24986 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024987 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024988 this_var->di_key, sign, f) < 0)
24989 || put_eol(fd) == FAIL)
24990 return FAIL;
24991 }
24992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993 }
24994 }
24995 return OK;
24996}
24997#endif
24998
Bram Moolenaar661b1822005-07-28 22:36:45 +000024999/*
25000 * Display script name where an item was last set.
25001 * Should only be invoked when 'verbose' is non-zero.
25002 */
25003 void
25004last_set_msg(scriptID)
25005 scid_T scriptID;
25006{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025007 char_u *p;
25008
Bram Moolenaar661b1822005-07-28 22:36:45 +000025009 if (scriptID != 0)
25010 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025011 p = home_replace_save(NULL, get_scriptname(scriptID));
25012 if (p != NULL)
25013 {
25014 verbose_enter();
25015 MSG_PUTS(_("\n\tLast set from "));
25016 MSG_PUTS(p);
25017 vim_free(p);
25018 verbose_leave();
25019 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025020 }
25021}
25022
Bram Moolenaard812df62008-11-09 12:46:09 +000025023/*
25024 * List v:oldfiles in a nice way.
25025 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025026 void
25027ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025028 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025029{
25030 list_T *l = vimvars[VV_OLDFILES].vv_list;
25031 listitem_T *li;
25032 int nr = 0;
25033
25034 if (l == NULL)
25035 msg((char_u *)_("No old files"));
25036 else
25037 {
25038 msg_start();
25039 msg_scroll = TRUE;
25040 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25041 {
25042 msg_outnum((long)++nr);
25043 MSG_PUTS(": ");
25044 msg_outtrans(get_tv_string(&li->li_tv));
25045 msg_putchar('\n');
25046 out_flush(); /* output one line at a time */
25047 ui_breakcheck();
25048 }
25049 /* Assume "got_int" was set to truncate the listing. */
25050 got_int = FALSE;
25051
25052#ifdef FEAT_BROWSE_CMD
25053 if (cmdmod.browse)
25054 {
25055 quit_more = FALSE;
25056 nr = prompt_for_number(FALSE);
25057 msg_starthere();
25058 if (nr > 0)
25059 {
25060 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25061 (long)nr);
25062
25063 if (p != NULL)
25064 {
25065 p = expand_env_save(p);
25066 eap->arg = p;
25067 eap->cmdidx = CMD_edit;
25068 cmdmod.browse = FALSE;
25069 do_exedit(eap, NULL);
25070 vim_free(p);
25071 }
25072 }
25073 }
25074#endif
25075 }
25076}
25077
Bram Moolenaar53744302015-07-17 17:38:22 +020025078/* reset v:option_new, v:option_old and v:option_type */
25079 void
25080reset_v_option_vars()
25081{
25082 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25083 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25084 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25085}
25086
25087
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088#endif /* FEAT_EVAL */
25089
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025091#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092
25093#ifdef WIN3264
25094/*
25095 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25096 */
25097static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25098static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25099static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25100
25101/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025102 * Get the short path (8.3) for the filename in "fnamep".
25103 * Only works for a valid file name.
25104 * When the path gets longer "fnamep" is changed and the allocated buffer
25105 * is put in "bufp".
25106 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25107 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 */
25109 static int
25110get_short_pathname(fnamep, bufp, fnamelen)
25111 char_u **fnamep;
25112 char_u **bufp;
25113 int *fnamelen;
25114{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025115 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 char_u *newbuf;
25117
25118 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119 l = GetShortPathName(*fnamep, *fnamep, len);
25120 if (l > len - 1)
25121 {
25122 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025123 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 newbuf = vim_strnsave(*fnamep, l);
25125 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025126 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127
25128 vim_free(*bufp);
25129 *fnamep = *bufp = newbuf;
25130
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025131 /* Really should always succeed, as the buffer is big enough. */
25132 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 }
25134
25135 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025136 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137}
25138
25139/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025140 * Get the short path (8.3) for the filename in "fname". The converted
25141 * path is returned in "bufp".
25142 *
25143 * Some of the directories specified in "fname" may not exist. This function
25144 * will shorten the existing directories at the beginning of the path and then
25145 * append the remaining non-existing path.
25146 *
25147 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025148 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025149 * bufp - Pointer to an allocated buffer for the filename.
25150 * fnamelen - Length of the filename pointed to by fname
25151 *
25152 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 */
25154 static int
25155shortpath_for_invalid_fname(fname, bufp, fnamelen)
25156 char_u **fname;
25157 char_u **bufp;
25158 int *fnamelen;
25159{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025160 char_u *short_fname, *save_fname, *pbuf_unused;
25161 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025163 int old_len, len;
25164 int new_len, sfx_len;
25165 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166
25167 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025168 old_len = *fnamelen;
25169 save_fname = vim_strnsave(*fname, old_len);
25170 pbuf_unused = NULL;
25171 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025173 endp = save_fname + old_len - 1; /* Find the end of the copy */
25174 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025176 /*
25177 * Try shortening the supplied path till it succeeds by removing one
25178 * directory at a time from the tail of the path.
25179 */
25180 len = 0;
25181 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025183 /* go back one path-separator */
25184 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25185 --endp;
25186 if (endp <= save_fname)
25187 break; /* processed the complete path */
25188
25189 /*
25190 * Replace the path separator with a NUL and try to shorten the
25191 * resulting path.
25192 */
25193 ch = *endp;
25194 *endp = 0;
25195 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025196 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025197 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25198 {
25199 retval = FAIL;
25200 goto theend;
25201 }
25202 *endp = ch; /* preserve the string */
25203
25204 if (len > 0)
25205 break; /* successfully shortened the path */
25206
25207 /* failed to shorten the path. Skip the path separator */
25208 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 }
25210
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025211 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025213 /*
25214 * Succeeded in shortening the path. Now concatenate the shortened
25215 * path with the remaining path at the tail.
25216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025218 /* Compute the length of the new path. */
25219 sfx_len = (int)(save_endp - endp) + 1;
25220 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025222 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025224 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025226 /* There is not enough space in the currently allocated string,
25227 * copy it to a buffer big enough. */
25228 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025230 {
25231 retval = FAIL;
25232 goto theend;
25233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 }
25235 else
25236 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025237 /* Transfer short_fname to the main buffer (it's big enough),
25238 * unless get_short_pathname() did its work in-place. */
25239 *fname = *bufp = save_fname;
25240 if (short_fname != save_fname)
25241 vim_strncpy(save_fname, short_fname, len);
25242 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025244
25245 /* concat the not-shortened part of the path */
25246 vim_strncpy(*fname + len, endp, sfx_len);
25247 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025249
25250theend:
25251 vim_free(pbuf_unused);
25252 vim_free(save_fname);
25253
25254 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255}
25256
25257/*
25258 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025259 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 */
25261 static int
25262shortpath_for_partial(fnamep, bufp, fnamelen)
25263 char_u **fnamep;
25264 char_u **bufp;
25265 int *fnamelen;
25266{
25267 int sepcount, len, tflen;
25268 char_u *p;
25269 char_u *pbuf, *tfname;
25270 int hasTilde;
25271
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025272 /* Count up the path separators from the RHS.. so we know which part
25273 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025275 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 if (vim_ispathsep(*p))
25277 ++sepcount;
25278
25279 /* Need full path first (use expand_env() to remove a "~/") */
25280 hasTilde = (**fnamep == '~');
25281 if (hasTilde)
25282 pbuf = tfname = expand_env_save(*fnamep);
25283 else
25284 pbuf = tfname = FullName_save(*fnamep, FALSE);
25285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025286 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025288 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290
25291 if (len == 0)
25292 {
25293 /* Don't have a valid filename, so shorten the rest of the
25294 * path if we can. This CAN give us invalid 8.3 filenames, but
25295 * there's not a lot of point in guessing what it might be.
25296 */
25297 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025298 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300 }
25301
25302 /* Count the paths backward to find the beginning of the desired string. */
25303 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025304 {
25305#ifdef FEAT_MBYTE
25306 if (has_mbyte)
25307 p -= mb_head_off(tfname, p);
25308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 if (vim_ispathsep(*p))
25310 {
25311 if (sepcount == 0 || (hasTilde && sepcount == 1))
25312 break;
25313 else
25314 sepcount --;
25315 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 if (hasTilde)
25318 {
25319 --p;
25320 if (p >= tfname)
25321 *p = '~';
25322 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324 }
25325 else
25326 ++p;
25327
25328 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25329 vim_free(*bufp);
25330 *fnamelen = (int)STRLEN(p);
25331 *bufp = pbuf;
25332 *fnamep = p;
25333
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025334 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335}
25336#endif /* WIN3264 */
25337
25338/*
25339 * Adjust a filename, according to a string of modifiers.
25340 * *fnamep must be NUL terminated when called. When returning, the length is
25341 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025342 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 * When there is an error, *fnamep is set to NULL.
25344 */
25345 int
25346modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25347 char_u *src; /* string with modifiers */
25348 int *usedlen; /* characters after src that are used */
25349 char_u **fnamep; /* file name so far */
25350 char_u **bufp; /* buffer for allocated file name or NULL */
25351 int *fnamelen; /* length of fnamep */
25352{
25353 int valid = 0;
25354 char_u *tail;
25355 char_u *s, *p, *pbuf;
25356 char_u dirname[MAXPATHL];
25357 int c;
25358 int has_fullname = 0;
25359#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025360 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 int has_shortname = 0;
25362#endif
25363
25364repeat:
25365 /* ":p" - full path/file_name */
25366 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25367 {
25368 has_fullname = 1;
25369
25370 valid |= VALID_PATH;
25371 *usedlen += 2;
25372
25373 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25374 if ((*fnamep)[0] == '~'
25375#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25376 && ((*fnamep)[1] == '/'
25377# ifdef BACKSLASH_IN_FILENAME
25378 || (*fnamep)[1] == '\\'
25379# endif
25380 || (*fnamep)[1] == NUL)
25381
25382#endif
25383 )
25384 {
25385 *fnamep = expand_env_save(*fnamep);
25386 vim_free(*bufp); /* free any allocated file name */
25387 *bufp = *fnamep;
25388 if (*fnamep == NULL)
25389 return -1;
25390 }
25391
25392 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025393 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 {
25395 if (vim_ispathsep(*p)
25396 && p[1] == '.'
25397 && (p[2] == NUL
25398 || vim_ispathsep(p[2])
25399 || (p[2] == '.'
25400 && (p[3] == NUL || vim_ispathsep(p[3])))))
25401 break;
25402 }
25403
25404 /* FullName_save() is slow, don't use it when not needed. */
25405 if (*p != NUL || !vim_isAbsName(*fnamep))
25406 {
25407 *fnamep = FullName_save(*fnamep, *p != NUL);
25408 vim_free(*bufp); /* free any allocated file name */
25409 *bufp = *fnamep;
25410 if (*fnamep == NULL)
25411 return -1;
25412 }
25413
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025414#ifdef WIN3264
25415# if _WIN32_WINNT >= 0x0500
25416 if (vim_strchr(*fnamep, '~') != NULL)
25417 {
25418 /* Expand 8.3 filename to full path. Needed to make sure the same
25419 * file does not have two different names.
25420 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25421 p = alloc(_MAX_PATH + 1);
25422 if (p != NULL)
25423 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025424 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025425 {
25426 vim_free(*bufp);
25427 *bufp = *fnamep = p;
25428 }
25429 else
25430 vim_free(p);
25431 }
25432 }
25433# endif
25434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 /* Append a path separator to a directory. */
25436 if (mch_isdir(*fnamep))
25437 {
25438 /* Make room for one or two extra characters. */
25439 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25440 vim_free(*bufp); /* free any allocated file name */
25441 *bufp = *fnamep;
25442 if (*fnamep == NULL)
25443 return -1;
25444 add_pathsep(*fnamep);
25445 }
25446 }
25447
25448 /* ":." - path relative to the current directory */
25449 /* ":~" - path relative to the home directory */
25450 /* ":8" - shortname path - postponed till after */
25451 while (src[*usedlen] == ':'
25452 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25453 {
25454 *usedlen += 2;
25455 if (c == '8')
25456 {
25457#ifdef WIN3264
25458 has_shortname = 1; /* Postpone this. */
25459#endif
25460 continue;
25461 }
25462 pbuf = NULL;
25463 /* Need full path first (use expand_env() to remove a "~/") */
25464 if (!has_fullname)
25465 {
25466 if (c == '.' && **fnamep == '~')
25467 p = pbuf = expand_env_save(*fnamep);
25468 else
25469 p = pbuf = FullName_save(*fnamep, FALSE);
25470 }
25471 else
25472 p = *fnamep;
25473
25474 has_fullname = 0;
25475
25476 if (p != NULL)
25477 {
25478 if (c == '.')
25479 {
25480 mch_dirname(dirname, MAXPATHL);
25481 s = shorten_fname(p, dirname);
25482 if (s != NULL)
25483 {
25484 *fnamep = s;
25485 if (pbuf != NULL)
25486 {
25487 vim_free(*bufp); /* free any allocated file name */
25488 *bufp = pbuf;
25489 pbuf = NULL;
25490 }
25491 }
25492 }
25493 else
25494 {
25495 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25496 /* Only replace it when it starts with '~' */
25497 if (*dirname == '~')
25498 {
25499 s = vim_strsave(dirname);
25500 if (s != NULL)
25501 {
25502 *fnamep = s;
25503 vim_free(*bufp);
25504 *bufp = s;
25505 }
25506 }
25507 }
25508 vim_free(pbuf);
25509 }
25510 }
25511
25512 tail = gettail(*fnamep);
25513 *fnamelen = (int)STRLEN(*fnamep);
25514
25515 /* ":h" - head, remove "/file_name", can be repeated */
25516 /* Don't remove the first "/" or "c:\" */
25517 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25518 {
25519 valid |= VALID_HEAD;
25520 *usedlen += 2;
25521 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025522 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025523 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524 *fnamelen = (int)(tail - *fnamep);
25525#ifdef VMS
25526 if (*fnamelen > 0)
25527 *fnamelen += 1; /* the path separator is part of the path */
25528#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025529 if (*fnamelen == 0)
25530 {
25531 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25532 p = vim_strsave((char_u *)".");
25533 if (p == NULL)
25534 return -1;
25535 vim_free(*bufp);
25536 *bufp = *fnamep = tail = p;
25537 *fnamelen = 1;
25538 }
25539 else
25540 {
25541 while (tail > s && !after_pathsep(s, tail))
25542 mb_ptr_back(*fnamep, tail);
25543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 }
25545
25546 /* ":8" - shortname */
25547 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25548 {
25549 *usedlen += 2;
25550#ifdef WIN3264
25551 has_shortname = 1;
25552#endif
25553 }
25554
25555#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025556 /*
25557 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558 */
25559 if (has_shortname)
25560 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025561 /* Copy the string if it is shortened by :h and when it wasn't copied
25562 * yet, because we are going to change it in place. Avoids changing
25563 * the buffer name for "%:8". */
25564 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 {
25566 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025567 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 return -1;
25569 vim_free(*bufp);
25570 *bufp = *fnamep = p;
25571 }
25572
25573 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025574 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 if (!has_fullname && !vim_isAbsName(*fnamep))
25576 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025577 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578 return -1;
25579 }
25580 else
25581 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025582 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583
Bram Moolenaardc935552011-08-17 15:23:23 +020025584 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025586 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 return -1;
25588
25589 if (l == 0)
25590 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025591 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025593 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 return -1;
25595 }
25596 *fnamelen = l;
25597 }
25598 }
25599#endif /* WIN3264 */
25600
25601 /* ":t" - tail, just the basename */
25602 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25603 {
25604 *usedlen += 2;
25605 *fnamelen -= (int)(tail - *fnamep);
25606 *fnamep = tail;
25607 }
25608
25609 /* ":e" - extension, can be repeated */
25610 /* ":r" - root, without extension, can be repeated */
25611 while (src[*usedlen] == ':'
25612 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25613 {
25614 /* find a '.' in the tail:
25615 * - for second :e: before the current fname
25616 * - otherwise: The last '.'
25617 */
25618 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25619 s = *fnamep - 2;
25620 else
25621 s = *fnamep + *fnamelen - 1;
25622 for ( ; s > tail; --s)
25623 if (s[0] == '.')
25624 break;
25625 if (src[*usedlen + 1] == 'e') /* :e */
25626 {
25627 if (s > tail)
25628 {
25629 *fnamelen += (int)(*fnamep - (s + 1));
25630 *fnamep = s + 1;
25631#ifdef VMS
25632 /* cut version from the extension */
25633 s = *fnamep + *fnamelen - 1;
25634 for ( ; s > *fnamep; --s)
25635 if (s[0] == ';')
25636 break;
25637 if (s > *fnamep)
25638 *fnamelen = s - *fnamep;
25639#endif
25640 }
25641 else if (*fnamep <= tail)
25642 *fnamelen = 0;
25643 }
25644 else /* :r */
25645 {
25646 if (s > tail) /* remove one extension */
25647 *fnamelen = (int)(s - *fnamep);
25648 }
25649 *usedlen += 2;
25650 }
25651
25652 /* ":s?pat?foo?" - substitute */
25653 /* ":gs?pat?foo?" - global substitute */
25654 if (src[*usedlen] == ':'
25655 && (src[*usedlen + 1] == 's'
25656 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25657 {
25658 char_u *str;
25659 char_u *pat;
25660 char_u *sub;
25661 int sep;
25662 char_u *flags;
25663 int didit = FALSE;
25664
25665 flags = (char_u *)"";
25666 s = src + *usedlen + 2;
25667 if (src[*usedlen + 1] == 'g')
25668 {
25669 flags = (char_u *)"g";
25670 ++s;
25671 }
25672
25673 sep = *s++;
25674 if (sep)
25675 {
25676 /* find end of pattern */
25677 p = vim_strchr(s, sep);
25678 if (p != NULL)
25679 {
25680 pat = vim_strnsave(s, (int)(p - s));
25681 if (pat != NULL)
25682 {
25683 s = p + 1;
25684 /* find end of substitution */
25685 p = vim_strchr(s, sep);
25686 if (p != NULL)
25687 {
25688 sub = vim_strnsave(s, (int)(p - s));
25689 str = vim_strnsave(*fnamep, *fnamelen);
25690 if (sub != NULL && str != NULL)
25691 {
25692 *usedlen = (int)(p + 1 - src);
25693 s = do_string_sub(str, pat, sub, flags);
25694 if (s != NULL)
25695 {
25696 *fnamep = s;
25697 *fnamelen = (int)STRLEN(s);
25698 vim_free(*bufp);
25699 *bufp = s;
25700 didit = TRUE;
25701 }
25702 }
25703 vim_free(sub);
25704 vim_free(str);
25705 }
25706 vim_free(pat);
25707 }
25708 }
25709 /* after using ":s", repeat all the modifiers */
25710 if (didit)
25711 goto repeat;
25712 }
25713 }
25714
Bram Moolenaar26df0922014-02-23 23:39:13 +010025715 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25716 {
25717 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25718 if (p == NULL)
25719 return -1;
25720 vim_free(*bufp);
25721 *bufp = *fnamep = p;
25722 *fnamelen = (int)STRLEN(p);
25723 *usedlen += 2;
25724 }
25725
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 return valid;
25727}
25728
25729/*
25730 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25731 * "flags" can be "g" to do a global substitute.
25732 * Returns an allocated string, NULL for error.
25733 */
25734 char_u *
25735do_string_sub(str, pat, sub, flags)
25736 char_u *str;
25737 char_u *pat;
25738 char_u *sub;
25739 char_u *flags;
25740{
25741 int sublen;
25742 regmatch_T regmatch;
25743 int i;
25744 int do_all;
25745 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025746 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 garray_T ga;
25748 char_u *ret;
25749 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025750 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751
25752 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25753 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025754 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755
25756 ga_init2(&ga, 1, 200);
25757
25758 do_all = (flags[0] == 'g');
25759
25760 regmatch.rm_ic = p_ic;
25761 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25762 if (regmatch.regprog != NULL)
25763 {
25764 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025765 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25767 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025768 /* Skip empty match except for first match. */
25769 if (regmatch.startp[0] == regmatch.endp[0])
25770 {
25771 if (zero_width == regmatch.startp[0])
25772 {
25773 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025774 i = MB_PTR2LEN(tail);
25775 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25776 (size_t)i);
25777 ga.ga_len += i;
25778 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025779 continue;
25780 }
25781 zero_width = regmatch.startp[0];
25782 }
25783
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 /*
25785 * Get some space for a temporary buffer to do the substitution
25786 * into. It will contain:
25787 * - The text up to where the match is.
25788 * - The substituted text.
25789 * - The text after the match.
25790 */
25791 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025792 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25794 {
25795 ga_clear(&ga);
25796 break;
25797 }
25798
25799 /* copy the text up to where the match is */
25800 i = (int)(regmatch.startp[0] - tail);
25801 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25802 /* add the substituted text */
25803 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25804 + ga.ga_len + i, TRUE, TRUE, FALSE);
25805 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025806 tail = regmatch.endp[0];
25807 if (*tail == NUL)
25808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 if (!do_all)
25810 break;
25811 }
25812
25813 if (ga.ga_data != NULL)
25814 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25815
Bram Moolenaar473de612013-06-08 18:19:48 +020025816 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 }
25818
25819 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25820 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025821 if (p_cpo == empty_option)
25822 p_cpo = save_cpo;
25823 else
25824 /* Darn, evaluating {sub} expression changed the value. */
25825 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826
25827 return ret;
25828}
25829
25830#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */